public FT_STATUS OpenByDescription(string description)
{
// Initialise ftStatus to something other than FT_OK
FT_STATUS ftStatus = FT_STATUS.FT_OTHER_ERROR;
// If the DLL hasn't been loaded, just return here
if (hFTD2XXDLL == IntPtr.Zero)
return ftStatus;
// Check for our required function pointers being set up
if ((pFT_OpenEx != IntPtr.Zero) & (pFT_SetDataCharacteristics != IntPtr.Zero) & (pFT_SetFlowControl != IntPtr.Zero) & (pFT_SetBaudRate != IntPtr.Zero))
{
tFT_OpenEx FT_OpenEx = (tFT_OpenEx)Marshal.GetDelegateForFunctionPointer(pFT_OpenEx, typeof(tFT_OpenEx));
tFT_SetDataCharacteristics FT_SetDataCharacteristics = (tFT_SetDataCharacteristics)Marshal.GetDelegateForFunctionPointer(pFT_SetDataCharacteristics, typeof(tFT_SetDataCharacteristics));
tFT_SetFlowControl FT_SetFlowControl = (tFT_SetFlowControl)Marshal.GetDelegateForFunctionPointer(pFT_SetFlowControl, typeof(tFT_SetFlowControl));
tFT_SetBaudRate FT_SetBaudRate = (tFT_SetBaudRate)Marshal.GetDelegateForFunctionPointer(pFT_SetBaudRate, typeof(tFT_SetBaudRate));
// Call FT_OpenEx
ftStatus = FT_OpenEx(description, FT_OPEN_BY_DESCRIPTION, ref ftHandle);
// Appears that the handle value can be non-NULL on a fail, so set it explicitly
if (ftStatus != FT_STATUS.FT_OK)
ftHandle = IntPtr.Zero;
if (ftHandle != IntPtr.Zero)
{
// Initialise port data characteristics
byte WordLength = FT_DATA_BITS.FT_BITS_8;
byte StopBits = FT_STOP_BITS.FT_STOP_BITS_1;
byte Parity = FT_PARITY.FT_PARITY_NONE;
ftStatus = FT_SetDataCharacteristics(ftHandle, WordLength, StopBits, Parity);
// Initialise to no flow control
UInt16 FlowControl = FT_FLOW_CONTROL.FT_FLOW_NONE;
byte Xon = 0x11;
byte Xoff = 0x13;
ftStatus = FT_SetFlowControl(ftHandle, FlowControl, Xon, Xoff);
// Initialise Baud rate
UInt32 BaudRate = 9600;
ftStatus = FT_SetBaudRate(ftHandle, BaudRate);
}
}
else
{
if (pFT_OpenEx == IntPtr.Zero)
{
LogB.Debug("FTD2XX: Failed to load function FT_OpenEx.");
}
if (pFT_SetDataCharacteristics == IntPtr.Zero)
{
LogB.Debug("FTD2XX: Failed to load function FT_SetDataCharacteristics.");
}
if (pFT_SetFlowControl == IntPtr.Zero)
{
LogB.Debug("FTD2XX: Failed to load function FT_SetFlowControl.");
}
if (pFT_SetBaudRate == IntPtr.Zero)
{
LogB.Debug("FTD2XX: Failed to load function FT_SetBaudRate.");
}
}
return ftStatus;
}