serialPort.read() is reading old data
-
I have a program where I am reading and writing to a serial port. The data that is being received appears to be old data. When I unplug the serial port connection from my device and do
QSerialPort serialPort;
serialPort.read(data);
QByteArray ba(data); qDebug() << "reply from device: " << ba;
Shows me the data that was sent from my device from BEFORE it was unplugged which is impossible, is it reading from a buffer? If so how do I clear the buffer before reading?
-
I have a program where I am reading and writing to a serial port. The data that is being received appears to be old data. When I unplug the serial port connection from my device and do
QSerialPort serialPort;
serialPort.read(data);
QByteArray ba(data); qDebug() << "reply from device: " << ba;
Shows me the data that was sent from my device from BEFORE it was unplugged which is impossible, is it reading from a buffer? If so how do I clear the buffer before reading?
@Aeroplane123 said in serialPort.read() is reading old data:
is it reading from a buffer?
Yes, the OS buffers the serial device
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.
-
this bothers me on a couple of levels. I'd expect that the open/close semantics should clear/clean any OS buffers.
OP isn't clear on whether they start/stop/rerun the program between iterations of unplugging the connections.
I'm wanting to know how "data" is declared. Smells to me like read(data) is just presenting what was in the data buffer prior.
No indication above whether they are checking the return value of QIODevice.read, since a return value of zero isn't going to touch the data buffer pointed to.
-
this bothers me on a couple of levels. I'd expect that the open/close semantics should clear/clean any OS buffers.
OP isn't clear on whether they start/stop/rerun the program between iterations of unplugging the connections.
I'm wanting to know how "data" is declared. Smells to me like read(data) is just presenting what was in the data buffer prior.
No indication above whether they are checking the return value of QIODevice.read, since a return value of zero isn't going to touch the data buffer pointed to.
@Kent-Dorfman said in serialPort.read() is reading old data:
I'd expect that the open/close semantics should clear/clean any OS buffers.
Yes, but I think the OP is unplugging the physical medium and expecting an open QSerialPort to intuit this and clear any already buffered data by magic.
OP isn't clear on whether they start/stop/rerun the program between iterations of unplugging the connections.
I think not.
I'm wanting to know how "data" is declared. Smells to me like read(data) is just presenting what was in the data buffer prior.
I do not know what type
data
could be. QIODevice::read() can take a character buffer and size, or a maximum size (returning a QByteArray), but not a bare buffer. I expect this is not the actual code. -
@Kent-Dorfman said in serialPort.read() is reading old data:
I'd expect that the open/close semantics should clear/clean any OS buffers.
Yes, but I think the OP is unplugging the physical medium and expecting an open QSerialPort to intuit this and clear any already buffered data by magic.
OP isn't clear on whether they start/stop/rerun the program between iterations of unplugging the connections.
I think not.
I'm wanting to know how "data" is declared. Smells to me like read(data) is just presenting what was in the data buffer prior.
I do not know what type
data
could be. QIODevice::read() can take a character buffer and size, or a maximum size (returning a QByteArray), but not a bare buffer. I expect this is not the actual code.@ChrisW67 You are correct data is not the code "data" in this case was more of pseudo-code. This is what is actually happening...
memcpy(cmd, "x", 4); cmd[4] = CMD_REQUEST_SYSTEM_INFO; cmd[5] = 0x00; cmd[6] = 0x00; cmd[7] = 0x00; cmd[8] = calcChecksum(cmd, 8); char* command = (char *) cmd; //cmd must be converted to char here serialPort.write(command, 9); bool writeSuccess = serialPort.write(command, 9) != -1; QByteArray ba(command, 9); qDebug() << "sent: " << ba; if (writeSuccess) { qDebug() << "Write Success!"; uint8_t reply[32]; char* getReply = (char *) reply; serialPort.read(getReply, 17); QByteArray ba(getReply, 17); qDebug() << "reply from version: " << ba; }
Where the debug "Reply from version: " always shows what I assume is the last data in the buffer.
-
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!