我写了一个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,则一切正常。
请问如何解决?

解决方案 »

  1.   

    这个跟你的机器com口有关系的
      

  2.   

    // 1、检查COM2是否为物理串口,也就是说检查COM2这个串口是否确实存在
    // 2、检查COM1和COM2的DCB有什么不同?没看到你设置DCB
      

  3.   


    是只有一个,但是是COM2,以前的主板是COM1口,现在换了,所以要用COM2口。
    这是机器上的显示,确实是有COM2口的。
      

  4.   

    等待事件
    #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());
        }
    }
      

  5.   

    既然以前是好的,在设备管理器里面将com2改成com1就行了。
      

  6.   

    楼上两位所提方法,我先试一下。
    zgl7903所写之代码似乎没有关闭句柄吧?
      

  7.   

    // 1、检查COM2是否为物理串口,也就是说检查COM2这个串口是否确实存在
    // 2、检查COM1和COM2的DCB有什么不同?没看到你设置DCB
      

  8.   


    按照你说的改了,结果发现COM1正在被使用。
      

  9.   


    please give your codes.
      

  10.   

    先把com1改成com20,再把com2改成com1,被使用一般都是一些驱动虚拟出来的一些串口,从前往后占用空的串口号。--------
    的确有改不成的时候。
      

  11.   

    zgl7903的代码我试了,if (WaitCommEvent(hCom, &dwEvtMask, &o)) 条件一直为假。
      

  12.   

    COM2串口不存在吧,没有这个串口肯定打开失败