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 send each byte with delay
Forum Updated to NodeBB v4.3 + New Features

SerialPort send each byte with delay

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 5 Posters 1.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.
  • S Offline
    S Offline
    Stefanoxjx
    wrote on last edited by
    #1

    Hi, I need an advice.
    I need to send some bytes by serialport, but with 5ms of delay between each byte sent.
    I tried this solution, but obviously don't work:

    QByteArray s = "1234567980";
    for(int i = 0; i  < s.lenght(); <++)
    {
        SerialPort.write(s[i], 1);
        QThread::msleep(5);
    }
    

    I haven't ideas.
    Can you help me?
    Thanks.

    jsulmJ KroMignonK 2 Replies Last reply
    0
    • S Stefanoxjx

      Hi, I need an advice.
      I need to send some bytes by serialport, but with 5ms of delay between each byte sent.
      I tried this solution, but obviously don't work:

      QByteArray s = "1234567980";
      for(int i = 0; i  < s.lenght(); <++)
      {
          SerialPort.write(s[i], 1);
          QThread::msleep(5);
      }
      

      I haven't ideas.
      Can you help me?
      Thanks.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Stefanoxjx said in SerialPort send each byte with delay:

      QThread::msleep(5);

      Never do this in event driven applications!
      Use a QTimer with 5ms timeout and send inside the slot connected to the timer.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

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

        Hi,

        You can use a QTimer.

        Out of curiosity, why do you need that delay between two writes ?

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

        1 Reply Last reply
        2
        • S Stefanoxjx

          Hi, I need an advice.
          I need to send some bytes by serialport, but with 5ms of delay between each byte sent.
          I tried this solution, but obviously don't work:

          QByteArray s = "1234567980";
          for(int i = 0; i  < s.lenght(); <++)
          {
              SerialPort.write(s[i], 1);
              QThread::msleep(5);
          }
          

          I haven't ideas.
          Can you help me?
          Thanks.

          KroMignonK Offline
          KroMignonK Offline
          KroMignon
          wrote on last edited by KroMignon
          #4

          @Stefanoxjx This cannot work, because you are locking the main thread, you have to give QSerialPort "time to work".

          QByteArray s = "1234567980";
          for(int i = 0; i  < s.lenght(); <++)
          {
              SerialPort.write(s[i], 1);
              if(!SerialPort.waitForBytesWritten())
                  qDebug() << "Failed to write on serial port!";
              QThread::msleep(5);
          }
          

          take a look at documentation ==> https://doc.qt.io/qt-5/qserialport.html#waitForBytesWritten

          It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

          1 Reply Last reply
          1
          • S Offline
            S Offline
            Stefanoxjx
            wrote on last edited by Stefanoxjx
            #5

            @SGaist: I must send command strings to an external device, but for sure it has a bad management of the serial port, because without delay it losts many bytes.

            Thanks at all for help (I hoped there was a more direct solution then QTimer)

            KroMignonK 1 Reply Last reply
            0
            • S Stefanoxjx

              @SGaist: I must send command strings to an external device, but for sure it has a bad management of the serial port, because without delay it losts many bytes.

              Thanks at all for help (I hoped there was a more direct solution then QTimer)

              KroMignonK Offline
              KroMignonK Offline
              KroMignon
              wrote on last edited by KroMignon
              #6

              @Stefanoxjx said in SerialPort send each byte with delay:

              I hoped there was a more direct solution then QTimer

              QTimer is not required in this case, you can do it in "polling mode". But you have to call QSerialPort::waitForBytesWritten() to give QSerialPort time to send the data.
              Calling QSerialPort::write() will only put data into the output buffer, not sending the data on serial port.
              Or you have to do it asynchronously, with a QTimer.

              Booth solutions are valid, it is up to you.

              It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

              1 Reply Last reply
              0
              • BuckwheatB Offline
                BuckwheatB Offline
                Buckwheat
                wrote on last edited by
                #7

                Hi @Stefanoxjx

                Like @SGaist, I believe msleep and usleep are very bad ideas. The waitForBytesWritten can work but is not as flexible in the event driven system. I like using a single-shot QTimer (0 for the time) to run a state machine in my apps when sending a command. The next state is initiated by the reply. I also keep a no-data QTimer with a 1.5sec timeout for when the reply is delayed to resend the command.
                This really works fast, is reliable, scalable, and ports well to its own thread or in the main thread.

                Dave Fileccia

                1 Reply Last reply
                1
                • S Offline
                  S Offline
                  Stefanoxjx
                  wrote on last edited by
                  #8

                  I tryed WaitForBytesWritten, but doesn't work.
                  QTimer solve the problem very well :)

                  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