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. QSerialPort with Thread miss same data
Qt 6.11 is out! See what's new in the release blog

QSerialPort with Thread miss same data

Scheduled Pinned Locked Moved Solved General and Desktop
32 Posts 6 Posters 17.7k 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.
  • G Gokhan

    @J.Hilk However, when I periodically read the serial port buffer via a thread for 100 ms, it returns the null data, so the serial port buffer is seeming empty and the device is giving the error again.

    J.HilkJ Online
    J.HilkJ Online
    J.Hilk
    Moderators
    wrote on last edited by
    #14

    @Gokhan
    I downloaded your code,

    take a look here and here and create your QThread, how it should be done ;-)

    And don't use the blocking waitForXxx functions, use signal slots with connects.


    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.

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

      @Gokhan
      I downloaded your code,

      take a look here and here and create your QThread, how it should be done ;-)

      And don't use the blocking waitForXxx functions, use signal slots with connects.

      G Offline
      G Offline
      Gokhan
      wrote on last edited by Gokhan
      #15

      @J.Hilk thank you, actually, first I wrote it like in my first post, and after that, I saw that it's not working, started to try all possibilities. I guess that there is a bug in Qt, because the assignee wrote a bug about this subject.
      https://bugreports.qt.io/browse/QTBUG-61237

      @mrdebug I have gotten lots of errors with this Project QtComPort, I didn't compile it, guess that it needs a Linux PC.

      1 Reply Last reply
      0
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #16
        • I guess that there is a bug in Qt, because the assignee wrote a bug about this subject.
          https://bugreports.qt.io/browse/QTBUG-61237

        Hi.
        It is not in Qt. its a windows/platform thing.
        It is a known thing with windows.
        It applies to all apps. Also those not written in Qt.
        https://stackoverflow.com/questions/18041622/how-do-i-stop-windows-from-blocking-the-program-during-a-window-drag-or-menu-but

        1 Reply Last reply
        0
        • G Offline
          G Offline
          Gokhan
          wrote on last edited by
          #17

          @mrjj However, another program is working well like you can see my video. Is this big problem for application? e.g my application must immediately give an error in this condition and stop the communication.
          What do you suggest me in order to solve this problem? I had used the LabVIEW before and developed like this application, it had worked flawlessly.

          mrjjM 1 Reply Last reply
          0
          • G Gokhan

            @mrjj However, another program is working well like you can see my video. Is this big problem for application? e.g my application must immediately give an error in this condition and stop the communication.
            What do you suggest me in order to solve this problem? I had used the LabVIEW before and developed like this application, it had worked flawlessly.

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #18

            @Gokhan
            The normal solution is to use a thread.
            Then it wont be interrupted when dragging/resizing.

            1 Reply Last reply
            1
            • K Offline
              K Offline
              kuzulis
              Qt Champions 2020
              wrote on last edited by kuzulis
              #19

              @Gokhan,

              I have added for you an example "qsp-no-freeze-workaround-windows" which demonstrates how it should work with threads!

              G 1 Reply Last reply
              1
              • K kuzulis

                @Gokhan,

                I have added for you an example "qsp-no-freeze-workaround-windows" which demonstrates how it should work with threads!

                G Offline
                G Offline
                Gokhan
                wrote on last edited by
                #20

                @kuzulis Thank you very much, it's really working well. My first code that is in my first post here looks like this, but there is one different between them, it is inheritance. Is this a problem?

                Also, how should I use qmutex between two classes in order to syncrhronşze? It's not declared the static and global variable is not recommended for it.

                mrjjM 1 Reply Last reply
                0
                • G Gokhan

                  @kuzulis Thank you very much, it's really working well. My first code that is in my first post here looks like this, but there is one different between them, it is inheritance. Is this a problem?

                  Also, how should I use qmutex between two classes in order to syncrhronşze? It's not declared the static and global variable is not recommended for it.

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #21

                  @Gokhan said in QSerialPort with Thread miss same data:

                  how should I use qmutex between two classes in order to syncrhronşze?

                  What do you need to synchronize ?
                  You could use signals to tell other class about new data.

                  1 Reply Last reply
                  0
                  • G Offline
                    G Offline
                    Gokhan
                    wrote on last edited by
                    #22

                    @mrjj I must convert the received data to be meaningful, then will show these in tableview. I thought these data periodically will be converted and shown in two different thread for 100milisec.

                    mrjjM 1 Reply Last reply
                    0
                    • G Gokhan

                      @mrjj I must convert the received data to be meaningful, then will show these in tableview. I thought these data periodically will be converted and shown in two different thread for 100milisec.

                      mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by
                      #23

                      @Gokhan
                      Hi, you cannot use any widget in any other thread than the main thread.
                      But using a signal to tell the some other class about the formatted data will work super.
                      https://stackoverflow.com/questions/15276628/send-data-from-worker-thread-to-gui-thread-in-qt

                      1 Reply Last reply
                      1
                      • G Offline
                        G Offline
                        Gokhan
                        wrote on last edited by Gokhan
                        #24

                        @mrjj Don't I need a mutex if use a signal to tell other class? By the way, thread appends the raw array and after slot received a signal, it will convert the raw array and then clear it. Doesn't it need a mutex to do this process?

                        mrjjM 1 Reply Last reply
                        0
                        • G Gokhan

                          @mrjj Don't I need a mutex if use a signal to tell other class? By the way, thread appends the raw array and after slot received a signal, it will convert the raw array and then clear it. Doesn't it need a mutex to do this process?

                          mrjjM Offline
                          mrjjM Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on last edited by
                          #25

                          @Gokhan
                          The signal will contain a copy of the data so in most cases you do not need to protect it.
                          But depends on your design. If its possible that array is being written to , while you clear it then, yes
                          you need concurrency control.

                          1 Reply Last reply
                          1
                          • G Offline
                            G Offline
                            Gokhan
                            wrote on last edited by
                            #26

                            @mrjj Sometimes the array can be huge size, e.g 100K and conversion can take a long time furthermore it will show these data in tableview. Do not you think it takes a long time? Then which method do you suggest me to use?

                            mrjjM 1 Reply Last reply
                            0
                            • G Gokhan

                              @mrjj Sometimes the array can be huge size, e.g 100K and conversion can take a long time furthermore it will show these data in tableview. Do not you think it takes a long time? Then which method do you suggest me to use?

                              mrjjM Offline
                              mrjjM Offline
                              mrjj
                              Lifetime Qt Champion
                              wrote on last edited by
                              #27

                              @Gokhan
                              Ok, if that big, then a processing/convert thread seems a good idea.

                              1 Reply Last reply
                              1
                              • G Offline
                                G Offline
                                Gokhan
                                wrote on last edited by
                                #28

                                @mrjj I'm thinking of using total three thread, one serial port that is receiving data, second converting the raw data to be meaningful data and the last will show these data in a table. Well, should I use mutex? If yes, How? (a global or another method)

                                1 Reply Last reply
                                0
                                • G Offline
                                  G Offline
                                  Gokhan
                                  wrote on last edited by
                                  #29

                                  To synchronize I used a global mutex, while running the convert class, everything is normal and it has converted successfully.
                                  However, another class to show data has a problem, it's giving an error about visual c++. Also while holding left button on the bar it's not running.
                                  When rowData<< new QStandardItem(strTableCount); and tableView->scrollToBottom(); is actived, it gives immediately an error. Where do I make mistake again?

                                  class DisplayData : public QThread
                                  {
                                  public:
                                      DisplayData () {}
                                  
                                      QTableView *tableView;
                                      uint32_t tableCount=0;
                                      uint32_t bufferSize = 100;
                                      QStandardItemModel *model;
                                  
                                      void init(QTableView *table){
                                  
                                          tableView = table;
                                          model = new QStandardItemModel();
                                          tableView->setModel(model);
                                          model->setHorizontalHeaderLabels(QString("Number;Time(sec);ID;Ext/Std;Rtr/Data;Lenght;D0;D1;D2;D3;D4;D5;D6;D7").split(";"));
                                          QHeaderView *verticalHeader = tableView->verticalHeader();
                                          verticalHeader->setVisible(false);
                                          verticalHeader->setSectionResizeMode(QHeaderView::Fixed);
                                          verticalHeader->setDefaultSectionSize(20);
                                  
                                          tableView->setColumnWidth(0,60);
                                          tableView->setColumnWidth(1,70);
                                          tableView->setColumnWidth(2,70);
                                          tableView->setColumnWidth(3,50);
                                          tableView->setColumnWidth(4,60);
                                          tableView->setColumnWidth(5,50);
                                          tableView->setColumnWidth(6,40);
                                          tableView->setColumnWidth(7,40);
                                          tableView->setColumnWidth(8,40);
                                          tableView->setColumnWidth(9,40);
                                          tableView->setColumnWidth(10,40);
                                          tableView->setColumnWidth(11,40);
                                          tableView->setColumnWidth(12,40);
                                          tableView->setColumnWidth(13,40);
                                      }
                                  
                                      void run() override{
                                          QString str;
                                          if(tableView == NULL)
                                              return;
                                  
                                          while(1){
                                              mutexCanbusData.lock();  // it's global
                                              int dataLength = canbus_data.length();
                                              mutexCanbusData.unlock();
                                              if(dataLength)
                                              {
                                                  for(int i=0;i<dataLength;i++){
                                                      tableCount++;
                                  
                                                      const QMutexLocker locker(&mutexCanbusData);
                                                      QString strTableCount=QString::number(tableCount);
                                                      QString strTime=str.sprintf("%.03f",canbus_data.at(i).time).toUpper();
                                  
                                                      QList<QStandardItem *> rowData;
                                  //                    rowData<< new QStandardItem(strTableCount);
                                  //                   rowData<< new QStandardItem(QString::number(tableCount));
                                  //                    //                    rowData<< new QStandardItem(str.sprintf("%.03f",shadowSerial->canbus_data.at(i).time).toUpper());
                                  //                    //                    rowData<< new QStandardItem(str.sprintf("%08x",shadowSerial->canbus_data.at(i).id.all).toUpper());
                                  //                    //                    rowData<< new QStandardItem((shadowSerial->canbus_data.at(i).attribute.att.id==1) ? "Ext":"Std");
                                  //                    //                    rowData<< new QStandardItem((shadowSerial->canbus_data.at(i).attribute.att.rtr==1) ? "Rtr":"Data");
                                  //                    //                    rowData<< new QStandardItem(QString::number(shadowSerial->canbus_data.at(i).attribute.att.number_of_bytes));
                                  //                    //                    rowData<< new QStandardItem(str.sprintf("%02x",shadowSerial->canbus_data.at(i).data[0]).toUpper());
                                  //                    //                    rowData<< new QStandardItem(str.sprintf("%02x",shadowSerial->canbus_data.at(i).data[1]).toUpper());
                                  //                    //                    rowData<< new QStandardItem(str.sprintf("%02x",shadowSerial->canbus_data.at(i).data[2]).toUpper());
                                  //                    //                    rowData<< new QStandardItem(str.sprintf("%02x",shadowSerial->canbus_data.at(i).data[3]).toUpper());
                                  //                    //                    rowData<< new QStandardItem(str.sprintf("%02x",shadowSerial->canbus_data.at(i).data[4]).toUpper());
                                  //                    //                    rowData<< new QStandardItem(str.sprintf("%02x",shadowSerial->canbus_data.at(i).data[5]).toUpper());
                                  //                    //                    rowData<< new QStandardItem(str.sprintf("%02x",shadowSerial->canbus_data.at(i).data[6]).toUpper());
                                  //                    //                    rowData<< new QStandardItem(str.sprintf("%02x",shadowSerial->canbus_data.at(i).data[7]).toUpper());
                                  
                                  //                    //                    for(uint8_t i=0;i<14;i++)
                                  //                    //                        rowData[i]->setTextAlignment(Qt::AlignCenter);
                                                      model->appendRow(rowData);
                                  
                                                      if(tableCount>bufferSize){
                                                          model->removeRow(0);
                                                      }
                                                  }
                                                  mutexCanbusData.lock();
                                                  canbus_data.clear();
                                                  mutexCanbusData.unlock();
                                              }
                                              //tableView->scrollToBottom(); 
                                              QThread::msleep(100);
                                          }
                                      }
                                  };```
                                  
                                  1 Reply Last reply
                                  0
                                  • mrjjM Offline
                                    mrjjM Offline
                                    mrjj
                                    Lifetime Qt Champion
                                    wrote on last edited by
                                    #30

                                    Hi, you CANNOT use QTableView in another thread. Only in main thread. ( the gui)

                                    https://stackoverflow.com/questions/13605141/best-strategy-to-update-qtableview-with-data-in-real-time-from-different-threads

                                    1 Reply Last reply
                                    2
                                    • G Offline
                                      G Offline
                                      Gokhan
                                      wrote on last edited by
                                      #31

                                      @mrjj I got it, thank you very much for your interest. In my opinion, this topic will help the developers who are newbie in Qt like me :)

                                      mrjjM 1 Reply Last reply
                                      1
                                      • G Gokhan

                                        @mrjj I got it, thank you very much for your interest. In my opinion, this topic will help the developers who are newbie in Qt like me :)

                                        mrjjM Offline
                                        mrjjM Offline
                                        mrjj
                                        Lifetime Qt Champion
                                        wrote on last edited by
                                        #32

                                        @Gokhan
                                        Yes it will also help people new to using threads etc.
                                        And how to use serial comm in thread.

                                        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