Is there a non-blocking function which returns the current rx queue length of a serial port in Windows, using C?
All examples I've seen simply call ReadFile which blocks until the specified timeout, so I was wondering if it's possible to check if there is anything in the buffer before reading?
E.g. I can simply do this for each character:
void ReadCharacter(char *theCharacter)
{
DWORD numBytesRead = 0;
while (numBytesRead == 0)
{
ReadFile(comPorthandle,
theCharacter,
sizeof(char),
&numBytesRead,
NULL);
}
}
But is it possible to have something like
int numBytesRx = GetNumBytesRx(&portHandle);
if (numBytesRx > 0)
Read(&portHandle, targetBuffer, numBytesRead);
To perform async IO with COM port via ReadFile consider using last parameter of function, LPOVERLAPPED and OVERLAPPED structure. OVERLAPPED is common practice for async IO in Windows.
Here you can find examples
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With