Last active
February 24, 2018 22:43
-
-
Save Bovaz/aad3609abb70f23a208a to your computer and use it in GitHub Desktop.
C# code to connect to a FTDI device using their FTD2XX library
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#region FTDI | |
static public FTDI myDevice; //do not forget to initialize this in the call to Load() | |
public UInt32 ftdiDeviceCount = 0; | |
public FTDI.FT_DEVICE_INFO_NODE[] ftdiDeviceList; | |
private bool ConnectionInit() | |
{ | |
//Check that there is a device connected | |
if (myDevice == null) | |
myDevice = new FTDI(); | |
int deviceCount = countDevices(); | |
if (deviceCount <= 0) | |
{ | |
Log("No device connected."); | |
return false; | |
} | |
//Connect to the device | |
FTDI.FT_STATUS ftStatus = myDevice.OpenByDescription("PAD+ v1.0"); | |
if (ftStatus != FTDI.FT_STATUS.FT_OK) | |
{ | |
Log("Error connecting to FTDI chip."); | |
return false; | |
} | |
//purge the buffers | |
do | |
{ | |
ftStatus = myDevice.StopInTask(); | |
} while (ftStatus != FTDI.FT_STATUS.FT_OK); | |
ftStatus = myDevice.Purge(FTDI.FT_PURGE.FT_PURGE_RX | FTDI.FT_PURGE.FT_PURGE_TX); | |
do | |
{ | |
ftStatus = myDevice.RestartInTask(); | |
} while (ftStatus != FTDI.FT_STATUS.FT_OK); | |
myDevice.SetLatency(2); | |
return true; | |
} | |
private int countDevices() | |
{ | |
FTDI.FT_STATUS ftStatus = FTDI.FT_STATUS.FT_OK; | |
ftStatus = myDevice.GetNumberOfDevices(ref ftdiDeviceCount); | |
if (ftStatus != FTDI.FT_STATUS.FT_OK) | |
return 0; | |
if (ftdiDeviceCount > 0) | |
{ | |
//allocate device info list | |
ftdiDeviceList = new FTDI.FT_DEVICE_INFO_NODE[ftdiDeviceCount]; | |
//populate list | |
ftStatus = myDevice.GetDeviceList(ftdiDeviceList); | |
if (ftStatus == FTDI.FT_STATUS.FT_OK) | |
{ | |
return (int)ftdiDeviceCount; | |
} | |
else | |
return 0; | |
} | |
else | |
return 0; | |
} | |
#endregion |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment