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. Reading from serial port
Forum Updated to NodeBB v4.3 + New Features

Reading from serial port

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 361 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.
  • G Offline
    G Offline
    gogoer
    wrote on last edited by
    #1

    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
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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
      1
      • G Offline
        G Offline
        gogoer
        wrote on last edited by gogoer
        #3

        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.

        eyllanescE 1 Reply Last reply
        0
        • G 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.

          eyllanescE Offline
          eyllanescE Offline
          eyllanesc
          wrote on last edited by eyllanesc
          #4

          @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
          1

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved