Serial read misses to read data from the serial port
When I try to read data from the serial port I use to get the available bytes in the port as 10 or 20 as per the reception but when I used readData function to capture the data and character buffer to store the received data I use to get only 2 bytes or 2 characters
char arr_cReadbuffer[1024];
int nBytesAvailable = m_serialportCoProcessor->bytesAvailable();
qDebug()<<"\r\n available bytes: "<<nBytesAvailable;
if ( nBytesAvailable > 0 ) {
/* read the available bytes */
int nBytesReceived = m_serialportCoProcessor->readData(arr_cReadbuffer,nBytesAvailable);
qDebug()<<"\r\n Bytes Received"<<nBytesReceived<<arr_cReadbuffer;
"available bytes: 9"
Bytes Received 9 ^MK1
Only 4 characters available at the time of reading
Re: Serial read misses to read data from the serial port
Your output states that you read 9 bytes, i.e. nBytesReceived is 9.
How do you get the impression there are only 4?
Cheers,
_
Re: Serial read misses to read data from the serial port
I am storing the read data in a character buffer which is ^MK1 which means only 4 byte is recieved
Re: Serial read misses to read data from the serial port
Show real code especially the place where you notice that you have received only 4 bytes.
Re: Serial read misses to read data from the serial port
Maybe just use QIODevice::readAll()?
It will allocate the buffer as needed, so its length will be exactly the size of its content.
Cheers,
_
Re: Serial read misses to read data from the serial port
I mean the data stored in arr_cReadbuffer is ^MK1 instead of the nBytesReceived is 9
Re: Serial read misses to read data from the serial port
Quote:
Originally Posted by
mania
I mean the data stored in arr_cReadbuffer is ^MK1 instead of the nBytesReceived is 9
How did you determine that your code snippet doesn't show.
Again, using readAll() will give you both the check and the data in one go
Code:
const QByteArray data
= m_serialportCoProcessor
->readAll
();
// check if there was data available
if (data.count() > 0){
// do something with the data
qDebug() << "received" << data.count() << "bytes";
}
Cheers,
_
Re: Serial read misses to read data from the serial port
Quote:
Originally Posted by
mania
I mean the data stored in arr_cReadbuffer is ^MK1 instead of the nBytesReceived is 9
If i were to take a stab at the dark based on the information you provided, I would guess that the fifth byte is a null character and you are reading the bytes as a string. Try printing the bytes 1 at a time in a loop rather than as a string.
Re: Serial read misses to read data from the serial port
anda_skoa:I read the data using readall which returns 0 bytes as the return value
nBytesReceived1=m_serialportCoProcessor->readAll();
qDebug()<<"rec1"<<nBytesReceived1.count();
Quote:
Originally Posted by
anda_skoa
How did you determine that your code snippet doesn't show.
Again, using readAll() will give you both the check and the data in one go
Code:
const QByteArray data
= m_serialportCoProcessor
->readAll
();
// check if there was data available
if (data.count() > 0){
// do something with the data
qDebug() << "received" << data.count() << "bytes";
}
Cheers,
_
Re: Serial read misses to read data from the serial port
Sure, if there is no data available, then nothing can be read.
The is like your bytesAvailable() check.
My suggestion was addressing the case when data is available.
Anyway, it might be helpful to see how you determine that you got less data than you expected.
All your snippet has so far is a log output that treats the data as an array of text characters.
Cheers,
_
Re: Serial read misses to read data from the serial port
But still while I use read data I am getting it as 9 bytes received
Re: Serial read misses to read data from the serial port
Show all of the code (method) performing the reading. Remember that the serial port does not know anything about the logic of your data (eg. That the package is 10 bytes).
P.S.
Read this thread.