Receive Data on Serial Port C++ Windows.
-
Hi,
I have to develop an application where I have to read data from serial (COM) port. Data is coming in the form of a structure where there is StartByte and EndByte. I have to implement the following:
Read data continuously from serial port and search for StartByte (5A), then store the next 10 bytes in an array and the 12th byte is the EndByte. This should be done continuously. Again repeat the same process. if the endbyte is not received on 12th byte, then discard the data and again search for the start byte and endbyte. Data is coming continuously on the serial port.Any help would be appreciated.
Thanks.
-
Hi,
I have to develop an application where I have to read data from serial (COM) port. Data is coming in the form of a structure where there is StartByte and EndByte. I have to implement the following:
Read data continuously from serial port and search for StartByte (5A), then store the next 10 bytes in an array and the 12th byte is the EndByte. This should be done continuously. Again repeat the same process. if the endbyte is not received on 12th byte, then discard the data and again search for the start byte and endbyte. Data is coming continuously on the serial port.Any help would be appreciated.
Thanks.
@mbatra Start here: https://doc.qt.io/qt-6/qserialport.html
Examples: https://doc.qt.io/qt-6/qtserialport-examples.html -
C Christian Ehrlicher moved this topic from C++ Gurus on
-
Hi,
I have to develop an application where I have to read data from serial (COM) port. Data is coming in the form of a structure where there is StartByte and EndByte. I have to implement the following:
Read data continuously from serial port and search for StartByte (5A), then store the next 10 bytes in an array and the 12th byte is the EndByte. This should be done continuously. Again repeat the same process. if the endbyte is not received on 12th byte, then discard the data and again search for the start byte and endbyte. Data is coming continuously on the serial port.Any help would be appreciated.
Thanks.
To go with the data in the QSerialPort documentation and examples, here is a sample program outline:
- Start with an empty byte array buffer. This buffer has to persist for the entire life of the serial connection.
- Each time you are told bytes have arrived:
- Read all available bytes and append them to the buffer
- If the buffer contains 12 or more bytes then it can possibly† contain a whole protocol data unit. Process all complete PDUs present and remove them from the buffer. There may be left-over bytes.
- Exit the receive handler
† It may contain the end of one and the start of another (both incomplete) or, less likely, contain random rubbish.