I'm having a trouble with managing data coming into a linux serial port from an Arduino unit.
Basically, I have a working code to read and print out (or just store) but it is written for windows platform:
status is of type COMSTAT
int SerialPort::readSerialPort(char *buffer, unsigned int buf_size)
{
DWORD bytesRead;
unsigned int toRead = 0;
Sleep(500);
ClearCommError(this->handler, &this->errors, &this->status);
if (this->status.cbInQue > 0) { //if there's something to read
if (this->status.cbInQue > buf_size) {//if there's something to read&bigger than buffer size
toRead = buf_size; //read as much as buffer alows
}
else toRead = this->status.cbInQue; //else just read as much as there is
}
if (ReadFile(this->handler, buffer, toRead, &bytesRead, NULL))
return bytesRead; //return read data
return 0;//return 0 if nothing is there.
}
apart from Sleep() that is windows function, I was wondering if there is an equivalent linux function for status.cbInQue to understand if there is any data that is ready to be read in the port. Right now I just go ahead and read, without checking, and often I get nothing printed later in the program.
TLDR: is there cbInQue equivalent for linux?
Thanks
Yes, you will need your file descriptor and then use FIONREAD
to see if anything is available.
Something like the following should work:
int available;
if( ioctl( fd, FIONREAD, &available ) < 0 ) {
// Error handling here
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.