各位好,我想问下用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);
}
}(在线等)
解决方案 »
- (100分求助)MS Project2007接口导出问题
- 紧急求助,路过的各位高手帮忙看看!!!
- 请问:如果使程序最小化后,显示一个小图标在窗口右下角的任务栏中啊???
- 那位兄弟帮忙看看这个warning会是什么问题
- 有办过劝退的吗,劝退会不会记入档案?
- ★『散分』在csdn上终于混到星星了! 同时在母亲节即将到来之际:祝福敬爱的母亲健康、快乐!
- 我用Install Shield做了一个安装程序,想在安装过程结束时自动启动另一个安装程序,应怎样实现!!
- 所有的文件中都没有CDateSet这个类,只有Classwizard中看到这个类,我现在要怎么删除这个类
- 如何在VC中测试G.729各个函数的执行时间或者CPU占用率
- 怎么样在SDI中弹出的对话框里取得一个子视图的指针?
- 生产者和消费者算法讨论
- 已知RGB矩阵,如何生成相应的图片
他表示你发出读写请求命令成功,只是操作还没有完成。你都没有读写操作,所以没有事件发生,IO一直处在PENDING状态。
这句,因为WaitCommEvent()的返回值一直为0,我有过另一个函数ReadFile放在if()后,但是一直进入不到啊,返回值一直为0,错误为87或者997