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 2.1k 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.
  • Christian EhrlicherC Offline
    Christian EhrlicherC Offline
    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 Offline
          Christian EhrlicherC Offline
          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 Offline
              Christian EhrlicherC Offline
              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 Offline
                    Christian EhrlicherC Offline
                    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