Results 1 to 2 of 2

Thread: problem reading the serial port

  1. #1
    Join Date
    Jun 2018
    Posts
    31
    Thanks
    8
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default problem reading the serial port

    Hi, I have a problem with obtaining data through a serial port of an anemometer. The reading data is sent in three parts, that is, "\ x02" "4", "208110000026", "5 \ r" the number of elements in each QByteArray is variable. The first QbyteArray always has as the first element \ x02 "and the third QbyteArray always has the last element \ r. According to the manual, it should have for each serial port read a string as follows" \ x02 "" 42081100000265 \ r " I want to rebuild the string looking for the beginning of text "\ x02" and the end of text "\ r" but when reading the first element of the QBytearray that is "\ x02" I get an empty element. How can I find the "\ x02 "and" \ r "to be able to rebuild in a Qbytearray (" \ x02 "" 42081100000265 \ r ") and process the data in order to obtain the reading of the device?
    Thanks for help me


    void Widget::readSerialPort()
    {
    qDebug() << "Serial port read start";
    QByteArray data=anemometerSerialPort->readAll();
    qDebug() << data;//first reading ("\x02""4") , second reading"208110000026", third reading ("5\r").
    qDebug() << data.at(0);//first reading ( ) , second reading (2), third reading (5).
    qDebug() << "End of serial port reading";
    repairDataString(data);
    }

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,593
    Thanks
    13
    Thanked 1,587 Times in 1,516 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: problem reading the serial port

    The anemometer may send data that is three separate logical chunks, but what arrives at the other end (your end) is just a stream of bytes. You might receive each logical chunk all at once (as your code assumes), you might receive the entire packet all at once (possibly if the packets are short and far between), but in general you will get a few bytes at a time without regard for completeness or logical meaning.

    If you want to read the stream of bytes and break that into several components then that is entirely up to your code to do.

    A complete data structure seems to be this:
    1. Two bytes "\x024" (hex 02 34)
    2. A collection of ASCII characters of unknown length
    3. Two bytes ''5\r" (hex 35 0d)

    So, your code needs to read bytes as they arrive and add them to a buffer. When the bytes read includes a CR you have a potentially complete packet (or packets) in the buffer to process. You should process the buffer looking for the '\x02' (STX) and matching \r (CR), deal with those bytes, remove them from the buffer, and keep looking and dealing with for more complete packets, before returning to accumulating received bytes.

    You would use the serial port readyRead() signal and a slot to accumulate bytes into a buffer (member variable) and look for CR, and another function to process a buffer for all complete packets when the CR is seen. The logic looks very like the same thing for a UDP socket, so those examples would be helpful.

Similar Threads

  1. Replies: 5
    Last Post: 7th October 2015, 20:56
  2. Replies: 2
    Last Post: 15th March 2014, 11:54
  3. Reading/writing a serial port through USB
    By jvwlong in forum Newbie
    Replies: 2
    Last Post: 28th June 2012, 11:09
  4. Serial Port Reading problem
    By sfabel in forum Qt Programming
    Replies: 12
    Last Post: 18th February 2010, 14:59
  5. Replies: 1
    Last Post: 1st July 2009, 00:37

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.