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. In multithreading, the data received by the serial port is incorrect

In multithreading, the data received by the serial port is incorrect

Scheduled Pinned Locked Moved Solved General and Desktop
qserialportreadall
13 Posts 6 Posters 3.5k 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.
  • H Offline
    H Offline
    HermesSJC
    wrote on last edited by
    #1

    I use STM32F407 send data by asynchronous serial send data to the upper computer and the operating system was Windows 10 x64.
    the code on STM32F407 is as follows:

    while(1)
    {
      printf("J%.3f",J);
      HAL_Delay(15);
      printf("W%.3f",W);
      HAL_Delay(15);
      printf("G%.3f",G);
      HAL_Delay(15);
      printf("V%.3f",V);
      printf("R%.3f\r\n",R);
      HAL_Delay(40);
      printf("A101N303\r\n");
      HAL_Delay(100);
      J += 0.001;
      W += 0.001;
      G += 0.001;
    }
    

    And I use the api QSerialPort::readAll to read data.the code is as follows

    void MSerialThread::run()
    {
        QByteArray receiveData = "";
        QString readData = "";
        //串口如果打开就永远进行这个线程
        while(serialPort.isOpen())
        {
            //读取串口缓冲区的数据
            receiveData = serialPort.readAll();
    
            //添加倒string类型的末尾
            readData.append(receiveData);
    
            //清除临时数据
            receiveData.clear();
    
            //如果收到 0x0a 0x0d 就说明到底了
            if(readData.endsWith("\r\n"))
            {
                //发送数据
                emit readEnd(readData);
    
                qDebug() << readData;
    
                //清除接收到的数据
                readData.clear();
            }
        }
    }
    

    but sometime I receive the data such as "J1.123W1.2332V1.123R1.123\r\n" instead of "J1.123W1.123G1.123V1.123R1.123\r\n" even "A101N303\r\nJ1.123W1.2332V1.123R1.123\r\n"

    I need some help

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      Why not use Qt's asynchronous nature rather that spinning a while loop without any really breaking conditions ?

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

      H 1 Reply Last reply
      4
      • G Offline
        G Offline
        geronSEU
        Banned
        wrote on last edited by
        #3
        This post is deleted!
        1 Reply Last reply
        -2
        • K Offline
          K Offline
          kuzulis
          Qt Champions 2020
          wrote on last edited by kuzulis
          #4

          I'm surprised, that your application work at all (it should not work) and not has been crashed. :)

          PS: Don't use the threads if you not sure how to use it. Just use signals/slots from a main application thread (as advised before).

          H 1 Reply Last reply
          0
          • K kuzulis

            I'm surprised, that your application work at all (it should not work) and not has been crashed. :)

            PS: Don't use the threads if you not sure how to use it. Just use signals/slots from a main application thread (as advised before).

            H Offline
            H Offline
            HermesSJC
            wrote on last edited by
            #5

            @kuzulis But,these codes refer to the official example "Blocking Slave Example"

            K 1 Reply Last reply
            0
            • SGaistS SGaist

              Hi and welcome to devnet,

              Why not use Qt's asynchronous nature rather that spinning a while loop without any really breaking conditions ?

              H Offline
              H Offline
              HermesSJC
              wrote on last edited by
              #6

              @SGaist excuse me , do you mean that I 'd better use signal and slot?

              1 Reply Last reply
              1
              • H HermesSJC

                @kuzulis But,these codes refer to the official example "Blocking Slave Example"

                K Offline
                K Offline
                kuzulis
                Qt Champions 2020
                wrote on last edited by
                #7

                @HermesSJC said in In multithreading, the data received by the serial port is incorrect:

                @kuzulis But,these codes refer to the official example "Blocking Slave Example"

                Please compare that "official examples" code to your provided code and find differences.

                H 1 Reply Last reply
                0
                • K kuzulis

                  @HermesSJC said in In multithreading, the data received by the serial port is incorrect:

                  @kuzulis But,these codes refer to the official example "Blocking Slave Example"

                  Please compare that "official examples" code to your provided code and find differences.

                  H Offline
                  H Offline
                  HermesSJC
                  wrote on last edited by
                  #8

                  @kuzulis said in In multithreading, the data received by the serial port is incorrect:

                  @HermesSJC said in In multithreading, the data received by the serial port is incorrect:

                  @kuzulis But,these codes refer to the official example "Blocking Slave Example"

                  Please compare that "official examples" code to your provided code and find differences.

                  I know the differences between the "official examples" and the code I wrote. Maybe because my transmission speed and frequency are fast, when using the signal "readyRead", it will loss data when running about three minutes, so I try using child threading to read data from serial port.

                  aha_1980A 1 Reply Last reply
                  0
                  • H HermesSJC

                    @kuzulis said in In multithreading, the data received by the serial port is incorrect:

                    @HermesSJC said in In multithreading, the data received by the serial port is incorrect:

                    @kuzulis But,these codes refer to the official example "Blocking Slave Example"

                    Please compare that "official examples" code to your provided code and find differences.

                    I know the differences between the "official examples" and the code I wrote. Maybe because my transmission speed and frequency are fast, when using the signal "readyRead", it will loss data when running about three minutes, so I try using child threading to read data from serial port.

                    aha_1980A Offline
                    aha_1980A Offline
                    aha_1980
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    hi @HermesSJC,

                    can you please define "fast transmission speed"?

                    serial ports are rather slow, but as the data is buffered, you should not lose a byte when using readyRead.

                    also, which platform and Qt verson is this?

                    regards

                    Qt has to stay free or it will die.

                    H 1 Reply Last reply
                    0
                    • mrdebugM Offline
                      mrdebugM Offline
                      mrdebug
                      wrote on last edited by
                      #10

                      Hi. I love to write multithreaded applications to manage ioT devices and others.
                      So there are devices that send data slowly (for example the IoT devices) but others could send data using serial port at 1 Mb/s and I have to manage them simultineously.
                      Each connection has got a thread and no one byte was missing.
                      You should review your code.

                      Need programmers to hire?
                      www.labcsp.com
                      www.denisgottardello.it
                      GMT+1
                      Skype: mrdebug

                      H 1 Reply Last reply
                      0
                      • aha_1980A aha_1980

                        hi @HermesSJC,

                        can you please define "fast transmission speed"?

                        serial ports are rather slow, but as the data is buffered, you should not lose a byte when using readyRead.

                        also, which platform and Qt verson is this?

                        regards

                        H Offline
                        H Offline
                        HermesSJC
                        wrote on last edited by
                        #11

                        @aha_1980 said in In multithreading, the data received by the serial port is incorrect:

                        hi @HermesSJC,

                        can you please define "fast transmission speed"?

                        serial ports are rather slow, but as the data is buffered, you should not lose a byte when using readyRead.

                        also, which platform and Qt verson is this?

                        regards

                        I 'm sorry that I do loss data when using the signal readyRead (Maybe my data processing isn't perfect)
                        my platform is based on win10 x64 and I use qt5.9.6 (msvc2015 x64) .
                        as the code shown in the stm32 , I send data about from 5Hz to 60Hz and the band rate is 115200.I have tried sending data about 2Hz,the problem of losing data will be gone.(sorry my mother language is not English, so maybe you will free strange when reading my words)

                        1 Reply Last reply
                        0
                        • mrdebugM mrdebug

                          Hi. I love to write multithreaded applications to manage ioT devices and others.
                          So there are devices that send data slowly (for example the IoT devices) but others could send data using serial port at 1 Mb/s and I have to manage them simultineously.
                          Each connection has got a thread and no one byte was missing.
                          You should review your code.

                          H Offline
                          H Offline
                          HermesSJC
                          wrote on last edited by
                          #12

                          @mrdebug said in In multithreading, the data received by the serial port is incorrect:

                          Hi. I love to write multithreaded applications to manage ioT devices and others.
                          So there are devices that send data slowly (for example the IoT devices) but others could send data using serial port at 1 Mb/s and I have to manage them simultineously.
                          Each connection has got a thread and no one byte was missing.
                          You should review your code.

                          I have shown the code on my slave computer at the beginning of the question which is based on STM32F407 and I ' sure that the configuration of register has no problem. I think I have no problem after reviewing it.

                          1 Reply Last reply
                          0
                          • mrdebugM Offline
                            mrdebugM Offline
                            mrdebug
                            wrote on last edited by mrdebug
                            #13

                            Please try

                            while (true) {
                                if (SerialPort.waitForReadyRead(100)) {
                                    while (SerialPort.canReadLine()) qDebug() << SerialPort.readLine();
                                }
                            }
                            

                            Now you can work on the already ready buffer obtained from "SerialPort.readLine();"

                            Need programmers to hire?
                            www.labcsp.com
                            www.denisgottardello.it
                            GMT+1
                            Skype: mrdebug

                            1 Reply Last reply
                            4

                            • Login

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