I'm trying to write a small utility that maps stdin/stdout to a serial port (a command line terminal emulator of sorts) using the Win32 APIs. I have the following code, which I think ought to work, but it doesn't appear to be receiving notifications properly from the serial port:
HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
HANDLE hCom = CreateFile(com_name, GENERIC_READ | GENERIC_WRITE, NULL, NULL, OPEN_EXISTING, 0, NULL);
/* check for errors opening the serial port, configure, set timeouts, etc */
HANDLE hWaitHandles[2];
hWaitHandles[0] = hStdin;
hWaitHandles[1] = hCom;
DWORD dwWaitResult = 0;
for (;;) {
dwWaitResult = WaitForMultipleObjects(2, hWaitHandles, FALSE, INFINITE);
if(dwWaitResult == WAIT_OBJECT_0)
{
DWORD bytesWritten;
int c = _getch();
WriteFile(hCom, &c, 1, &bytesWritten, NULL);
FlushConsoleInputBuffer( hStdin);
} else if (dwWaitResult == WAIT_OBJECT_0+1) {
char byte;
ReadFile(hCom, &byte, 1, &bytesRead, NULL);
if (bytesRead)
printf("%c",byte);
}
}
Any ideas what I'm doing wrong here?