小编Som*_*uts的帖子

使用 WaitCommEvent 时 WriteFile 挂起应用程序

我遇到了使用事件驱动方法进行串行端口通信的 win32 编程问题。我将我的通信句柄创建为:

hComm = CreateFile(lpszCommName, GENERIC_READ | GENERIC_WRITE, 0,
    NULL, OPEN_EXISTING, 0, NULL);
Run Code Online (Sandbox Code Playgroud)

我将 CommTimeouts 设置为:

    commTimeout.ReadIntervalTimeout = MAXWORD;
    commTimeout.ReadTotalTimeoutConstant = 0;
    commTimeout.ReadTotalTimeoutMultiplier = 0;
    commTimeout.WriteTotalTimeoutConstant = 0;
    commTimeout.WriteTotalTimeoutMultiplier = 0;
Run Code Online (Sandbox Code Playgroud)

我为 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)
                /* …
Run Code Online (Sandbox Code Playgroud)

c winapi serial-port writefile readfile

5
推荐指数
1
解决办法
1677
查看次数

标签 统计

c ×1

readfile ×1

serial-port ×1

winapi ×1

writefile ×1