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. I miss some bytes from Serial Port using Qt
QtWS25 Last Chance

I miss some bytes from Serial Port using Qt

Scheduled Pinned Locked Moved General and Desktop
qserialportarduinoreadyreadthreading
9 Posts 3 Posters 5.0k 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.
  • L Offline
    L Offline
    LostInSpace
    wrote on last edited by
    #1

    Dear Sirs
    I have a problem with a GUI that I implemented to get a stream of byte from Arduino Uno using Serial Com

    I transmit every 2 seconds a packet made by 20 byte from Arduino to the Serial Port of my laptop

    I used the SIGNAL/SLOT method in order to get every single byte every time there's a new byte pending on Serial Port.
    I would like to read each byte then pass it to another function and process is.
    I just got every time 5 bytes, then stop and I'm no more able to process the rest of them

    Could you suggest me any design strategy in order to avoid this problem

    Best Regards

    1 Reply Last reply
    0
    • HamedH Offline
      HamedH Offline
      Hamed
      wrote on last edited by
      #2

      Hi
      Can you post the part of your code that you read bytes from serial port?

      HamedBabaeyan@yahoo.com
      for more interesting stuff

      1 Reply Last reply
      0
      • L Offline
        L Offline
        LostInSpace
        wrote on last edited by
        #3

        Of Course:

        void MainWindow::readData()
        {
        char inchar = 0X00;

        qint64 read = serial->read(&inchar,1);
        uchar b = static_cast<uchar>(inchar);
        processing(b); // This is the methods that will process the UNSIGNED CHAR  b
        //qDebug() << inchar << endl;
        

        }

        I post also the code in the constructor where I connect SIGNAL TO SLOT

        connect(serial, SIGNAL(readyRead()), this, SLOT(readData()));
        // That's in order to open the function readData() every time I have a new byte incoming from serial
        connect(this,SIGNAL(processing(uchar)),downlink,SLOT(tlmStateMachine(uchar)));
        // That's is order to connect the methods "processing" of THIS object to the method "tlmStateMachine" of DOWNLINK object

        That's All

        1 Reply Last reply
        0
        • HamedH Offline
          HamedH Offline
          Hamed
          wrote on last edited by Hamed
          #4

          there is a issue here.
          you used readyRead() signal which will emit every time you have data ready on serial port.
          then if your for example 5 bytes arrives together then it will emit only one time!
          so you just read one single byte and your signal came only one time, so it means you only can see that one byte! you need to use readAll() method in order to get all of the available bytes.
          if you don't want to use readAll() method you should know your serial port device baud rate and set a timer to get data from it every time a data is ready!

          HamedBabaeyan@yahoo.com
          for more interesting stuff

          L 1 Reply Last reply
          0
          • HamedH Hamed

            there is a issue here.
            you used readyRead() signal which will emit every time you have data ready on serial port.
            then if your for example 5 bytes arrives together then it will emit only one time!
            so you just read one single byte and your signal came only one time, so it means you only can see that one byte! you need to use readAll() method in order to get all of the available bytes.
            if you don't want to use readAll() method you should know your serial port device baud rate and set a timer to get data from it every time a data is ready!

            L Offline
            L Offline
            LostInSpace
            wrote on last edited by
            #5

            @Hamed Ok I got it
            In this way I'm not able to process byte-by-byte but I ll get a QByteArray and I have to process it using a Thread

            Is it correct?

            L 1 Reply Last reply
            0
            • L LostInSpace

              @Hamed Ok I got it
              In this way I'm not able to process byte-by-byte but I ll get a QByteArray and I have to process it using a Thread

              Is it correct?

              L Offline
              L Offline
              LostInSpace
              wrote on last edited by
              #6

              @LostInSpace I would like something in order to process byte by byte

              1 Reply Last reply
              0
              • HamedH Offline
                HamedH Offline
                Hamed
                wrote on last edited by Hamed
                #7

                No, don't use thread for reading from serial port.
                you have two way to have your bytes :
                1- use readAll() and store your data in QbyteArray and read by byte from it
                2- use timer with for example 1ms refresh time to get your data byte by byte

                HamedBabaeyan@yahoo.com
                for more interesting stuff

                JKSHJ 1 Reply Last reply
                0
                • HamedH Hamed

                  No, don't use thread for reading from serial port.
                  you have two way to have your bytes :
                  1- use readAll() and store your data in QbyteArray and read by byte from it
                  2- use timer with for example 1ms refresh time to get your data byte by byte

                  JKSHJ Offline
                  JKSHJ Offline
                  JKSH
                  Moderators
                  wrote on last edited by
                  #8

                  @Hamed said:

                  No, don't use thread for reading from serial port.
                  you have two way to have your bytes :
                  1- use readAll() and store your data in QbyteArray and read by byte from it
                  2- use timer with for example 1ms refresh time to get your data byte by byte

                  I recommend option #1.

                  Option #2 is very wasteful of CPU resources. Also, Windows laptops don't provide good support for 1 ms timers.

                  Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                  1 Reply Last reply
                  1
                  • HamedH Offline
                    HamedH Offline
                    Hamed
                    wrote on last edited by Hamed
                    #9

                    #2 is wasteful way indeed!
                    but in low baud rates it will help, but in this case I don't think this is a low baud rate.

                    EDIT : I said for example 1ms, you really don't need it this fast, it depends on the frequency of getting bytes, for example for 20Hz you will have 20bytes every second which means you need a timer with at least 50ms refresh time and of course you read from serial port in asynchronous way then you can't 100% trust this way to read byte by byte, So as @JKSH said you better don't use this way if you don't have a specific reason.

                    HamedBabaeyan@yahoo.com
                    for more interesting stuff

                    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