WaitCommEvent function

Waits for an event to occur for a specified communications device. The set of events that are monitored by this function is contained in the event mask associated with the device handle.

等待制定通讯设备产生一个事件.这个函数可以监视与设备句柄相关联的的事件(具体什么事件,是通过查看参数event mask。)

Syntax

C++
BOOL WINAPI WaitCommEvent(
  _In_   HANDLE hFile,
  _Out_  LPDWORD lpEvtMask,
  _In_   LPOVERLAPPED lpOverlapped
);

Parameters

hFile [in]

A handle to the communications device. The CreateFile function returns this handle.

通讯设备的句柄。函数CreatFile返回这个句柄。

lpEvtMask [out]

A pointer to a variable that receives a mask indicating the type of event that occurred. If an error occurs, the value is zero; otherwise, it is one of the following values.

指向一个变量的指针,这个变量表示接收到的事件类型。如果错误发生,这个值为零。否则是下列值中的一个。

ValueMeaning
EV_BREAK 0x0040

A break was detected on input .检测到输入中断。

EV_CTS 0x0008

The CTS (clear-to-send) signal changed state.准备发送数据信号 改变状态。

EV_DSR 0x0010

The DSR (data-set-ready) signal changed state. 数据装置准备就绪信号(MODEM处于可用状态) 改变状态。

EV_ERR 0x0080

A line-status error occurred. Line-status errors are CE_FRAME,CE_OVERRUN, andCE_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'sDCB structure, which is applied to a serial port by using theSetCommState function.

EV_TXEMPTY 0x0004

The last character in the output buffer was sent. 输出缓存最后一个字符已输出。(TX为空)

 

lpOverlapped [in]

A pointer to an OVERLAPPED structure. This structure is required if hFile was opened withFILE_FLAG_OVERLAPPED.

一个指向OVERLAPPED结构体的指针。如果通讯设备的句柄是通过FILE_FLAG_OVERLAPPED打开的,那么这个结构体是必须的。

 

If hFile was opened with FILE_FLAG_OVERLAPPED, the lpOverlapped parameter must not be NULL. It must point to a validOVERLAPPED structure. IfhFile was opened with FILE_FLAG_OVERLAPPED and lpOverlapped isNULL, the function can incorrectly report that the operation is complete.

如果通讯设备的句柄是通过FILE_FLAG_OVERLAPPED打开的,那么lpOverlapped这个参数不能为NULL. 它必须指向一个有效的OVERLAPPED结构体。

如果通讯设备的句柄是通过FILE_FLAG_OVERLAPPED打开的,并且lpOverlapped这个参数为NULL。这个函数所报告的相关操作将是错误的。

 

If hFile was opened with FILE_FLAG_OVERLAPPED and lpOverlapped is not NULL, WaitCommEvent is performed as an overlapped operation. In this case, theOVERLAPPED structure must contain a handle to a manual-reset event object (created by using theCreateEvent function).

如果通讯设备的句柄是通过FILE_FLAG_OVERLAPPED打开的,并且lpOverlapped这个参数不为NULL,WaitCommEvent表现为异步输入输出操作方式。在这种情况下,OVERLAPPED结构必须包含一个 手动重置事件对象 的句柄(由CreateEvent函数产生的)。

 

If hFile was not opened with FILE_FLAG_OVERLAPPED, WaitCommEvent does not return until one of the specified events or an error occurs.

如果通讯设备的句柄不是以FILE_FLAG_OVERLAPPED方式打开的(也就是以同步的方式打开的),WaitCommEvent不会有返回值,除非指定的事件产生或是有错误产生。

Return value

If the function succeeds, the return value is nonzero.

If the function fails, the return value is zero. To get extended error information, callGetLastError.

Remarks

The WaitCommEvent function monitors a set of events for a specified communications resource. To set and query the current event mask of a communications resource, use theSetCommMask and GetCommMask functions.

If the overlapped operation cannot be completed immediately, the function returnsFALSE and theGetLastError function returns ERROR_IO_PENDING, indicating that the operation is executing in the background. When this happens, the system sets thehEvent member of theOVERLAPPED structure to the not-signaled state before WaitCommEvent returns, and then it sets it to the signaled state when one of the specified events or an error occurs. The calling process can use one of thewait functions to determine the event object's state and then use the GetOverlappedResult function to determine the results of theWaitCommEvent operation.GetOverlappedResult reports the success or failure of the operation, and the variable pointed to by thelpEvtMask parameter is set to indicate the event that occurred.

If a process attempts to change the device handle's event mask by using the SetCommMask function while an overlapped WaitCommEvent operation is in progress,WaitCommEvent returns immediately. The variable pointed to by thelpEvtMask parameter is set to zero.

Examples

For an example, see Monitoring Communications Events.

Requirements

Minimum supported client

Windows XP

Minimum supported server

Windows Server 2003

Header

Winbase.h (include Windows.h)

Library

Kernel32.lib

DLL

Kernel32.dll

See also

Communications Functions Communications Resources CreateFile DCB GetCommMask GetOverlappedResult OVERLAPPED SetCommMask SetCommState

 

 

Send comments about this topic to Microsoft

Build date: 4/16/2013

Community Additions

ADD

Clarification about "failure", ERROR_IO_PENDING and blocking

Regarding the phrase "If the overlapped operation cannot be completed immediately": This is somewhat misleading, because it's not a matter of whether this operation "can" or "cannot" complete. Rather, it's whether it's "allowed to block" based on whether the port was opened with FILE_FLAG_OVERLAPPED. If it was, then this Wait function, despite its name, is not allowed to block.


What it really means is "if there are no events to report, and the communication port was opened in overlapped mode, then this will return false and not block. If blocking is desired then you will need to wait on the actual event object contained within the OVERLAPPED structure by using GetOverlappedResult". This function will remember the original pointer passed in the lpEvtMask parameter, and will update it when it signals the event.

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
WaitCommEvent 是一个用于等待串口事件的函数,可以等待串口收到数据或者数据发送完毕等事件的发生。下面是一个使用 WaitCommEvent 函数的示例: ```c++ #include <Windows.h> #include <iostream> int main() { HANDLE hComm; DWORD dwEventMask; bool success; // 打开串口 hComm = CreateFile("COM1", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL); if (hComm == INVALID_HANDLE_VALUE) { std::cout << "Failed to open COM1\n"; return 1; } // 设置串口参数 DCB dcbSerialParams = { 0 }; dcbSerialParams.DCBlength = sizeof(DCB); success = GetCommState(hComm, &dcbSerialParams); if (!success) { std::cout << "Failed to get serial parameter\n"; CloseHandle(hComm); return 1; } dcbSerialParams.BaudRate = CBR_9600; dcbSerialParams.ByteSize = 8; dcbSerialParams.StopBits = ONESTOPBIT; dcbSerialParams.Parity = NOPARITY; success = SetCommState(hComm, &dcbSerialParams); if (!success) { std::cout << "Failed to set serial parameter\n"; CloseHandle(hComm); return 1; } // 设置串口事件 success = SetCommMask(hComm, EV_RXCHAR); if (!success) { std::cout << "Failed to set comm mask\n"; CloseHandle(hComm); return 1; } // 等待串口事件 success = WaitCommEvent(hComm, &dwEventMask, NULL); if (!success) { std::cout << "Failed to wait comm event\n"; CloseHandle(hComm); return 1; } else { if (dwEventMask & EV_RXCHAR) { std::cout << "Received data from COM1\n"; } } // 关闭串口 CloseHandle(hComm); return 0; } ``` 这个例子中,首先打开串口 "COM1",然后设置串口参数,接着设置串口事件为 EV_RXCHAR,即等待串口收到数据事件的发生。最后调用 WaitCommEvent 函数等待事件的发生,如果收到 EV_RXCHAR 事件,就输出 "Received data from COM1"。最后关闭串口。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值