Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Unsolved Reading from serial port

    General and Desktop
    3
    4
    78
    Loading More Posts
    • 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.
    • G
      gogoer last edited by

      Hello.
      I use next code to communicate with some hadware by UART.
      I send commands:

      void controller::sendCommand(QString command)
      {
          serialPort.write(command.toUtf8());
          serialPort.waitForBytesWritten(100);
      }
      

      and controller returns answer asynchronously. I process it next way:

      connect(&serialPort, &QSerialPort::readyRead, this,  &controller::processReadyRead);
      
      void controller::processReadyRead()
      {
          QByteArray messageData;
          while (serialPort.waitForReadyRead(100)) {
              messageData += serialPort.readAll();
          }
      
         // process data
      
      }
      

      for example, for success controller returns "OK!" string.
      But answer comes in strange way: sometemes i got "OK!", sometemes i got "" (empty string) and after next command got "OK!OK!" (answer for this and prev command).
      what is right way to get answers correctly (one for each command)?

      1 Reply Last reply Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        Hi,

        Do not mix the blocking and asynchronous APIs. Send your command and once you got the full "OK!" frame, send the following command.

        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 Reply Quote 1
        • G
          gogoer last edited by gogoer

          Do not mix the blocking and asynchronous APIs.

          can you explain, please?

          Send your command and once you got the full "OK!" frame, send the following command.

          getting some responses in one message is not a problem, i use number to mark commands and answers. But can you explain why it do such way?
          And why i get empty responses? its a problem.
          One more thing: sometimes controller generate status messages and sends it to me without command from my side.

          eyllanesc 1 Reply Last reply Reply Quote 0
          • eyllanesc
            eyllanesc @gogoer last edited by eyllanesc

            @gogoer In simple words: Don't use the waitForXXX methods as they block the eventloop and hence the signals. Instead of using those methods then just use the signals. It also eliminates the whiles since if they run for a long time they also block the eventloop. Also provide a minimal and reproducible example.

            If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

            1 Reply Last reply Reply Quote 1
            • First post
              Last post