我正在尝试编写一个使用 Win32 API 将 stdin/stdout 映射到串行端口(各种命令行终端仿真器)的小实用程序。我有以下代码,我认为它应该可以工作,但它似乎没有从串口正确接收通知:
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);
}
}
有什么想法我在这里做错了吗?