我写了一个DLL,专门用来从COM2口读取状态信息。代码如下:#include "stdafx.h"
#include <windows.h>
#include <assert.h>
#include "COM_CTSImpl.h"extern "C" int PASCAL EXPORT CheckCTSStatus()
{
HANDLE hCom = CreateFile(_T("COM2"),//"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.
return 0;
} SetCommMask(hCom, EV_RING | EV_CTS);
DWORD dwEvtMask = 0;
if(GetCommModemStatus(hCom, &dwEvtMask))
{
if (dwEvtMask & MS_RING_ON)
{
CloseHandle(hCom);
return 1;
}
}//if(GetCommModemStatus(hCom, &dwEvtMask)) CloseHandle(hCom); return 0;
}
我发现GetCommModemStatus这个函数返回是非零值,就是说执行成功了,但dwEvtMask却一直为0.
同样的情况下,我仅仅是把COM2改成COM1,则一切正常。
请问如何解决?
#include <windows.h>
#include <assert.h>
#include "COM_CTSImpl.h"extern "C" int PASCAL EXPORT CheckCTSStatus()
{
HANDLE hCom = CreateFile(_T("COM2"),//"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.
return 0;
} SetCommMask(hCom, EV_RING | EV_CTS);
DWORD dwEvtMask = 0;
if(GetCommModemStatus(hCom, &dwEvtMask))
{
if (dwEvtMask & MS_RING_ON)
{
CloseHandle(hCom);
return 1;
}
}//if(GetCommModemStatus(hCom, &dwEvtMask)) CloseHandle(hCom); return 0;
}
我发现GetCommModemStatus这个函数返回是非零值,就是说执行成功了,但dwEvtMask却一直为0.
同样的情况下,我仅仅是把COM2改成COM1,则一切正常。
请问如何解决?
解决方案 »
- 我想了解有关修改文件权限的api
- 用 XML DOM 保存的文件没有换行没有缩进,怎么办啊
- 一个view里面只能显示一个子view?
- opengl中加纹理
- 怎样取系统硬件的序列号?
- ■代码纠错 各位来拿分 【附加分】在其他帖子!
- 字符串前加上“L”的含义是什么?
- 一个使用activex control的问题,我添加完了后在右面的contrl panel会出现一个DBGrid的图标,但是mfc sample中的BINDENRRL中也使用了DBGrid右面却没有dbgrid的图标?
- VC的编译优化,真是见鬼了……
- PreCreateWindow被调用了几次
- 主对话框调用对话框类出错
- MFC中打开保存对话框如何添加文件格式的支持?
// 2、检查COM1和COM2的DCB有什么不同?没看到你设置DCB
是只有一个,但是是COM2,以前的主板是COM1口,现在换了,所以要用COM2口。
这是机器上的显示,确实是有COM2口的。
#include <windows.h>
#include <assert.h>
#include <stdio.h>void main( )
{
HANDLE hCom;
OVERLAPPED o;
BOOL fSuccess;
DWORD dwEvtMask; hCom = CreateFile( TEXT("COM2"),
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_RING | EV_CTS); 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); if (WaitCommEvent(hCom, &dwEvtMask, &o))
{
if (dwEvtMask & EV_RING)
{
// To do.
} if (dwEvtMask & EV_CTS)
{
// To do.
}
}
else
{
DWORD dwRet = GetLastError();
if( ERROR_IO_PENDING == dwRet)
{
printf("I/O is pending...\n"); // To do.
}
else
printf("Wait failed with error %d.\n", GetLastError());
}
}
zgl7903所写之代码似乎没有关闭句柄吧?
// 2、检查COM1和COM2的DCB有什么不同?没看到你设置DCB
按照你说的改了,结果发现COM1正在被使用。
please give your codes.
的确有改不成的时候。