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
Forum Updated to NodeBB v4.3 + New Features

serial Communication using Threading

Scheduled Pinned Locked Moved Solved General and Desktop
30 Posts 8 Posters 2.4k 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 Offline
    V Offline
    Vijaykarthikeyan
    wrote on last edited by
    #1
    #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(timer, &QTimer::timeout, 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
            {
            }
    
            }
    
    }
    
    
    
    
    Christian EhrlicherC JonBJ V 3 Replies Last reply
    0
    • V Vijaykarthikeyan
      #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(timer, &QTimer::timeout, 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
              {
              }
      
              }
      
      }
      
      
      
      
      Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2
      1. What's your question?
      2. As always when using threads for asnyc operations in Qt - why?
      3. 'QByteArray *sendData = new QByteArray();' - it's not used and for sure not needed on the stack

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      2
      • V Vijaykarthikeyan
        #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(timer, &QTimer::timeout, 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
                {
                }
        
                }
        
        }
        
        
        
        
        JonBJ Online
        JonBJ Online
        JonB
        wrote on last edited by JonB
        #3

        @Vijaykarthikeyan
        Is there a question here?

        Your read_data() assumes that after reading a single byte it can then immediately read further bytes (serial->read(sizeof(MyStruct)-1)). You cannot assume this will be the case (and you do not even check the size of any returned data there, which you absolutely should). This is not the way it works. It is your job to write code which accumulates data received --- possibly through multiple calls to read_data()/serial->read() --- until enough bytes have been received to form a complete structure.

        V 1 Reply Last reply
        1
        • V Vijaykarthikeyan
          #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(timer, &QTimer::timeout, 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
                  {
                  }
          
                  }
          
          }
          
          
          
          
          V Offline
          V Offline
          Vijaykarthikeyan
          wrote on last edited by
          #4

          @Vijaykarthikeyan said in serial Communication using Threading:

          #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(timer, &QTimer::timeout, 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
                  {
                  }
          
                  }
          
          }
          
          
          
          

          I have been trying to do serial port communication using same single serial port where I successfully implemented sending and receiving the struct data. There is no problem in it. But, Now., I want to use Threading .The receiving logic should move to Thread so there's no interruption for sending the struct data. I want to use same serial port. How to create a thread within the same MainWindow.cpp without any need of separate class

          Christian EhrlicherC 1 Reply Last reply
          0
          • V Vijaykarthikeyan

            @Vijaykarthikeyan said in serial Communication using Threading:

            #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(timer, &QTimer::timeout, 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
                    {
                    }
            
                    }
            
            }
            
            
            
            

            I have been trying to do serial port communication using same single serial port where I successfully implemented sending and receiving the struct data. There is no problem in it. But, Now., I want to use Threading .The receiving logic should move to Thread so there's no interruption for sending the struct data. I want to use same serial port. How to create a thread within the same MainWindow.cpp without any need of separate class

            Christian EhrlicherC Online
            Christian EhrlicherC Online
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @Vijaykarthikeyan said in serial Communication using Threading:

            Now., I want to use Threading .The receiving logic should move to Thread so there's no interruption for sending the struct data.

            Why? 'I want' is no reason. And as we already said - it's not needed at all.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            V 1 Reply Last reply
            0
            • JonBJ JonB

              @Vijaykarthikeyan
              Is there a question here?

              Your read_data() assumes that after reading a single byte it can then immediately read further bytes (serial->read(sizeof(MyStruct)-1)). You cannot assume this will be the case (and you do not even check the size of any returned data there, which you absolutely should). This is not the way it works. It is your job to write code which accumulates data received --- possibly through multiple calls to read_data()/serial->read() --- until enough bytes have been received to form a complete structure.

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

              @JonB It works perfectly..it is checking that number 253 .If it matches, it should read the remaining data from the stream. IT gives output like this:

              50.66 20.77 17.89
              50.66 20.77 17.89

              My question which I forgot to add is that I want my read_data() to work in a separate thread. I worked out in separate Class/file. But,it needs the same serial port. So,I'm quering that how to implement threading within the same MainWindow.cpp so I could pass the serial in read_data() function which is running in the thread

              JonBJ 1 Reply Last reply
              0
              • V Vijaykarthikeyan

                @JonB It works perfectly..it is checking that number 253 .If it matches, it should read the remaining data from the stream. IT gives output like this:

                50.66 20.77 17.89
                50.66 20.77 17.89

                My question which I forgot to add is that I want my read_data() to work in a separate thread. I worked out in separate Class/file. But,it needs the same serial port. So,I'm quering that how to implement threading within the same MainWindow.cpp so I could pass the serial in read_data() function which is running in the thread

                JonBJ Online
                JonBJ Online
                JonB
                wrote on last edited by JonB
                #7

                @Vijaykarthikeyan

                It works perfectly

                Then you are "fortunate". The code should be corrected as I wrote. If you insist on implementing it your way, why do you not check the return result of receivedData = serial->read(sizeof(MyStruct)-1);?

                read_data() would be better implemented as a slot attached to readyRead() rather than your QTimer principle.

                As @Christian-Ehrlicher has said, with readyRead() signals there is no need to use a thread here. Everyone wants to get into threads when they are (a) not needed and (b) the cause of coding mistakes (just read how many posts there are about thread problems).

                V 1 Reply Last reply
                2
                • Christian EhrlicherC Christian Ehrlicher

                  @Vijaykarthikeyan said in serial Communication using Threading:

                  Now., I want to use Threading .The receiving logic should move to Thread so there's no interruption for sending the struct data.

                  Why? 'I want' is no reason. And as we already said - it's not needed at all.

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

                  @Christian-Ehrlicher I'm little bit worried of performance. Is it fine to send and receive at the same time. Will it send and receive concurrently (or) parallely without any lag. As, I want much reduced latency. Also, I gone through that threading will be the good way for multiple tasks

                  Christian EhrlicherC 1 Reply Last reply
                  0
                  • V Vijaykarthikeyan

                    @Christian-Ehrlicher I'm little bit worried of performance. Is it fine to send and receive at the same time. Will it send and receive concurrently (or) parallely without any lag. As, I want much reduced latency. Also, I gone through that threading will be the good way for multiple tasks

                    Christian EhrlicherC Online
                    Christian EhrlicherC Online
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on last edited by Christian Ehrlicher
                    #9

                    We already said mulitple times: Qt is asynchronous - your data will be sent in the background already now. No need for threads, esp. not when you did not notice a bottleneck (and don't have much clue about threading).

                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                    Visit the Qt Academy at https://academy.qt.io/catalog

                    V 1 Reply Last reply
                    1
                    • JonBJ JonB

                      @Vijaykarthikeyan

                      It works perfectly

                      Then you are "fortunate". The code should be corrected as I wrote. If you insist on implementing it your way, why do you not check the return result of receivedData = serial->read(sizeof(MyStruct)-1);?

                      read_data() would be better implemented as a slot attached to readyRead() rather than your QTimer principle.

                      As @Christian-Ehrlicher has said, with readyRead() signals there is no need to use a thread here. Everyone wants to get into threads when they are (a) not needed and (b) the cause of coding mistakes (just read how many posts there are about thread problems).

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

                      @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

                      JonBJ V 2 Replies Last reply
                      0
                      • Christian EhrlicherC Christian Ehrlicher

                        We already said mulitple times: Qt is asynchronous - your data will be sent in the background already now. No need for threads, esp. not when you did not notice a bottleneck (and don't have much clue about threading).

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

                        @Christian-Ehrlicher I have done a sample threading example creating 2 separate classes,called the thread in main class. But, it is not using mainWindow.cpp members and functions.

                        But,here,in this case I want to access the port which is in use by mainWindow.cpp..

                        There's where I find the bottle neck

                        SGaistS 1 Reply Last reply
                        0
                        • V Vijaykarthikeyan

                          @Christian-Ehrlicher I have done a sample threading example creating 2 separate classes,called the thread in main class. But, it is not using mainWindow.cpp members and functions.

                          But,here,in this case I want to access the port which is in use by mainWindow.cpp..

                          There's where I find the bottle neck

                          SGaistS Offline
                          SGaistS Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          Hi,

                          As is usual with serial ports, do you have a proper protocol in place to know when you have received a full frame of your data ? Size is no such protocol, time as well.

                          The proper way do handle data is to cumulate what you read until you have a frame and then process it.

                          Don't involve threads in there until it is proven by a real benchmark that you need it. As my fellow already wrote, Qt is asynchronous and does not need threading for most tasks such as serial port handling.

                          Interested in AI ? www.idiap.ch
                          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                          V 1 Reply Last reply
                          2
                          • SGaistS SGaist

                            Hi,

                            As is usual with serial ports, do you have a proper protocol in place to know when you have received a full frame of your data ? Size is no such protocol, time as well.

                            The proper way do handle data is to cumulate what you read until you have a frame and then process it.

                            Don't involve threads in there until it is proven by a real benchmark that you need it. As my fellow already wrote, Qt is asynchronous and does not need threading for most tasks such as serial port handling.

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

                            @SGaist Yes.. It's the universal Mavlink message format. By implementing the above mentioned method. It is correctly sending and receiving the data. This is different from other format of receiving data. We could only have the required data based upon the the reading of the pre determined size as it is sending continously and we dont know when will that magic number 253..it could appear from anywhere but after the pack of data.That's why it is reading the stream based upon the size

                            1 Reply Last reply
                            0
                            • 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

                              JonBJ Online
                              JonBJ Online
                              JonB
                              wrote on last edited by
                              #14

                              @Vijaykarthikeyan said in serial Communication using Threading:

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

                              readyRead() will not throw an error or be in an error case. Your code will be incorrect. I explained what you need to do for correct code for asynchronous serial port reading.

                              Anyway, we cannot convince you of this. If you are determined to use threading then go ahead.

                              V 1 Reply Last reply
                              0
                              • JonBJ JonB

                                @Vijaykarthikeyan said in serial Communication using Threading:

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

                                readyRead() will not throw an error or be in an error case. Your code will be incorrect. I explained what you need to do for correct code for asynchronous serial port reading.

                                Anyway, we cannot convince you of this. If you are determined to use threading then go ahead.

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

                                @JonB No..I'm with the side of Qt community.I seek the community for a small doubt?that's all. If you clarify me or telling me the solution, Ill gladly accept it

                                1 Reply Last reply
                                0
                                • V Offline
                                  V Offline
                                  vrooktreat
                                  wrote on last edited by vrooktreat
                                  #16

                                  The proper way do handle data is to cumulate what you read until you have a frame and then process it.
                                  plus whatsapp

                                  project free tv

                                  V 1 Reply Last reply
                                  1
                                  • V vrooktreat

                                    The proper way do handle data is to cumulate what you read until you have a frame and then process it.
                                    plus whatsapp

                                    project free tv

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

                                    @vrooktreat yes..That's why I have read a struct size of data

                                    JonBJ 1 Reply Last reply
                                    0
                                    • V Vijaykarthikeyan

                                      @vrooktreat yes..That's why I have read a struct size of data

                                      JonBJ Online
                                      JonBJ Online
                                      JonB
                                      wrote on last edited by JonB
                                      #18

                                      @Vijaykarthikeyan
                                      No, you do not accumulate the data received as you should. I have previously said that you just assume receivedData = serial->read(sizeof(MyStruct)-1); will work, you don't bother checking and you don't care. And it assumes read() will always immediately return the rest of the "packet" after the initial byte of 253 arrives, which is just not what Qt/serial port guarantees. @vrooktreat is saying the same.

                                      I am not saying that correcting that will necessarily resolve whatever your issue is (though it likely will for whatever your implementation of readyRead() slot was which went wrong), but it's not correct/makes an unnecessary assumption.

                                      V 1 Reply Last reply
                                      2
                                      • JonBJ JonB

                                        @Vijaykarthikeyan
                                        No, you do not accumulate the data received as you should. I have previously said that you just assume receivedData = serial->read(sizeof(MyStruct)-1); will work, you don't bother checking and you don't care. And it assumes read() will always immediately return the rest of the "packet" after the initial byte of 253 arrives, which is just not what Qt/serial port guarantees. @vrooktreat is saying the same.

                                        I am not saying that correcting that will necessarily resolve whatever your issue is (though it likely will for whatever your implementation of readyRead() slot was which went wrong), but it's not correct/makes an unnecessary assumption.

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

                                        @JonB Ok. I agree with you. I'm doing the wrong way. But,replacing timer with readyRead() signal throws the garbage..this is my 1st issue.If my code implementation is wrong..please correct me.

                                        My aim is to send mavlink like struct data from one device to my device.For that,the 1st byte should be the magic number(in this case, I have used 253(0xFD in hexadecimal)).If and only if matches, it reads through the remaining bytes which is the size of my struct. We can't send/receive struct data type through serial port.That's Why Im using QByteArray.

                                        So,as far as your suggestion, How to cumulate the data? I know this issue is out of text thing which I have asked . Im ready to receive your coorrections

                                        jsulmJ 1 Reply Last reply
                                        0
                                        • V Vijaykarthikeyan

                                          @JonB Ok. I agree with you. I'm doing the wrong way. But,replacing timer with readyRead() signal throws the garbage..this is my 1st issue.If my code implementation is wrong..please correct me.

                                          My aim is to send mavlink like struct data from one device to my device.For that,the 1st byte should be the magic number(in this case, I have used 253(0xFD in hexadecimal)).If and only if matches, it reads through the remaining bytes which is the size of my struct. We can't send/receive struct data type through serial port.That's Why Im using QByteArray.

                                          So,as far as your suggestion, How to cumulate the data? I know this issue is out of text thing which I have asked . Im ready to receive your coorrections

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

                                          @Vijaykarthikeyan said in serial Communication using Threading:

                                          But,replacing timer with readyRead() signal throws the garbage

                                          What does this mean? How does your code using readyRead() look like now? Do you accumulate the data?
                                          You need to accumulate data you get every time readyRead() is emitted until you get a whole frame of data. Just like @SGaist and @JonB suggested.

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

                                          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