Reading from Arduino using QSerialPort stops after a few bytes



  • Hi,

    I am trying to receive bytes from an Arduino's (emulated USB) serial port using QSerialPort.

    Currently I simply send an incrementing / overflowing single byte on the arduno side:

    void loop() {
    static unsigned char cnt = 0;
    cnt++;
    Serial.write(&cnt, 1);
    }

    When I connect to the emulated COM-port using putty, I receive an endless stream of bytes, as expected.

    However, when I try to achieve the same using QSerialPort, the Arduino sends for a short time (TX led is active), I receive a batch of bytes in one go but then the TX led stays dark and I don't receive any more data.

    port.setPortName("COM23");
    if (!port.open(QIODevice::ReadWrite)) {
    printf("Error opening serial: %d\n", port.error());
    }

    if(!port.setParity(QSerialPort::NoParity) ||
        !port.setStopBits(QSerialPort::OneStop) ||
        !port.setDataBits(QSerialPort::Data8) ||
        !port.setFlowControl(QSerialPort::NoFlowControl ) ||
        !port.setBaudRate(115200))
     {
        qFatal("Unable to configure serial port");
        exit(1);
     }
    
    char ackStatus = 0;
    while(1) {
        int bytesRead = port.read(&ackStatus, 1);
    
        if(bytesRead > 0) {
          printf("Data received, length: %d, val: %d\n", bytesRead, ackStatus);
          fflush(stdout);
        }else {
          qApp->processEvents(QEventLoop::ExcludeUserInputEvents);
        }
    }
    

    Any idea what the difference between putty and QSerialPort might be in this case?

    Thank you in advance, Clemens


  • Qt Champions 2016

    Hi could you try the "Terminal sample"
    and see if it has same behavior?



  • I figured out, it was nescessary to set DTR:

    port.setDataTerminalReady(true);

    Thanks & br, Clemens


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.