public OpenByIndex ( |
||
index | Index of the device to open. /// Note that this cannot be guaranteed to open a specific device. | |
Résultat | FT_STATUS |
public FT_STATUS OpenByIndex(UInt32 index)
{
// 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_Open != IntPtr.Zero) & (pFT_SetDataCharacteristics != IntPtr.Zero) & (pFT_SetFlowControl != IntPtr.Zero) & (pFT_SetBaudRate != IntPtr.Zero))
{
tFT_Open FT_Open = (tFT_Open)Marshal.GetDelegateForFunctionPointer(pFT_Open, typeof(tFT_Open));
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_Open
ftStatus = FT_Open(index, 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_Open == IntPtr.Zero)
{
LogB.Debug("FTD2XX: Failed to load function FT_Open.");
}
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;
}
private void Open(FTDI port, string description) { uint deviceCount = 0; if (port == null) throw new Exception(description + "is not initialized"); if (port.IsOpen) { Global.ShowError(description + " is already open"); return; } // Determine the number of FTDI devices connected to the machine if ((ftStatus = port.GetNumberOfDevices(ref deviceCount)) != FTDI.FT_STATUS.FT_OK) { throw new FTDI.FT_EXCEPTION("Failed to get number of devices. err: " + ftStatus.ToString()); } // If no devices available, return if (deviceCount == 0) throw new Exception("No devices on " + description); // Allocate storage for device info list FTDI.FT_DEVICE_INFO_NODE[] ftdiDeviceList = new FTDI.FT_DEVICE_INFO_NODE[deviceCount]; // Populate our device list if ((ftStatus = port.GetDeviceList(ftdiDeviceList)) != FTDI.FT_STATUS.FT_OK) { throw new FTDI.FT_EXCEPTION("Failed to get device list. err " + ftStatus.ToString()); } int foundIndex = -1; for (int i = 0; i < deviceCount; i++) { if (ftdiDeviceList[i].Description.ToString().Contains(description)) { foundIndex = i; } /* Console.WriteLine("Device Index: " + i.ToString()); Console.WriteLine("Flags: " + String.Format("{0:x}", ftdiDeviceList[i].Flags)); Console.WriteLine("Type: " + ftdiDeviceList[i].Type.ToString()); Console.WriteLine("ID: " + String.Format("{0:x}", ftdiDeviceList[i].ID)); Console.WriteLine("Location ID: " + String.Format("{0:x}", ftdiDeviceList[i].LocId)); Console.WriteLine("Serial Number: " + ftdiDeviceList[i].SerialNumber.ToString()); Console.WriteLine("Description: " + ftdiDeviceList[i].Description.ToString()); Console.WriteLine(""); */ } if (foundIndex < 0) { throw new Exception("Failed to find device with description " + description); } if ((ftStatus = port.OpenByIndex((uint) foundIndex)) != FTDI.FT_STATUS.FT_OK) { throw new FTDI.FT_EXCEPTION("Failed to open device. err: " + ftStatus.ToString()); } }