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. serial Communication using Threading
Qt 6.11 is out! See what's new in the release blog

serial Communication using Threading

Scheduled Pinned Locked Moved Solved General and Desktop
30 Posts 8 Posters 3.9k Views 2 Watching
  • 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.
  • V Vijaykarthikeyan

    @JonB When using ready read() signal..it throws the error like this:

    Port opened
    50.66 20.77 17.89
    -4.21704e+37 9.11138e-41 1.95399e-13
    -4.21704e+37 9.11138e-41 1.56319e-13
    50.66 20.77 17.89
    -4.21704e+37 -1.99953e+18 3.76588e-13

    That's Why i choose timer for receiving data

    V Offline
    V Offline
    Vijaykarthikeyan
    wrote on last edited by
    #21

    @Vijaykarthikeyan said in serial Communication using Threading:

    @jsulm When using ready read() signal..it throws the error like this:

    Port opened
    50.66 20.77 17.89
    -4.21704e+37 9.11138e-41 1.95399e-13
    -4.21704e+37 9.11138e-41 1.56319e-13
    50.66 20.77 17.89
    -4.21704e+37 -1.99953e+18 3.76588e-13

    That's Why i choose timer for receiving data

    Already said that corrections are welcomed.. thank you

    jsulmJ 1 Reply Last reply
    0
    • V Vijaykarthikeyan

      @Vijaykarthikeyan said in serial Communication using Threading:

      @jsulm When using ready read() signal..it throws the error like this:

      Port opened
      50.66 20.77 17.89
      -4.21704e+37 9.11138e-41 1.95399e-13
      -4.21704e+37 9.11138e-41 1.56319e-13
      50.66 20.77 17.89
      -4.21704e+37 -1.99953e+18 3.76588e-13

      That's Why i choose timer for receiving data

      Already said that corrections are welcomed.. thank you

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

      @Vijaykarthikeyan said in serial Communication using Threading:

      Already said that corrections are welcomed

      Corrections? Do you expect somebody to write correct code for you?
      You got the suggestion to use readyRead(), accumulate the data and parse it according to the protocol - did you do that? You do not even answer questions...

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

      V 1 Reply Last reply
      0
      • jsulmJ jsulm

        @Vijaykarthikeyan said in serial Communication using Threading:

        Already said that corrections are welcomed

        Corrections? Do you expect somebody to write correct code for you?
        You got the suggestion to use readyRead(), accumulate the data and parse it according to the protocol - did you do that? You do not even answer questions...

        V Offline
        V Offline
        Vijaykarthikeyan
        wrote on last edited by
        #23

        @jsulm on using readyRead() it throws the garbage values in the middle of incoming serial port values,,that's what im mentioning in every thread.

        I didn't tell anyone to write the code for me.. and why I should? I have implemented the method without seeing into any examples and it worked for me? My Question is now gone nowhere. The topic is changed? The actual defintion of correction is not writing the code behalf of me.

        jsulmJ 1 Reply Last reply
        0
        • V Vijaykarthikeyan

          @jsulm on using readyRead() it throws the garbage values in the middle of incoming serial port values,,that's what im mentioning in every thread.

          I didn't tell anyone to write the code for me.. and why I should? I have implemented the method without seeing into any examples and it worked for me? My Question is now gone nowhere. The topic is changed? The actual defintion of correction is not writing the code behalf of me.

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

          @Vijaykarthikeyan said in serial Communication using Threading:

          on using readyRead() it throws the garbage values in the middle of incoming serial port values

          It was already asked before: show your current implementation. How else should anybody know why it does not work? We do not know whether you're accumulating the data correctly and we do not know whether you're parsing accumulated data correctly.

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

          J.HilkJ V 2 Replies Last reply
          1
          • jsulmJ jsulm

            @Vijaykarthikeyan said in serial Communication using Threading:

            on using readyRead() it throws the garbage values in the middle of incoming serial port values

            It was already asked before: show your current implementation. How else should anybody know why it does not work? We do not know whether you're accumulating the data correctly and we do not know whether you're parsing accumulated data correctly.

            J.HilkJ Offline
            J.HilkJ Offline
            J.Hilk
            Moderators
            wrote on last edited by
            #25

            @jsulm @Vijaykarthikeyan and wether you're printing it correctly, simply dumping it into qDebug() will probably lead to "chunk" data interpretation.

            also

            memcpy(&receivedStruct, receivedData2+receivedData.constData(), sizeof(MyStruct));

            I somehow doubt you're familiar with packing rules and restrictions. I would suggest ditching C and giving your Struct an constructor overload that accepts a QByteArray or a QDataStream implementation or simply overload <<


            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.

            V 1 Reply Last reply
            1
            • J.HilkJ J.Hilk

              @jsulm @Vijaykarthikeyan and wether you're printing it correctly, simply dumping it into qDebug() will probably lead to "chunk" data interpretation.

              also

              memcpy(&receivedStruct, receivedData2+receivedData.constData(), sizeof(MyStruct));

              I somehow doubt you're familiar with packing rules and restrictions. I would suggest ditching C and giving your Struct an constructor overload that accepts a QByteArray or a QDataStream implementation or simply overload <<

              V Offline
              V Offline
              Vijaykarthikeyan
              wrote on last edited by
              #26

              @J-Hilk Thank you,Sir you are correct. it has to be correctly packed to avoid those conditions. I Pre-determinely know the Struct size.And that helped me to pack the struct from extracting the stream input based on the size.

              1 Reply Last reply
              0
              • jsulmJ jsulm

                @Vijaykarthikeyan said in serial Communication using Threading:

                on using readyRead() it throws the garbage values in the middle of incoming serial port values

                It was already asked before: show your current implementation. How else should anybody know why it does not work? We do not know whether you're accumulating the data correctly and we do not know whether you're parsing accumulated data correctly.

                V Offline
                V Offline
                Vijaykarthikeyan
                wrote on last edited by
                #27

                @jsulm ```
                #include "mainwindow.h"
                #include <QThread>
                #include <thread>

                MainWindow::MainWindow(QWidget *parent)
                : QMainWindow(parent)
                {
                serial = new QSerialPort(this);
                serial->setPortName("COM7");
                serial->setBaudRate(QSerialPort::Baud115200);
                serial->setDataBits(QSerialPort::Data8);
                serial->setParity(QSerialPort::NoParity);
                serial->setStopBits(QSerialPort::OneStop);
                serial->setFlowControl(QSerialPort::NoFlowControl);

                if (serial->open(QIODevice::ReadWrite))
                {
                    qDebug() << "Port opened";
                
                    QThread::msleep(1000);
                    timer = new QTimer(this);
                    connect(timer, &QTimer::timeout, this, &MainWindow::send_data);
                    connect(serial, &QSerialPort::readyRead, this, &MainWindow::read_data);
                    timer->start(500);
                
                }
                else
                {
                    qDebug() << "Port not opened";
                }
                

                }

                MainWindow::~MainWindow()
                {

                }

                void MainWindow::send_data()
                {

                //QByteArray *sendData = new QByteArray();
                    MyStruct sendStruct ;
                    AHRS ahrs_data;
                    ahrs_data.yaw =50.66;
                    ahrs_data.pitch=20.77;
                    ahrs_data.roll=17.89;
                
                    memcpy(sendStruct.payload, &ahrs_data, sizeof(AHRS));
                    sendStruct.magic=0xFD;
                    sendStruct.length=sizeof(AHRS);
                    sendStruct.sequence=1;
                    sendStruct.sysId=1;
                    sendStruct.compId=2;
                    sendStruct.msgId=1;
                
                    //sendData->append(reinterpret_cast<const char*>(&sendStruct), sizeof(MyStruct));
                    serial->write(reinterpret_cast<const char*>(&sendStruct), sizeof(MyStruct));
                
                    //delete sendData;
                

                }

                void MainWindow::read_data()
                {
                if(serial->bytesAvailable())
                {
                QByteArray receivedData,receivedData2;
                MyStruct receivedStruct ;
                receivedData2 = serial->read(1);

                    if(receivedData2.contains(253))
                    {
                
                        receivedData = serial->read(sizeof(MyStruct)-1);
                        memcpy(&receivedStruct, receivedData2+receivedData.constData(), sizeof(MyStruct));
                        AHRS ahrs_data;
                        memcpy(&ahrs_data,receivedStruct.payload,sizeof(AHRS));
                        qDebug()<<ahrs_data.yaw<<"\t"<<ahrs_data.pitch<<"\t"<<ahrs_data.roll;
                
                    }
                
                    else
                    {
                    }
                
                    }
                

                }

                jsulmJ 1 Reply Last reply
                0
                • V Vijaykarthikeyan

                  @jsulm ```
                  #include "mainwindow.h"
                  #include <QThread>
                  #include <thread>

                  MainWindow::MainWindow(QWidget *parent)
                  : QMainWindow(parent)
                  {
                  serial = new QSerialPort(this);
                  serial->setPortName("COM7");
                  serial->setBaudRate(QSerialPort::Baud115200);
                  serial->setDataBits(QSerialPort::Data8);
                  serial->setParity(QSerialPort::NoParity);
                  serial->setStopBits(QSerialPort::OneStop);
                  serial->setFlowControl(QSerialPort::NoFlowControl);

                  if (serial->open(QIODevice::ReadWrite))
                  {
                      qDebug() << "Port opened";
                  
                      QThread::msleep(1000);
                      timer = new QTimer(this);
                      connect(timer, &QTimer::timeout, this, &MainWindow::send_data);
                      connect(serial, &QSerialPort::readyRead, this, &MainWindow::read_data);
                      timer->start(500);
                  
                  }
                  else
                  {
                      qDebug() << "Port not opened";
                  }
                  

                  }

                  MainWindow::~MainWindow()
                  {

                  }

                  void MainWindow::send_data()
                  {

                  //QByteArray *sendData = new QByteArray();
                      MyStruct sendStruct ;
                      AHRS ahrs_data;
                      ahrs_data.yaw =50.66;
                      ahrs_data.pitch=20.77;
                      ahrs_data.roll=17.89;
                  
                      memcpy(sendStruct.payload, &ahrs_data, sizeof(AHRS));
                      sendStruct.magic=0xFD;
                      sendStruct.length=sizeof(AHRS);
                      sendStruct.sequence=1;
                      sendStruct.sysId=1;
                      sendStruct.compId=2;
                      sendStruct.msgId=1;
                  
                      //sendData->append(reinterpret_cast<const char*>(&sendStruct), sizeof(MyStruct));
                      serial->write(reinterpret_cast<const char*>(&sendStruct), sizeof(MyStruct));
                  
                      //delete sendData;
                  

                  }

                  void MainWindow::read_data()
                  {
                  if(serial->bytesAvailable())
                  {
                  QByteArray receivedData,receivedData2;
                  MyStruct receivedStruct ;
                  receivedData2 = serial->read(1);

                      if(receivedData2.contains(253))
                      {
                  
                          receivedData = serial->read(sizeof(MyStruct)-1);
                          memcpy(&receivedStruct, receivedData2+receivedData.constData(), sizeof(MyStruct));
                          AHRS ahrs_data;
                          memcpy(&ahrs_data,receivedStruct.payload,sizeof(AHRS));
                          qDebug()<<ahrs_data.yaw<<"\t"<<ahrs_data.pitch<<"\t"<<ahrs_data.roll;
                  
                      }
                  
                      else
                      {
                      }
                  
                      }
                  

                  }

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

                  @Vijaykarthikeyan said in serial Communication using Threading:

                  receivedData = serial->read(sizeof(MyStruct)-1);

                  I think the problem here is that it can happen that you will not receive the whole struct at once. That's why it was suggested to accumulate received data until you have enough data to extract the whole struct.

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

                  V 1 Reply Last reply
                  1
                  • jsulmJ jsulm

                    @Vijaykarthikeyan said in serial Communication using Threading:

                    receivedData = serial->read(sizeof(MyStruct)-1);

                    I think the problem here is that it can happen that you will not receive the whole struct at once. That's why it was suggested to accumulate received data until you have enough data to extract the whole struct.

                    V Offline
                    V Offline
                    Vijaykarthikeyan
                    wrote on last edited by
                    #29

                    @jsulm ok..I understand. I'll try. If any issue occurs..I'll post that error. Thank you

                    1 Reply Last reply
                    0
                    • V Vijaykarthikeyan has marked this topic as solved on
                    • C Offline
                      C Offline
                      craxedunt
                      wrote on last edited by
                      #30
                      This post is deleted!
                      1 Reply Last reply
                      0

                      • Login

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