Reading Data from Serial Port.
-
Hello Everyone,
I need help regarding one solution. I have created an application and I am reading data from Serial Port. The data comes in pack of 8 bytes ( 5A 00 01 02 03 04 05 5F ). Start and End bytes fixed (5A and 5F). I am using the below code to read data:
if (_serialPort->open(QSerialPort::ReadOnly))
{
QObject::connect(_serialPort, &QSerialPort::readyRead, this, &SerialPort::dataReady);
}QByteArray receivedData; if (_serialPort->isOpen()) { receivedData.append(_serialPort->readAll()); emit dataReceived(receivedData.toHex(' ')); }
Read the data and send it to another class. Application will be working fine till the data from serial port read is 8 bytes. When data is recieved less than or more than 8 bytes, the application crashes.
Data is coming in 8 bytes at rate of 40 ms. I want to read only 8 bytes of data at a time and buffer should not keep on filling with the data.
Any help will be appreciated.
Thanks..!!
-
Hello Everyone,
I need help regarding one solution. I have created an application and I am reading data from Serial Port. The data comes in pack of 8 bytes ( 5A 00 01 02 03 04 05 5F ). Start and End bytes fixed (5A and 5F). I am using the below code to read data:
if (_serialPort->open(QSerialPort::ReadOnly))
{
QObject::connect(_serialPort, &QSerialPort::readyRead, this, &SerialPort::dataReady);
}QByteArray receivedData; if (_serialPort->isOpen()) { receivedData.append(_serialPort->readAll()); emit dataReceived(receivedData.toHex(' ')); }
Read the data and send it to another class. Application will be working fine till the data from serial port read is 8 bytes. When data is recieved less than or more than 8 bytes, the application crashes.
Data is coming in 8 bytes at rate of 40 ms. I want to read only 8 bytes of data at a time and buffer should not keep on filling with the data.
Any help will be appreciated.
Thanks..!!
@mbatra said in Reading Data from Serial Port.:
Any help will be appreciated.
Use the readyRead() signal and read the data in the connected slot. Since a serial port is stream-based you have to fiddle out by yourself where / when the 8 bytes are available.
The forum search can also help here since this is asked many times..