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. QSerialPort reads only one byte

QSerialPort reads only one byte

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

    I connect TX/Rx contacts together. Then I send **IDN?*. My oscilloscop shows that all of 5 bytes are sent. But readAll returns a QByteArray with only first byte.

    #include <QCoreApplication>
    #include <QtSerialPort>
    
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
    
        printf("initialization...\n"); //debug
        QSerialPort serial;
        serial.setPortName("COM2");
        serial.setBaudRate(QSerialPort::Baud9600);
        serial.setDataBits(QSerialPort::Data8);
        serial.setParity(QSerialPort::NoParity);
        serial.setStopBits(QSerialPort::OneStop);
        serial.setFlowControl(QSerialPort::NoFlowControl);
        serial.open(QSerialPort::ReadWrite);
    
        QString sendData("*IDN?");
        printf("send data:    "); //debug
        printf(sendData.toUtf8()); //debug
        serial.write(sendData.toUtf8());
        serial.waitForBytesWritten(1000);
        serial.waitForReadyRead(1000);
        QByteArray receiveData = serial.readAll();
        printf("\nreceive data: "); //debug
        printf(receiveData.data()); //debug
        printf("\nreceive byts: "); //debug
        printf("%d\n", receiveData.size()); //debug
        serial.close();
        printf("end"); //debug
    
        return a.exec();
    }
    

    I use a USB-UART demoboard PL2302 from the WaveShare with PL2303TA IC. My enviornment is Windows 7 and Qt 5.10.0. Also I have test this code on a UAS-DB9M-02 USB-UART cable from the Gembird with the same behavior. Examples from LabVIEW works fine with PL2303. So I think it's a Qt problem.

    What am I doing wrong?

    jsulmJ Ketan__Patel__0011K 2 Replies Last reply
    0
    • F flammmable

      I connect TX/Rx contacts together. Then I send **IDN?*. My oscilloscop shows that all of 5 bytes are sent. But readAll returns a QByteArray with only first byte.

      #include <QCoreApplication>
      #include <QtSerialPort>
      
      
      int main(int argc, char *argv[])
      {
          QCoreApplication a(argc, argv);
      
          printf("initialization...\n"); //debug
          QSerialPort serial;
          serial.setPortName("COM2");
          serial.setBaudRate(QSerialPort::Baud9600);
          serial.setDataBits(QSerialPort::Data8);
          serial.setParity(QSerialPort::NoParity);
          serial.setStopBits(QSerialPort::OneStop);
          serial.setFlowControl(QSerialPort::NoFlowControl);
          serial.open(QSerialPort::ReadWrite);
      
          QString sendData("*IDN?");
          printf("send data:    "); //debug
          printf(sendData.toUtf8()); //debug
          serial.write(sendData.toUtf8());
          serial.waitForBytesWritten(1000);
          serial.waitForReadyRead(1000);
          QByteArray receiveData = serial.readAll();
          printf("\nreceive data: "); //debug
          printf(receiveData.data()); //debug
          printf("\nreceive byts: "); //debug
          printf("%d\n", receiveData.size()); //debug
          serial.close();
          printf("end"); //debug
      
          return a.exec();
      }
      

      I use a USB-UART demoboard PL2302 from the WaveShare with PL2303TA IC. My enviornment is Windows 7 and Qt 5.10.0. Also I have test this code on a UAS-DB9M-02 USB-UART cable from the Gembird with the same behavior. Examples from LabVIEW works fine with PL2303. So I think it's a Qt problem.

      What am I doing wrong?

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

      @flammmable said in QSerialPort reads only one byte:

      readAll

      You should not expect that it delivers you everything at once. You should rather connect a slot to readyRead signal, call readAll in that slot and accumulate the data in a buffer until you got everything.

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

      1 Reply Last reply
      4
      • F flammmable

        I connect TX/Rx contacts together. Then I send **IDN?*. My oscilloscop shows that all of 5 bytes are sent. But readAll returns a QByteArray with only first byte.

        #include <QCoreApplication>
        #include <QtSerialPort>
        
        
        int main(int argc, char *argv[])
        {
            QCoreApplication a(argc, argv);
        
            printf("initialization...\n"); //debug
            QSerialPort serial;
            serial.setPortName("COM2");
            serial.setBaudRate(QSerialPort::Baud9600);
            serial.setDataBits(QSerialPort::Data8);
            serial.setParity(QSerialPort::NoParity);
            serial.setStopBits(QSerialPort::OneStop);
            serial.setFlowControl(QSerialPort::NoFlowControl);
            serial.open(QSerialPort::ReadWrite);
        
            QString sendData("*IDN?");
            printf("send data:    "); //debug
            printf(sendData.toUtf8()); //debug
            serial.write(sendData.toUtf8());
            serial.waitForBytesWritten(1000);
            serial.waitForReadyRead(1000);
            QByteArray receiveData = serial.readAll();
            printf("\nreceive data: "); //debug
            printf(receiveData.data()); //debug
            printf("\nreceive byts: "); //debug
            printf("%d\n", receiveData.size()); //debug
            serial.close();
            printf("end"); //debug
        
            return a.exec();
        }
        

        I use a USB-UART demoboard PL2302 from the WaveShare with PL2303TA IC. My enviornment is Windows 7 and Qt 5.10.0. Also I have test this code on a UAS-DB9M-02 USB-UART cable from the Gembird with the same behavior. Examples from LabVIEW works fine with PL2303. So I think it's a Qt problem.

        What am I doing wrong?

        Ketan__Patel__0011K Offline
        Ketan__Patel__0011K Offline
        Ketan__Patel__0011
        wrote on last edited by
        #3
        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