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
QtWS25 Last Chance

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.
  • DoohamD Offline
    DoohamD Offline
    Dooham
    wrote on 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.

    jsulmJ 1 Reply Last reply
    0
    • DoohamD Dooham

      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.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

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

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

      DoohamD 1 Reply Last reply
      0
      • jsulmJ jsulm

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

        DoohamD Offline
        DoohamD Offline
        Dooham
        wrote on last edited by
        #3

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

        J.HilkJ 1 Reply Last reply
        0
        • DoohamD Dooham

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

          J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on 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.

          DoohamD 1 Reply Last reply
          2
          • J.HilkJ J.Hilk

            @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)?

            DoohamD Offline
            DoohamD Offline
            Dooham
            wrote on last edited by
            #5

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

            jsulmJ J.HilkJ 2 Replies Last reply
            0
            • P Offline
              P Offline
              PavloPonomarov
              wrote on 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
              • DoohamD Dooham

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

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on 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
                • DoohamD Dooham

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

                  J.HilkJ Offline
                  J.HilkJ Offline
                  J.Hilk
                  Moderators
                  wrote on last edited by J.Hilk
                  #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.

                  DoohamD 1 Reply Last reply
                  5
                  • J.HilkJ J.Hilk

                    @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();
                       }
                    }
                    
                    DoohamD Offline
                    DoohamD Offline
                    Dooham
                    wrote on last edited by
                    #9

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

                    1 Reply Last reply
                    1

                    • Login

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