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 ignores new input, after reading a few bytes bytesAvailable() always returns 0
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved General and Desktop
qserialportbytesavailable
3 Posts 2 Posters 2.1k 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 koahnig
    #1

    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.

    1 Reply Last reply
    0
    • K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      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());
      }
      

      Vote the answer(s) that helped you to solve your issue(s)

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

        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

        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