Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting input buffer length for Windows serial port in C

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);
like image 699
Lou Avatar asked Sep 14 '25 16:09

Lou


1 Answers

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

like image 63
V. Kravchenko Avatar answered Sep 17 '25 06:09

V. Kravchenko