WaitCommEvent

  作用:   为一个特指的通信设备等待一个事件发生,该函数所监控的事件是与该设备句柄相关联的一系列事件。   原型:   BOOL WINAPI WaitCommEvent(   __in HANDLE hFile,   __out LPDWORD lpEvtMask,   __in LPOVERLAPPED lpOverlapped   );   参数:   hFile:指向通信设备的一个句柄,该句柄应该是由 CreateFile函数返回的。   lpEvtMask:一个指向DWORD的指针。如果发生错误,pEvtMask指向0,否则指向以下的某一事件   Value Meaning EV_BREAK   0x0040 A break was detected on input.(在输入过程中发生中断) EV_CTS   0x0008 The CTS (clear-to-send) signal changed state.(CTS线上的信号改变状态) EV_DSR   0x0010 The DSR (data-set-ready) signal changed state.(DSR 线上的信号改变状态) EV_ERR   0x0080 A line-status error occurred. Line-status errors are CE_FRAME, CE_OVERRUN, and CE_RXPARITY. EV_RING   0x0100 A ring indicator was detected. EV_RLSD   0x0020 The RLSD (receive-line-signal-detect) signal changed state. EV_RXCHAR   0x0001 A character was received and placed in the input buffer.(输入缓冲区不为空) EV_RXFLAG   0x0002 The event character was received and placed in the input buffer. The event character is specified in the device's DCB structure, which is applied to a serial port by using the SetCommState function. EV_TXEMPTY   0x0004 The last character in the output buffer was sent.(输出缓冲区的数据全部发送出去)   lpOverlapped :指向OVERLAPPED结构体的一个指针。如果hFile是用异步方式打开的(在CreateFile()函数中,第三个参数设置为FILE_FLAG_OVERLAPPED),lpOverlapped 不能指向一个空OVERLAPPED结构体,而是与Readfile()和WreteFile()中的OVERLAPPED参数为同一个参数。如果hFile是用异步方式打开的,而lpOverlapped 指向一个空的OVERLAPPED结构体,那么函数会错误地报告,等待的操作已经完成(而此时等待的操作可能还没有完成)。   如果hFile是用异步方式打开的,而lpOverlapped 指向一个非空的OVERLAPPED结构体,那么函数WaitCommEvent被默认为异步操作,马上返回。这时,OVERLAPPED结构体必须包含一个由CreateEvent()函数返回的手动重置事件对象的句柄hEven。   如果hFile是用同步方式打开的,那么函数WaitCommEvent不会返回,直到要等待的事件发生。   返回值:   如果函数成功,返回非零值,否则返回0。要得到错误信息,可以调用GetLastError函数。   备注:   WaitCommEvent函数为指定的通信资源监听一系列的Event,这些Event可以由 SetcommMask和 GetcommMask函数来设置和查询。   如果异步操作不能马上完成,那么该函数会返回一个FALSE,同时GetLastError函数可以截获错误码ERROR_IO_PENDING(#define ERROR_IO_PENDING 997),表示操作转到后台运行。在WaitCommEvent函数返回之前,系统将OVERLAPPED结构中的hEven句柄设置为无信号状态;当WaitCommEvent函数所等待的任何一个Event发生后,系统将OVERLAPPED结构中的hEven句柄设置为有信号状态,同时将所发生事件赋给lpEvtMask。   父进程可以根据lpEvtMask来做出相应的事件处理,然后也可以调用GetOverlappedResult函数来判断WaitCommEvent的操作是否成功。   如果WaitCommEvent函数在后台运行的时候,进程企图想通过 SetcommMask函数来改变当前设备的Event,那么WaitCommEvent函数马上返回,lpEvtMask指向0。   举例:   The following example code opens the serial port for overlapped I/O, creates an event mask to monitor CTS and DSR signals, and then waits for an event to occur. The WaitCommEvent function should be executed as an overlapped operation so the other threads of the process can perform I/O operations during the wait.   #include   #include   #include   void main( )   {   HANDLE hCom;   OVERLAPPED o;   BOOL fSuccess;   DWORD dwEvtMask;   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_CTS | EV_DSR);   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_DSR)   {   // 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());   }   }
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值