QSerialPort ignores new input, after reading a few bytes bytesAvailable() always returns 0



  • Hi,

    I am trying to communicate with my Arduino-Board using the QSerialPort-Class. For now the ardunio simply sends an endless stream of '+'-Characters I would like to read, which works well when directly accessing the device files (linux) or when connecting to the serial port using minicom, however using QSerialPort I am only able to read the first 12-16 bytes.

    My code looks like:

    char ackStatus;
    int bytesRead;
    while(true) {
        bytesRead = port.read(&ackStatus, 1);
        printf("Status %d, bytes read %d , available %d\n", ackStatus, bytesRead, port.bytesAvailable());
    }
    

    And the typical output when running this code is:
    Status 43, bytes read 1 , available 12
    Status 43, bytes read 1 , available 11
    Status 43, bytes read 1 , available 10
    Status 43, bytes read 1 , available 9
    Status 43, bytes read 1 , available 8
    Status 43, bytes read 1 , available 7
    Status 43, bytes read 1 , available 6
    Status 43, bytes read 1 , available 5
    Status 43, bytes read 1 , available 4
    Status 43, bytes read 1 , available 3
    Status 43, bytes read 1 , available 2
    Status 43, bytes read 1 , available 1
    Status 43, bytes read 1 , available 0
    Status 0, bytes read 0 , available 0
    Status 0, bytes read 0 , available 0
    Status 0, bytes read 0 , available 0
    .......

    The strange thing is after restarting the application, I get the same result - 12-16 bytes are read successfully, but after that port.bytesAvailable() always returns 0 and reads fail. I verified the ardunio is still sending data with minicom, somehow QSerialPort doesn't notice the new input.

    Any idea what is going on here?

    Thank you in advance, Clemens

    PS: I've just recently started with QT, and it really seems like QT is the thing I always missed when writing C++: A well-designed and modern standard-libary.


  • Moderators

    Welcome to devnet

    Either you have not a very good stripped down code snippet or there is your problem.
    If this code is in the slot method called by signal readyRead, it never exits. IIRC this may keep your machine too busy for receiving more bytes. I am not sure right away, if the explanation is 100% correct.

    However, you may want to exit the infinite loop above when no more bytes are available. This will keep your CPU on full load even when you are receiving only a byte once in a while. You should receive another readyRead signal and you can read the next bytes.

    It should look more like:

    char ackStatus;
    int bytesRead;
    while ( port.bytesAvailable() ) {
        bytesRead = port.read(&ackStatus, 1);
        printf("Status %d, bytes read %d , available %d\n", ackStatus, bytesRead, port.bytesAvailable());
    }
    


  • Hi,

    Thanks for your reply.

    The problem was the code in snippit I pasted was executed in the Main/UI-Thread, which seems to receive new bytes asynchronously. So my infinite loop was not allowing the Main-Thread to execute the receive-handler and therefore bytesAvailable() always returned 0.
    I now call processEvents() within the loop, and things seem to work as expected :)

    Thanks & br, Clemens


Log in to reply
 

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