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 Arduino using QSerialPort stops after a few bytes

Reading from Arduino using QSerialPort stops after a few bytes

Scheduled Pinned Locked Moved Unsolved General and Desktop
qserialportarduinowindows
3 Posts 2 Posters 2.0k 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
    FlyingDude
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

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

      1 Reply Last reply
      1
      • F Offline
        F Offline
        FlyingDude
        wrote on last edited by
        #3

        I figured out, it was nescessary to set DTR:

        port.setDataTerminalReady(true);

        Thanks & br, Clemens

        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