各位好,我想问下用windows API函数编写串口通信程序时的一个问题,我参考msdn上的WaitCommEvent上的例子,用了个无限循环,但是一直报错为error87 和IO_PENDING_ERROR,在网上看过类似的例子,但是一直没找到解决的办法,希望你们能帮助我,我的源代码如下:#include <windows.h>
#include <assert.h>
#include <stdio.h>void main( )
{
    HANDLE hCom;
    OVERLAPPED o;
    BOOL fSuccess;
    DWORD dwEvtMask;
DWORD dwLength = 0;
BOOL  fLight;    hCom = CreateFile( TEXT("COM1"),
        GENERIC_READ | GENERIC_WRITE,
        0,    // exclusive access 
        NULL, // default security attributes 
        OPEN_EXISTING,
        FILE_FLAG_OVERLAPPED,
        NULL 
        );    if (hCom == INVALID_HANDLE_VALUE) 
    {
        // Handle the error. 
        printf("CreateFile failed with error %d.\n", GetLastError());
        return;
    }    // Set the event mask.     fSuccess = SetCommMask(hCom, EV_RXCHAR); PurgeComm(hCom, PURGE_RXCLEAR);    if (!fSuccess) 
    {
        // Handle the error. 
        printf("SetCommMask failed with error %d.\n", GetLastError());
        return;
    }    // Create an event object for use by WaitCommEvent.     o.hEvent = CreateEvent(
        NULL,   // default security attributes 
        TRUE,   // manual-reset event 
        FALSE,  // not signaled 
        NULL    // no name
);
        // Initialize the rest of the OVERLAPPED structure to zero.
    o.Internal = 0;
    o.InternalHigh = 0;
    o.Offset = 0;
    o.OffsetHigh = 0;    assert(o.hEvent); while(1)
{
fLight = WaitCommEvent(hCom, &dwEvtMask, &o);

if (fLight)

 printf("EV_RXCHAR is doing...\n");
}
else
{

DWORD dwRet = GetLastError();
if( ERROR_IO_PENDING == dwRet)
{
printf("I/O is pending...\n");

}
else 
printf("Wait failed with error %d.\n", GetLastError());
}
Sleep(1);
  }
}(在线等)

解决方案 »

  1.   

    ERROR_IO_PENDING 这不是真正的错误
    他表示你发出读写请求命令成功,只是操作还没有完成。你都没有读写操作,所以没有事件发生,IO一直处在PENDING状态。
      

  2.   

    你好,不是因为我没有读操作,程序都一直没有进入正确的printf("EV_RXCHAR is doing...\n");
    这句,因为WaitCommEvent()的返回值一直为0,我有过另一个函数ReadFile放在if()后,但是一直进入不到啊,返回值一直为0,错误为87或者997
      

  3.   

    如果这个程序能够进入if()里面,程序就能够调通了,呵呵,但是这个程序一直进不去,困闷ing...
      

  4.   

    我是在wince下面用的WaitCommEvent,程序一跑到那没有反应了,不吝指教啊!