I have three different java threads opening three Serial Devices. I am just curious if I have to create EVENT_HANDLE eh for each thread (local variable) inside read function or just a Global variable outside of the read Data function. And pthread_cond_wait(&eh.eCondVar, &eh.eMutex);
is a critical section according to the example below provided in the link https://www.ftdichip.com/Support/Knowledgebase/index.html?ft_seteventnotification.htm for Linux operating system. I am trying to understand if I have to do the same locking mechanism because I am only doing one read at a time from each of my java threads. Please provide some inputs.
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);
My code is c# using the d2xx driver, but I hope it helps. You have to create an EVENT_HANDLE for each FTDI device you open. Create the EventHandle when you open the device and destroy it when you close it. In the meanwile listen to events.
A global handle, shared between different devices, would have the effect of mixing up event notification from the devices. Not so good.
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
}
User contributions licensed under CC BY-SA 3.0