Results 1 to 12 of 12

Thread: Serial read misses to read data from the serial port

  1. #1
    Join Date
    Nov 2012
    Location
    Coimbatore
    Posts
    53
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Serial read misses to read data from the serial port

    When I try to read data from the serial port I use to get the available bytes in the port as 10 or 20 as per the reception but when I used readData function to capture the data and character buffer to store the received data I use to get only 2 bytes or 2 characters

    char arr_cReadbuffer[1024];
    int nBytesAvailable = m_serialportCoProcessor->bytesAvailable();
    qDebug()<<"\r\n available bytes: "<<nBytesAvailable;
    if ( nBytesAvailable > 0 ) {
    /* read the available bytes */
    int nBytesReceived = m_serialportCoProcessor->readData(arr_cReadbuffer,nBytesAvailable);
    qDebug()<<"\r\n Bytes Received"<<nBytesReceived<<arr_cReadbuffer;




    "available bytes: 9"

    Bytes Received 9 ^MK1

    Only 4 characters available at the time of reading

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Serial read misses to read data from the serial port

    Your output states that you read 9 bytes, i.e. nBytesReceived is 9.

    How do you get the impression there are only 4?

    Cheers,
    _

  3. #3
    Join Date
    Nov 2012
    Location
    Coimbatore
    Posts
    53
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Serial read misses to read data from the serial port

    I am storing the read data in a character buffer which is ^MK1 which means only 4 byte is recieved

  4. #4
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,499
    Thanked 277 Times in 272 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Serial read misses to read data from the serial port

    Show real code especially the place where you notice that you have received only 4 bytes.

  5. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Serial read misses to read data from the serial port

    Maybe just use QIODevice::readAll()?

    It will allocate the buffer as needed, so its length will be exactly the size of its content.

    Cheers,
    _

  6. #6
    Join Date
    Nov 2012
    Location
    Coimbatore
    Posts
    53
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Serial read misses to read data from the serial port

    I mean the data stored in arr_cReadbuffer is ^MK1 instead of the nBytesReceived is 9

  7. #7
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Serial read misses to read data from the serial port

    Quote Originally Posted by mania View Post
    I mean the data stored in arr_cReadbuffer is ^MK1 instead of the nBytesReceived is 9
    How did you determine that your code snippet doesn't show.

    Again, using readAll() will give you both the check and the data in one go

    Qt Code:
    1. const QByteArray data = m_serialportCoProcessor->readAll();
    2. // check if there was data available
    3. if (data.count() > 0){
    4. // do something with the data
    5. qDebug() << "received" << data.count() << "bytes";
    6. }
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  8. #8
    Join Date
    Oct 2013
    Posts
    41
    Thanks
    1
    Thanked 8 Times in 7 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Serial read misses to read data from the serial port

    Quote Originally Posted by mania View Post
    I mean the data stored in arr_cReadbuffer is ^MK1 instead of the nBytesReceived is 9
    If i were to take a stab at the dark based on the information you provided, I would guess that the fifth byte is a null character and you are reading the bytes as a string. Try printing the bytes 1 at a time in a loop rather than as a string.

  9. #9
    Join Date
    Nov 2012
    Location
    Coimbatore
    Posts
    53
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Serial read misses to read data from the serial port

    anda_skoa:I read the data using readall which returns 0 bytes as the return value

    nBytesReceived1=m_serialportCoProcessor->readAll();
    qDebug()<<"rec1"<<nBytesReceived1.count();

    Quote Originally Posted by anda_skoa View Post
    How did you determine that your code snippet doesn't show.

    Again, using readAll() will give you both the check and the data in one go

    Qt Code:
    1. const QByteArray data = m_serialportCoProcessor->readAll();
    2. // check if there was data available
    3. if (data.count() > 0){
    4. // do something with the data
    5. qDebug() << "received" << data.count() << "bytes";
    6. }
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  10. #10
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Serial read misses to read data from the serial port

    Sure, if there is no data available, then nothing can be read.
    The is like your bytesAvailable() check.

    My suggestion was addressing the case when data is available.

    Anyway, it might be helpful to see how you determine that you got less data than you expected.
    All your snippet has so far is a log output that treats the data as an array of text characters.

    Cheers,
    _

  11. #11
    Join Date
    Nov 2012
    Location
    Coimbatore
    Posts
    53
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Serial read misses to read data from the serial port

    But still while I use read data I am getting it as 9 bytes received

  12. #12
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,499
    Thanked 277 Times in 272 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Serial read misses to read data from the serial port

    Show all of the code (method) performing the reading. Remember that the serial port does not know anything about the logic of your data (eg. That the package is 10 bytes).
    P.S.
    Read this thread.

Similar Threads

  1. read data from serial port and display it
    By vanduongbk in forum Newbie
    Replies: 1
    Last Post: 30th June 2013, 03:46
  2. Replies: 4
    Last Post: 2nd June 2012, 07:04
  3. read data from serial port
    By amitpatel22 in forum Qt for Embedded and Mobile
    Replies: 2
    Last Post: 20th July 2011, 17:11
  4. Replies: 4
    Last Post: 10th July 2010, 17:34
  5. How to write bytes read from serial port to a QFile
    By shamik in forum Qt Programming
    Replies: 19
    Last Post: 25th June 2007, 14:12

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.