Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Troubles with SerialPort
Forum Update on Monday, May 27th 2025

Troubles with SerialPort

Scheduled Pinned Locked Moved Solved General and Desktop
serial portqfile
9 Posts 4 Posters 1.7k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Dooham
    wrote on 11 Mar 2019, 11:49 last edited by
    #1

    Hello,
    I'm new using qt and I have some troubles with QSerialPort. I'm trying to connect Pixhawk to my computer with the USB port and make a little code to create a txt file that contains the information that send the Pixhawk. This is my code:

    myserialport.cpp

    #include "myserialport.h"
    #include <QObject>
    #include <QtSerialPort/QSerialPort>
    #include <QDebug>
    #include<QFile>
    #include <QTextStream>
    #include<QString>
    
    MySerialPort::MySerialPort()
    {
        serial = new QSerialPort(this);
        data= "";
    
        connect(serial, SIGNAL(readyRead()), this, SLOT(readData()));
    
    
    }
    
    void MySerialPort::openSerialPort()
    {
        serial->setPortName("COM4");
        serial->setBaudRate(QSerialPort::Baud9600);
        serial->setDataBits(QSerialPort::Data8);
        serial->setParity(QSerialPort::NoParity);
        serial->setStopBits(QSerialPort::OneStop);
        serial->setFlowControl(QSerialPort::NoFlowControl);
        serial->open(QIODevice::ReadOnly);
    
    
    }
    
    void MySerialPort::closeSerialPort()
    {
        if(serial->isOpen()){
            serial->close();
        }
    }
    
    void MySerialPort::readData()
    {
        /*data = serial->readAll();
        data2 += QString::fromStdString(data.toStdString());
        qDebug()<<data<<"\n la longitud es: "<<data2.length();
        qDebug()<<data2.value(data2.length()-1);
        qDebug()<<"Proximo mensaje";
        //data3 = data;
        //qDebug()<<data3<<"Next";*/
        data.append(serial->readAll());
        QByteArray data1 = serial->readAll();
        data2 += QString::fromStdString(data1.toStdString());
       // data3 = data3 + QString::fromStdString(data.toStdString());
        //data.clear();
        dataAux = QByteArray::fromHex(data);
        for(int i = 0; i<dataAux.size(); i++){
            data3=data3+dataAux.data()[i];
    
        }
    
    
        if(data2.length()>=10){
    
            closeSerialPort();
            qDebug()<<data;
            qDebug()<<"Los datos Auxiliares";
            qDebug()<<dataAux;
            qDebug()<<" ";
            qDebug()<<" ";
            qDebug()<<"EL QString es "<<data3;
            QFile arch ("C:/Users/Alvaro/Desktop/DatosLecturaDron.txt");
            QTextStream out(&arch);
            arch.open(QIODevice::ReadWrite| QIODevice::Truncate| QIODevice::Text);
            out<<data3;
            arch.close();
        }else{
            data.clear();
            dataAux.clear();
        }
    
    
    
    }
    
    

    myserialport.h

    #ifndef MYSERIALPORT_H
    #define MYSERIALPORT_H
    #include <QObject>
    #include <QtSerialPort/QSerialPort>
    
    
    class MySerialPort: public QObject
    {
        Q_OBJECT
    public:
        MySerialPort();
    public slots:
        void openSerialPort();
        void closeSerialPort();
    
        void readData();
    
    
    private:
    QSerialPort *serial;
    QByteArray data;
    QStringList data2;
    QString data3;
    QByteArray dataAux;
    
    
    
    };
    
    #endif // MYSERIALPORT_H
    
    

    main.cpp

    #include <QCoreApplication>
    #include <QtSerialPort/QtSerialPort>
    #include <myserialport.h>
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
        MySerialPort isSerialPort;
        isSerialPort.openSerialPort();
    
        return a.exec();
    }
    

    My issue is that i can read the messages with the QByteArray and show it in the console. However I cannot write the txt file correctly.
    For example, this is one message from the pixhawk:

    0_1552304747669_48d37d81-ad9c-4c15-a777-ff0a854e363c-image.png

    And this what i get in the txt file:

    0_1552304809175_aa42426f-3c83-4351-aa70-e2d16ef3ce03-image.png

    Can Anyone help me?
    PD. sorry for my english, it's not my native language.

    J 1 Reply Last reply 11 Mar 2019, 11:52
    0
    • D Dooham
      11 Mar 2019, 11:49

      Hello,
      I'm new using qt and I have some troubles with QSerialPort. I'm trying to connect Pixhawk to my computer with the USB port and make a little code to create a txt file that contains the information that send the Pixhawk. This is my code:

      myserialport.cpp

      #include "myserialport.h"
      #include <QObject>
      #include <QtSerialPort/QSerialPort>
      #include <QDebug>
      #include<QFile>
      #include <QTextStream>
      #include<QString>
      
      MySerialPort::MySerialPort()
      {
          serial = new QSerialPort(this);
          data= "";
      
          connect(serial, SIGNAL(readyRead()), this, SLOT(readData()));
      
      
      }
      
      void MySerialPort::openSerialPort()
      {
          serial->setPortName("COM4");
          serial->setBaudRate(QSerialPort::Baud9600);
          serial->setDataBits(QSerialPort::Data8);
          serial->setParity(QSerialPort::NoParity);
          serial->setStopBits(QSerialPort::OneStop);
          serial->setFlowControl(QSerialPort::NoFlowControl);
          serial->open(QIODevice::ReadOnly);
      
      
      }
      
      void MySerialPort::closeSerialPort()
      {
          if(serial->isOpen()){
              serial->close();
          }
      }
      
      void MySerialPort::readData()
      {
          /*data = serial->readAll();
          data2 += QString::fromStdString(data.toStdString());
          qDebug()<<data<<"\n la longitud es: "<<data2.length();
          qDebug()<<data2.value(data2.length()-1);
          qDebug()<<"Proximo mensaje";
          //data3 = data;
          //qDebug()<<data3<<"Next";*/
          data.append(serial->readAll());
          QByteArray data1 = serial->readAll();
          data2 += QString::fromStdString(data1.toStdString());
         // data3 = data3 + QString::fromStdString(data.toStdString());
          //data.clear();
          dataAux = QByteArray::fromHex(data);
          for(int i = 0; i<dataAux.size(); i++){
              data3=data3+dataAux.data()[i];
      
          }
      
      
          if(data2.length()>=10){
      
              closeSerialPort();
              qDebug()<<data;
              qDebug()<<"Los datos Auxiliares";
              qDebug()<<dataAux;
              qDebug()<<" ";
              qDebug()<<" ";
              qDebug()<<"EL QString es "<<data3;
              QFile arch ("C:/Users/Alvaro/Desktop/DatosLecturaDron.txt");
              QTextStream out(&arch);
              arch.open(QIODevice::ReadWrite| QIODevice::Truncate| QIODevice::Text);
              out<<data3;
              arch.close();
          }else{
              data.clear();
              dataAux.clear();
          }
      
      
      
      }
      
      

      myserialport.h

      #ifndef MYSERIALPORT_H
      #define MYSERIALPORT_H
      #include <QObject>
      #include <QtSerialPort/QSerialPort>
      
      
      class MySerialPort: public QObject
      {
          Q_OBJECT
      public:
          MySerialPort();
      public slots:
          void openSerialPort();
          void closeSerialPort();
      
          void readData();
      
      
      private:
      QSerialPort *serial;
      QByteArray data;
      QStringList data2;
      QString data3;
      QByteArray dataAux;
      
      
      
      };
      
      #endif // MYSERIALPORT_H
      
      

      main.cpp

      #include <QCoreApplication>
      #include <QtSerialPort/QtSerialPort>
      #include <myserialport.h>
      
      int main(int argc, char *argv[])
      {
          QCoreApplication a(argc, argv);
          MySerialPort isSerialPort;
          isSerialPort.openSerialPort();
      
          return a.exec();
      }
      

      My issue is that i can read the messages with the QByteArray and show it in the console. However I cannot write the txt file correctly.
      For example, this is one message from the pixhawk:

      0_1552304747669_48d37d81-ad9c-4c15-a777-ff0a854e363c-image.png

      And this what i get in the txt file:

      0_1552304809175_aa42426f-3c83-4351-aa70-e2d16ef3ce03-image.png

      Can Anyone help me?
      PD. sorry for my english, it's not my native language.

      J Online
      J Online
      jsulm
      Lifetime Qt Champion
      wrote on 11 Mar 2019, 11:52 last edited by
      #2

      @Dooham Does your device actually send text data or binary?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      D 1 Reply Last reply 11 Mar 2019, 12:00
      0
      • J jsulm
        11 Mar 2019, 11:52

        @Dooham Does your device actually send text data or binary?

        D Offline
        D Offline
        Dooham
        wrote on 11 Mar 2019, 12:00 last edited by
        #3

        @jsulm The pixhawk sends messages in form of string of number in hexadecimal.

        J 1 Reply Last reply 11 Mar 2019, 12:25
        0
        • D Dooham
          11 Mar 2019, 12:00

          @jsulm The pixhawk sends messages in form of string of number in hexadecimal.

          J Offline
          J Offline
          J.Hilk
          Moderators
          wrote on 11 Mar 2019, 12:25 last edited by
          #4

          @Dooham said in Troubles with SerialPort:

          The pixhawk sends messages in form of string of number in hexadecimal.

          that does not clarify much, can your data be any value between 0x00 (0 in dec) and 0xFF(255 in dec) ? or is it limited between 0x30(45 in dec) and 0x39(57 in dec) ( representing the string character 0 -9) and 0x41(65 in dec) and 0x5A(90 in dec) (representing the string character A - Z)?


          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          D 1 Reply Last reply 11 Mar 2019, 12:30
          2
          • J J.Hilk
            11 Mar 2019, 12:25

            @Dooham said in Troubles with SerialPort:

            The pixhawk sends messages in form of string of number in hexadecimal.

            that does not clarify much, can your data be any value between 0x00 (0 in dec) and 0xFF(255 in dec) ? or is it limited between 0x30(45 in dec) and 0x39(57 in dec) ( representing the string character 0 -9) and 0x41(65 in dec) and 0x5A(90 in dec) (representing the string character A - Z)?

            D Offline
            D Offline
            Dooham
            wrote on 11 Mar 2019, 12:30 last edited by
            #5

            @J.Hilk Sorry, the data from the pixhack can be any value between 0x00 and 0xFF.

            J J 2 Replies Last reply 11 Mar 2019, 12:45
            0
            • P Offline
              P Offline
              PavloPonomarov
              wrote on 11 Mar 2019, 12:38 last edited by
              #6

              Do you mean those HEX'es are numeric only? You could QByteArray:split this array to separate strings containing your HEX of each number and convert it as written here

              1 Reply Last reply
              0
              • D Dooham
                11 Mar 2019, 12:30

                @J.Hilk Sorry, the data from the pixhack can be any value between 0x00 and 0xFF.

                J Online
                J Online
                jsulm
                Lifetime Qt Champion
                wrote on 11 Mar 2019, 12:45 last edited by
                #7

                @Dooham In that case you can't expect your file to be readable.

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                1
                • D Dooham
                  11 Mar 2019, 12:30

                  @J.Hilk Sorry, the data from the pixhack can be any value between 0x00 and 0xFF.

                  J Offline
                  J Offline
                  J.Hilk
                  Moderators
                  wrote on 11 Mar 2019, 12:51 last edited by J.Hilk 3 Nov 2019, 13:07
                  #8

                  @Dooham Than a simple QString from QByteArray won't do it.

                  but this should work:

                  void MySerialPort::readData()
                  {
                       QByteArray data = serial->readAll();
                  
                      QString str = QString(data.toHex(' ')); // The space is for readability, will separate each value by a space
                  
                     QFile f (myFile.txt)
                     if(f.open(QIODevice::WriteOnly){
                        QTextStream out(&f);
                        out << str;
                        f.close();
                     }
                  }
                  

                  Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                  Q: What's that?
                  A: It's blue light.
                  Q: What does it do?
                  A: It turns blue.

                  D 1 Reply Last reply 11 Mar 2019, 12:58
                  5
                  • J J.Hilk
                    11 Mar 2019, 12:51

                    @Dooham Than a simple QString from QByteArray won't do it.

                    but this should work:

                    void MySerialPort::readData()
                    {
                         QByteArray data = serial->readAll();
                    
                        QString str = QString(data.toHex(' ')); // The space is for readability, will separate each value by a space
                    
                       QFile f (myFile.txt)
                       if(f.open(QIODevice::WriteOnly){
                          QTextStream out(&f);
                          out << str;
                          f.close();
                       }
                    }
                    
                    D Offline
                    D Offline
                    Dooham
                    wrote on 11 Mar 2019, 12:58 last edited by
                    #9

                    @J.Hilk Thank You it did work. You saved me.

                    1 Reply Last reply
                    1

                    4/9

                    11 Mar 2019, 12:25

                    • Login

                    • Login or register to search.
                    4 out of 9
                    • First post
                      4/9
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved