serialPort.read() is reading old data
-
Please use signals and slots when waiting for data - there is no guarantee that at the time you call read() some data is read so your reply[] array contains uninitialized data - you don't even check the return value of read()...
There are a lot of thread on QSerialPort and signals and slots here and the documentation is also a good start -
Please use signals and slots when waiting for data - there is no guarantee that at the time you call read() some data is read so your reply[] array contains uninitialized data - you don't even check the return value of read()...
There are a lot of thread on QSerialPort and signals and slots here and the documentation is also a good start@Christian-Ehrlicher
I have now addedconnect(serial, SIGNAL(readyRead()), this, SLOT(serialReceived()));
void Widget::serialReceived(){ qDebug() << "serial received"; }
THe quantity of serial received appears to change just about every time. Sometimes I will get just 1 line of serial received, other times I might get 5 or 6.
-
not checking the return value of read() was my big point of contention. if you pass a data buffer array to read() and nothing is returned then I'd quess the previous data will still be in it.
-
@Christian-Ehrlicher
I have now addedconnect(serial, SIGNAL(readyRead()), this, SLOT(serialReceived()));
void Widget::serialReceived(){ qDebug() << "serial received"; }
THe quantity of serial received appears to change just about every time. Sometimes I will get just 1 line of serial received, other times I might get 5 or 6.
@Aeroplane123 said in serialPort.read() is reading old data:
THe quantity of serial received appears to change just about every time. Sometimes I will get just 1 line of serial received, other times I might get 5 or 6.
That's fine - you have to make sure you handle it correctly.
-
not checking the return value of read() was my big point of contention. if you pass a data buffer array to read() and nothing is returned then I'd quess the previous data will still be in it.
@Kent-Dorfman Thanks for the reply. What would be your recommendation on how to check this?
-
@Kent-Dorfman Thanks for the reply. What would be your recommendation on how to check this?
@Aeroplane123 said in serialPort.read() is reading old data:
What would be your recommendation on how to check this?
Simply check the return value would be a good start. Also looking at QIODevice::bytesAvailable() should help not reading out data when not enough is in the buffers.
-
When I do
qDebug() << "Bytes available: " << serial->bytesAvailable(); I get "Bytes available: " 1 even when my device is unplugged.
-
I have confirmed that every time I write the data is the same being sent (with an oscilloscope) and every time I read, the data received is the same. So on the first read, the program is clearly showing me some other data that is stored in a buffer, how can I clear the buffer first and just put the received data into the buffer?
-
I have confirmed that every time I write the data is the same being sent (with an oscilloscope) and every time I read, the data received is the same. So on the first read, the program is clearly showing me some other data that is stored in a buffer, how can I clear the buffer first and just put the received data into the buffer?
@Aeroplane123 said in serialPort.read() is reading old data:
a buffer, how can I clear the buffer first and just put the received data into the buffer?
If so how do I clear the buffer before reading?
You can simply read all data after you opened the device but don't think this will solve your problem - you have to find the start of your data by yourself.
You still want a dedicated protocol to clearly distinguish the single commands/data from each other.
-
This post is deleted!