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. serialPort.read() is reading old data
Forum Updated to NodeBB v4.3 + New Features

serialPort.read() is reading old data

Scheduled Pinned Locked Moved Unsolved General and Desktop
15 Posts 4 Posters 1.6k Views 1 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.
  • A Offline
    A Offline
    Aeroplane123
    wrote on last edited by Aeroplane123
    #1

    I have a program where I am reading and writing to a serial port. The data that is being received appears to be old data. When I unplug the serial port connection from my device and do

    QSerialPort serialPort;
    
    serialPort.read(data);  
    
     QByteArray ba(data);
     qDebug() << "reply from device: " << ba;
    

    Shows me the data that was sent from my device from BEFORE it was unplugged which is impossible, is it reading from a buffer? If so how do I clear the buffer before reading?

    Christian EhrlicherC 1 Reply Last reply
    0
    • A Aeroplane123

      I have a program where I am reading and writing to a serial port. The data that is being received appears to be old data. When I unplug the serial port connection from my device and do

      QSerialPort serialPort;
      
      serialPort.read(data);  
      
       QByteArray ba(data);
       qDebug() << "reply from device: " << ba;
      

      Shows me the data that was sent from my device from BEFORE it was unplugged which is impossible, is it reading from a buffer? If so how do I clear the buffer before reading?

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

      @Aeroplane123 said in serialPort.read() is reading old data:

      is it reading from a buffer?

      Yes, the OS buffers the serial device

      If so how do I clear the buffer before reading?

      You can simply read all data after you opened the device but don't think this will solve your problem - you have to find the start of your data by yourself.

      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
      0
      • Kent-DorfmanK Offline
        Kent-DorfmanK Offline
        Kent-Dorfman
        wrote on last edited by Kent-Dorfman
        #3

        this bothers me on a couple of levels. I'd expect that the open/close semantics should clear/clean any OS buffers.

        OP isn't clear on whether they start/stop/rerun the program between iterations of unplugging the connections.

        I'm wanting to know how "data" is declared. Smells to me like read(data) is just presenting what was in the data buffer prior.

        No indication above whether they are checking the return value of QIODevice.read, since a return value of zero isn't going to touch the data buffer pointed to.

        C 1 Reply Last reply
        0
        • Kent-DorfmanK Kent-Dorfman

          this bothers me on a couple of levels. I'd expect that the open/close semantics should clear/clean any OS buffers.

          OP isn't clear on whether they start/stop/rerun the program between iterations of unplugging the connections.

          I'm wanting to know how "data" is declared. Smells to me like read(data) is just presenting what was in the data buffer prior.

          No indication above whether they are checking the return value of QIODevice.read, since a return value of zero isn't going to touch the data buffer pointed to.

          C Offline
          C Offline
          ChrisW67
          wrote on last edited by
          #4

          @Kent-Dorfman said in serialPort.read() is reading old data:

          I'd expect that the open/close semantics should clear/clean any OS buffers.

          Yes, but I think the OP is unplugging the physical medium and expecting an open QSerialPort to intuit this and clear any already buffered data by magic.

          OP isn't clear on whether they start/stop/rerun the program between iterations of unplugging the connections.

          I think not.

          I'm wanting to know how "data" is declared. Smells to me like read(data) is just presenting what was in the data buffer prior.

          I do not know what type data could be. QIODevice::read() can take a character buffer and size, or a maximum size (returning a QByteArray), but not a bare buffer. I expect this is not the actual code.

          A 1 Reply Last reply
          0
          • C ChrisW67

            @Kent-Dorfman said in serialPort.read() is reading old data:

            I'd expect that the open/close semantics should clear/clean any OS buffers.

            Yes, but I think the OP is unplugging the physical medium and expecting an open QSerialPort to intuit this and clear any already buffered data by magic.

            OP isn't clear on whether they start/stop/rerun the program between iterations of unplugging the connections.

            I think not.

            I'm wanting to know how "data" is declared. Smells to me like read(data) is just presenting what was in the data buffer prior.

            I do not know what type data could be. QIODevice::read() can take a character buffer and size, or a maximum size (returning a QByteArray), but not a bare buffer. I expect this is not the actual code.

            A Offline
            A Offline
            Aeroplane123
            wrote on last edited by Aeroplane123
            #5

            @ChrisW67 You are correct data is not the code "data" in this case was more of pseudo-code. This is what is actually happening...

                memcpy(cmd, "x", 4);
                cmd[4] = CMD_REQUEST_SYSTEM_INFO;
                cmd[5] = 0x00;
                cmd[6] = 0x00;
                cmd[7] = 0x00;
                cmd[8] = calcChecksum(cmd, 8);
            
                char* command = (char *) cmd; //cmd must be converted to char here  
                serialPort.write(command, 9);
                bool writeSuccess = serialPort.write(command, 9) != -1;
            
                QByteArray ba(command, 9);
                qDebug() << "sent: " << ba;
            
                if (writeSuccess)
                {
            		
                    qDebug() << "Write Success!";
             
                    uint8_t reply[32];
            
                    char* getReply = (char *) reply;
                    serialPort.read(getReply, 17);
            
                    QByteArray ba(getReply, 17);
                    qDebug() << "reply from version: " << ba;
            
            }
            

            Where the debug "Reply from version: " always shows what I assume is the last data in the buffer.

            1 Reply Last reply
            0
            • Christian EhrlicherC Online
              Christian EhrlicherC Online
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Please use signals and slots when waiting for data - there is no guarantee that at the time you call read() some data is read so your reply[] array contains uninitialized data - you don't even check the return value of read()...
              There are a lot of thread on QSerialPort and signals and slots here and the documentation is also a good start

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

              A 1 Reply Last reply
              1
              • Christian EhrlicherC Christian Ehrlicher

                Please use signals and slots when waiting for data - there is no guarantee that at the time you call read() some data is read so your reply[] array contains uninitialized data - you don't even check the return value of read()...
                There are a lot of thread on QSerialPort and signals and slots here and the documentation is also a good start

                A Offline
                A Offline
                Aeroplane123
                wrote on last edited by Aeroplane123
                #7

                @Christian-Ehrlicher
                I have now added

                connect(serial, SIGNAL(readyRead()), this, SLOT(serialReceived()));
                
                
                void Widget::serialReceived(){
                
                qDebug() << "serial received";
                
                }
                

                THe quantity of serial received appears to change just about every time. Sometimes I will get just 1 line of serial received, other times I might get 5 or 6.

                Christian EhrlicherC 1 Reply Last reply
                0
                • Kent-DorfmanK Offline
                  Kent-DorfmanK Offline
                  Kent-Dorfman
                  wrote on last edited by
                  #8

                  not checking the return value of read() was my big point of contention. if you pass a data buffer array to read() and nothing is returned then I'd quess the previous data will still be in it.

                  A 1 Reply Last reply
                  0
                  • A Aeroplane123

                    @Christian-Ehrlicher
                    I have now added

                    connect(serial, SIGNAL(readyRead()), this, SLOT(serialReceived()));
                    
                    
                    void Widget::serialReceived(){
                    
                    qDebug() << "serial received";
                    
                    }
                    

                    THe quantity of serial received appears to change just about every time. Sometimes I will get just 1 line of serial received, other times I might get 5 or 6.

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

                    @Aeroplane123 said in serialPort.read() is reading old data:

                    THe quantity of serial received appears to change just about every time. Sometimes I will get just 1 line of serial received, other times I might get 5 or 6.

                    That's fine - you have to make sure you handle it correctly.

                    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
                    0
                    • Kent-DorfmanK Kent-Dorfman

                      not checking the return value of read() was my big point of contention. if you pass a data buffer array to read() and nothing is returned then I'd quess the previous data will still be in it.

                      A Offline
                      A Offline
                      Aeroplane123
                      wrote on last edited by
                      #10

                      @Kent-Dorfman Thanks for the reply. What would be your recommendation on how to check this?

                      Christian EhrlicherC 1 Reply Last reply
                      0
                      • A Aeroplane123

                        @Kent-Dorfman Thanks for the reply. What would be your recommendation on how to check this?

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

                        @Aeroplane123 said in serialPort.read() is reading old data:

                        What would be your recommendation on how to check this?

                        Simply check the return value would be a good start. Also looking at QIODevice::bytesAvailable() should help not reading out data when not enough is in the buffers.

                        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
                        0
                        • A Offline
                          A Offline
                          Aeroplane123
                          wrote on last edited by
                          #12

                          When I do

                          qDebug() << "Bytes available: " << serial->bytesAvailable();
                          
                          I get "Bytes available: " 1 even when my device is unplugged.
                          
                          1 Reply Last reply
                          0
                          • A Offline
                            A Offline
                            Aeroplane123
                            wrote on last edited by
                            #13

                            I have confirmed that every time I write the data is the same being sent (with an oscilloscope) and every time I read, the data received is the same. So on the first read, the program is clearly showing me some other data that is stored in a buffer, how can I clear the buffer first and just put the received data into the buffer?

                            Christian EhrlicherC 1 Reply Last reply
                            0
                            • A Aeroplane123

                              I have confirmed that every time I write the data is the same being sent (with an oscilloscope) and every time I read, the data received is the same. So on the first read, the program is clearly showing me some other data that is stored in a buffer, how can I clear the buffer first and just put the received data into the buffer?

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

                              @Aeroplane123 said in serialPort.read() is reading old data:

                              a buffer, how can I clear the buffer first and just put the received data into the buffer?

                              If so how do I clear the buffer before reading?
                              

                              You can simply read all data after you opened the device but don't think this will solve your problem - you have to find the start of your data by yourself.

                              You still want a dedicated protocol to clearly distinguish the single commands/data from each other.

                              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
                              0
                              • A Offline
                                A Offline
                                Aeroplane123
                                wrote on last edited by Aeroplane123
                                #15
                                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