FT_SetEventNotification on Linux



我有三个不同的java线程打开三个串行设备。我只是想知道我是否必须为read函数内的每个线程(局部变量(创建EVENT_HANDLE eh,或者只为readData函数外的全局变量创建EVENT-HANDLE eh。根据链接中提供的以下示例,pthread_cond_wait(&eh.eCondVar, &eh.eMutex);是关键部分https://www.ftdichip.com/Support/Knowledgebase/index.html?ft_seteventnotification.htm适用于Linux操作系统。我试图理解我是否必须执行相同的锁定机制,因为我每次只从每个java线程执行一次读取。请提供一些意见。

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);
pthread_mutex_lock(&eh.eMutex);
pthread_cond_wait(&eh.eCondVar, &eh.eMutex);
pthread_mutex_unlock(&eh.eMutex);
DWORD EventDWord;
DWORD RxBytes;
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);

我的代码是使用d2xx驱动程序的c#,但我希望它能有所帮助。您必须为打开的每个FTDI设备创建一个EVENT_HANDLE。打开设备时创建EventHandle,关闭设备时销毁它。在这种情况下,我们会监听事件。

在不同设备之间共享的全局句柄将具有混淆来自设备的事件通知的效果。不太好。

public FTDISerialPortDevice(string serialNumber)
{
_serialNumber = serialNumber
_ftdi = new FTDI(); // This is the ftdi device
_bytesReceivedEvent = new AutoResetEvent(false);
}

public void Open()
{
if (_ftdi.IsOpen)
throw new InvalidOperationException($"Access to the device '{_serialNumber}' is denied.");

if (_ftdi.OpenBySerialNumber(_serialNumber ) != FTDI.FT_STATUS.FT_OK)
throw new InvalidOperationException($"Access to the device '{_serialNumber }' is denied.");

// Do device configuration stuff ....
_ftdi.SetEventNotification(FTDI.FT_EVENTS.FT_EVENT_RXCHAR, _bytesReceivedEvent);

}

public int Receive(byte[] readBuffer, int offset)
{
// listen to event and do reads
}

最新更新