我正在为使用事件驱动方法进行串行端口通信的win32编程提出问题。我有我的通讯手柄创建为:WriteFile在使用WaitCommEvent时挂起应用程序
hComm = CreateFile(lpszCommName, GENERIC_READ | GENERIC_WRITE, 0,
NULL, OPEN_EXISTING, 0, NULL);
,并设置我的COMMTIMEOUTS为:
commTimeout.ReadIntervalTimeout = MAXWORD;
commTimeout.ReadTotalTimeoutConstant = 0;
commTimeout.ReadTotalTimeoutMultiplier = 0;
commTimeout.WriteTotalTimeoutConstant = 0;
commTimeout.WriteTotalTimeoutMultiplier = 0;
我创建了ReadFile的线程,看起来像这样:
SetCommMask(hComm, EV_RXCHAR);
while (isConnected)
{
if (WaitCommEvent(hComm, &dwEvent, NULL)) //If i comment out this block my write file will work fine
{
ClearCommError(hComm, &dwError, &cs);
if ((dwEvent & EV_RXCHAR) && cs.cbInQue)
{
if (!ReadFile(hComm, str, cs.cbInQue, &read_byte, NULL))
/* Process error*/
else if (read_byte)
/* Print to screen */
}
else {
/* Process error*/
}
}
}
PurgeComm(hComm, PURGE_RXCLEAR);
我Wrifile去转换成当WM_CHAR被触发时向通信设备发送字符的WndProc:
VOID Write_To_Serial(WPARAM wParam, HWND hwnd){
DWORD write_byte;
char str[10];
sprintf_s(str, "%c", (char)wParam); //Convert wParam to a string
WriteFile(hComm, str, strlen(str), &write_byte, NULL)//Program hangs here
}
我的问题是每次WriteFile()被称为我的应用程序挂起,我不得不强制关闭它。如果我在我读取的线程中注释掉WaitCommEvent(),它可以正常工作,但是我无法阅读。任何指针都将不胜感激。谢谢
我不太记得,但我认为你需要一个'FILE_FLAG_OVERLAPPED'在你的实现中。如果它可以帮助你看看我的旧的旧的旧的CBuilder Com Port组件[这里](http://sourceforge.net/projects/tlpscomport/) – LPs
它是没有意义的,它会阻止。寻找不平凡的错误。 'str'没有正确的零终止,很容易出错,有些东西会爆炸。并尝试另一个USB模拟器,周围有很多垃圾。 –
你可以发布写入端口的实际代码吗?在当前代码中,你发送的是未初始化的堆栈垃圾,我希望这不是你实际做的。 – theB