介绍FT_SetEventNotification函数的用法、参数、返回值、注意事项、适用的操作系统环境、调用代码示例等。
支持的操作系统
Linux
Mac OS X 10.4 以上版本
Windows 2000 以上版本
Windows CE 4.2以上版本
概述
FT_SetEventNotification函数设定事件通知的条件。
定义
FT_STATUS FT_SetEventNotification (FT_HANDLE ftHandle, DWORD dwEventMask, PVOID pvArg)
参数说明
ftHandle—设备句柄。
dwEventMask—导致事件激发的条件。
pvArg—事件句柄。
返回值
成功则返回FT_OK,如果失败则根据错误返回特定错误码(FT_*).
备注
应用程序可以使用FT_SetEventNotification 函数来设置线程阻塞,等待某一条件满足。典型的情况如:应用程序创建一个事件,然后调用FT_SetEventNotification 函数,然后程序阻塞等待事件条件满足。当条件满足时,事件激活,线程解除阻塞状态。
参数dwEventMask 为一个位图(bit map),描述应用程序需要关心的事件条件。
参数pvArg 为程序创建的事件的句柄。事件条件有一个满足,则事件被设定激活。
如果在参数 dwEventMask 中设置 FT_EVENT_RXCHAR,则当接收到一个字符时就触发事件。
如果在参数 dwEventMask 中设置 FT_EVENT_MODEM_STATUS , 则当设备modem状态改变时触发事件。
如果在参数 dwEventMask 中设置 FT_EVENT_LINE_STATUS , 则当设备检测到线路状态变化时触发事件。
代码示例
1. 本段代码在 Windows and Windows CE 操作系统下适用。等待接收字符或者解调器modem状态变化。
// 第一步, 创建事件并调用 FT_SetEventNotification函数。
FT_HANDLE ftHandle; // handle of an open device
FT_STATUS ftStatus;
HANDLE hEvent;
DWORD EventMask;
hEvent = CreateEvent(
NULL,
false, // auto-reset event
false, // non-signalled state
Copyright © 2011 Future Technology Devices International Limited 36
Document Reference No.: FT_000071
D2XX Programmer’s Guide Version 1.3
Clearance No.: FTDI# 170
“”
);
EventMask = FT_EVENT_RXCHAR | FT_EVENT_MODEM_STATUS;
ftStatus = FT_SetEventNotification(ftHandle,EventMask,hEvent);
// Sometime later, block the application thread by waiting on the event, then when the
event has
// occurred, determine the condition that caused the event, and process it accordingly.
WaitForSingleObject(hEvent,INFINITE);
DWORD EventDWord;
DWORD RxBytes;
DWORD TxBytes;
FT_GetStatus(ftHandle,&RxBytes,&TxBytes,&EventDWord);
if (EventDWord & FT_EVENT_MODEM_STATUS) {
// modem status event detected, so get current modem status
FT_GetModemStatus(ftHandle,&Status);
if (Status & 0x00000010) {
// CTS is high
}
else {
// CTS is low
}
if (Status & 0x00000020) {
// DSR is high
}
else {
// DSR is low
}
}
if (RxBytes > 0) {
// call FT_Read() to get received data from device
}
2. 下面代码示例在 Linux 操作系统下适用。
// First, create the event and call FT_SetEventNotification.
FT_HANDLE ftHandle;
FT_STATUS ftStatus;
EVENT_HANDLE eh;
DWORD EventMask;
ftStatus = FT_Open(0, &ftHandle);
if(ftStatus != FT_OK) {
// FT_Open failed
return;
}
pthread_mutex_init(&eh.eMutex, NULL);
pthread_cond_init(&eh.eCondVar, NULL);
EventMask = FT_EVENT_RXCHAR | FT_EVENT_MODEM_STATUS;
ftStatus = FT_SetEventNotification(ftHandle, EventMask, (PVOID)&eh);
// Sometime later, block the application thread by waiting on the event, then when the
event has
// occurred, determine the condition that caused the event, and process it accordingly.
pthread_mutex_lock(&eh.eMutex);
pthread_cond_wait(&eh.eCondVar, &eh.eMutex);
pthread_mutex_unlock(&eh.eMutex);
DWORD EventDWord;
DWORD RxBytes;
Copyright © 2011 Future Technology Devices International Limited 37
Document Reference No.: FT_000071
D2XX Programmer’s Guide Version 1.3
Clearance No.: FTDI# 170
DWORD TxBytes;
DWORD Status;
FT_GetStatus(ftHandle,&RxBytes,&TxBytes,&EventDWord);
if (EventDWord & FT_EVENT_MODEM_STATUS) {
// modem status event detected, so get current modem status
FT_GetModemStatus(ftHandle,&Status);
if (Status & 0x00000010) {
// CTS is high
}
else {
// CTS is low
}
if (Status & 0x00000020) {
// DSR is high
}
else {
// DSR is low
}
}
if (RxBytes > 0) {
// call FT_Read() to get received data from device
}
FT_Close(ftHandle);
暂无评论内容