/packages/FTD2XX_NET.1.0.14/src/FTD2XX_NET.cs

https://bitbucket.org/adronhell/mpsse_i2c · C# · 6419 lines · 3596 code · 500 blank · 2323 comment · 780 complexity · 0ab3fc3c4d12b689078d95c3455671d2 MD5 · raw file

  1. /*
  2. ** FTD2XX_NET.cs
  3. **
  4. ** Copyright © 2009-2012 Future Technology Devices International Limited
  5. **
  6. ** C# Source file for .NET wrapper of the Windows FTD2XX.dll API calls.
  7. ** Main module
  8. **
  9. ** Author: FTDI
  10. ** Project: CDM Windows Driver Package
  11. ** Module: FTD2XX_NET Managed Wrapper
  12. ** Requires:
  13. ** Comments:
  14. **
  15. ** History:
  16. ** 1.0.0 - Initial version
  17. ** 1.0.12 - Included support for the FT232H device.
  18. ** 1.0.14 - Included Support for the X-Series of devices.
  19. **
  20. */
  21. using System;
  22. using System.Collections.Generic;
  23. using System.Text;
  24. using System.Runtime.InteropServices;
  25. using System.Threading;
  26. using System.Windows.Forms;
  27. using System.IO;
  28. namespace FTD2XX_NET
  29. {
  30. /// <summary>
  31. /// Class wrapper for FTD2XX.DLL
  32. /// </summary>
  33. public class FTDI
  34. {
  35. #region CONSTRUCTOR_DESTRUCTOR
  36. // constructor
  37. /// <summary>
  38. /// Constructor for the FTDI class.
  39. /// </summary>
  40. public FTDI()
  41. {
  42. // If FTD2XX.DLL is NOT loaded already, load it
  43. if (hFTD2XXDLL == IntPtr.Zero)
  44. {
  45. // Load our FTD2XX.DLL library
  46. hFTD2XXDLL = LoadLibrary(@"FTD2XX.DLL");
  47. if (hFTD2XXDLL == IntPtr.Zero)
  48. {
  49. // Failed to load our FTD2XX.DLL library from System32 or the application directory
  50. // Try the same directory that this FTD2XX_NET DLL is in
  51. MessageBox.Show("Attempting to load FTD2XX.DLL from:\n" + Path.GetDirectoryName(GetType().Assembly.Location));
  52. hFTD2XXDLL = LoadLibrary(@Path.GetDirectoryName(GetType().Assembly.Location) + "\\FTD2XX.DLL");
  53. }
  54. }
  55. // If we have succesfully loaded the library, get the function pointers set up
  56. if (hFTD2XXDLL != IntPtr.Zero)
  57. {
  58. // Set up our function pointers for use through our exported methods
  59. pFT_CreateDeviceInfoList = GetProcAddress(hFTD2XXDLL, "FT_CreateDeviceInfoList");
  60. pFT_GetDeviceInfoDetail = GetProcAddress(hFTD2XXDLL, "FT_GetDeviceInfoDetail");
  61. pFT_Open = GetProcAddress(hFTD2XXDLL, "FT_Open");
  62. pFT_OpenEx = GetProcAddress(hFTD2XXDLL, "FT_OpenEx");
  63. pFT_Close = GetProcAddress(hFTD2XXDLL, "FT_Close");
  64. pFT_Read = GetProcAddress(hFTD2XXDLL, "FT_Read");
  65. pFT_Write = GetProcAddress(hFTD2XXDLL, "FT_Write");
  66. pFT_GetQueueStatus = GetProcAddress(hFTD2XXDLL, "FT_GetQueueStatus");
  67. pFT_GetModemStatus = GetProcAddress(hFTD2XXDLL, "FT_GetModemStatus");
  68. pFT_GetStatus = GetProcAddress(hFTD2XXDLL, "FT_GetStatus");
  69. pFT_SetBaudRate = GetProcAddress(hFTD2XXDLL, "FT_SetBaudRate");
  70. pFT_SetDataCharacteristics = GetProcAddress(hFTD2XXDLL, "FT_SetDataCharacteristics");
  71. pFT_SetFlowControl = GetProcAddress(hFTD2XXDLL, "FT_SetFlowControl");
  72. pFT_SetDtr = GetProcAddress(hFTD2XXDLL, "FT_SetDtr");
  73. pFT_ClrDtr = GetProcAddress(hFTD2XXDLL, "FT_ClrDtr");
  74. pFT_SetRts = GetProcAddress(hFTD2XXDLL, "FT_SetRts");
  75. pFT_ClrRts = GetProcAddress(hFTD2XXDLL, "FT_ClrRts");
  76. pFT_ResetDevice = GetProcAddress(hFTD2XXDLL, "FT_ResetDevice");
  77. pFT_ResetPort = GetProcAddress(hFTD2XXDLL, "FT_ResetPort");
  78. pFT_CyclePort = GetProcAddress(hFTD2XXDLL, "FT_CyclePort");
  79. pFT_Rescan = GetProcAddress(hFTD2XXDLL, "FT_Rescan");
  80. pFT_Reload = GetProcAddress(hFTD2XXDLL, "FT_Reload");
  81. pFT_Purge = GetProcAddress(hFTD2XXDLL, "FT_Purge");
  82. pFT_SetTimeouts = GetProcAddress(hFTD2XXDLL, "FT_SetTimeouts");
  83. pFT_SetBreakOn = GetProcAddress(hFTD2XXDLL, "FT_SetBreakOn");
  84. pFT_SetBreakOff = GetProcAddress(hFTD2XXDLL, "FT_SetBreakOff");
  85. pFT_GetDeviceInfo = GetProcAddress(hFTD2XXDLL, "FT_GetDeviceInfo");
  86. pFT_SetResetPipeRetryCount = GetProcAddress(hFTD2XXDLL, "FT_SetResetPipeRetryCount");
  87. pFT_StopInTask = GetProcAddress(hFTD2XXDLL, "FT_StopInTask");
  88. pFT_RestartInTask = GetProcAddress(hFTD2XXDLL, "FT_RestartInTask");
  89. pFT_GetDriverVersion = GetProcAddress(hFTD2XXDLL, "FT_GetDriverVersion");
  90. pFT_GetLibraryVersion = GetProcAddress(hFTD2XXDLL, "FT_GetLibraryVersion");
  91. pFT_SetDeadmanTimeout = GetProcAddress(hFTD2XXDLL, "FT_SetDeadmanTimeout");
  92. pFT_SetChars = GetProcAddress(hFTD2XXDLL, "FT_SetChars");
  93. pFT_SetEventNotification = GetProcAddress(hFTD2XXDLL, "FT_SetEventNotification");
  94. pFT_GetComPortNumber = GetProcAddress(hFTD2XXDLL, "FT_GetComPortNumber");
  95. pFT_SetLatencyTimer = GetProcAddress(hFTD2XXDLL, "FT_SetLatencyTimer");
  96. pFT_GetLatencyTimer = GetProcAddress(hFTD2XXDLL, "FT_GetLatencyTimer");
  97. pFT_SetBitMode = GetProcAddress(hFTD2XXDLL, "FT_SetBitMode");
  98. pFT_GetBitMode = GetProcAddress(hFTD2XXDLL, "FT_GetBitMode");
  99. pFT_SetUSBParameters = GetProcAddress(hFTD2XXDLL, "FT_SetUSBParameters");
  100. pFT_ReadEE = GetProcAddress(hFTD2XXDLL, "FT_ReadEE");
  101. pFT_WriteEE = GetProcAddress(hFTD2XXDLL, "FT_WriteEE");
  102. pFT_EraseEE = GetProcAddress(hFTD2XXDLL, "FT_EraseEE");
  103. pFT_EE_UASize = GetProcAddress(hFTD2XXDLL, "FT_EE_UASize");
  104. pFT_EE_UARead = GetProcAddress(hFTD2XXDLL, "FT_EE_UARead");
  105. pFT_EE_UAWrite = GetProcAddress(hFTD2XXDLL, "FT_EE_UAWrite");
  106. pFT_EE_Read = GetProcAddress(hFTD2XXDLL, "FT_EE_Read");
  107. pFT_EE_Program = GetProcAddress(hFTD2XXDLL, "FT_EE_Program");
  108. pFT_EEPROM_Read = GetProcAddress(hFTD2XXDLL, "FT_EEPROM_Read");
  109. pFT_EEPROM_Program = GetProcAddress(hFTD2XXDLL, "FT_EEPROM_Program");
  110. }
  111. else
  112. {
  113. // Failed to load our DLL - alert the user
  114. MessageBox.Show("Failed to load FTD2XX.DLL. Are the FTDI drivers installed?");
  115. }
  116. }
  117. /// <summary>
  118. /// Destructor for the FTDI class.
  119. /// </summary>
  120. ~FTDI()
  121. {
  122. // FreeLibrary here - we should only do this if we are completely finished
  123. FreeLibrary(hFTD2XXDLL);
  124. hFTD2XXDLL = IntPtr.Zero;
  125. }
  126. #endregion
  127. #region LOAD_LIBRARIES
  128. /// <summary>
  129. /// Built-in Windows API functions to allow us to dynamically load our own DLL.
  130. /// Will allow us to use old versions of the DLL that do not have all of these functions available.
  131. /// </summary>
  132. [DllImport("kernel32.dll")]
  133. private static extern IntPtr LoadLibrary(string dllToLoad);
  134. [DllImport("kernel32.dll")]
  135. private static extern IntPtr GetProcAddress(IntPtr hModule, string procedureName);
  136. [DllImport("kernel32.dll")]
  137. private static extern bool FreeLibrary(IntPtr hModule);
  138. #endregion
  139. #region DELEGATES
  140. // Definitions for FTD2XX functions
  141. [UnmanagedFunctionPointer(CallingConvention.StdCall)]
  142. private delegate FT_STATUS tFT_CreateDeviceInfoList(ref UInt32 numdevs);
  143. [UnmanagedFunctionPointer(CallingConvention.StdCall)]
  144. private delegate FT_STATUS tFT_GetDeviceInfoDetail(UInt32 index, ref UInt32 flags, ref FT_DEVICE chiptype, ref UInt32 id, ref UInt32 locid, byte[] serialnumber, byte[] description, ref IntPtr ftHandle);
  145. [UnmanagedFunctionPointer(CallingConvention.StdCall)]
  146. private delegate FT_STATUS tFT_Open(UInt32 index, ref IntPtr ftHandle);
  147. [UnmanagedFunctionPointer(CallingConvention.StdCall)]
  148. private delegate FT_STATUS tFT_OpenEx(string devstring, UInt32 dwFlags, ref IntPtr ftHandle);
  149. [UnmanagedFunctionPointer(CallingConvention.StdCall)]
  150. private delegate FT_STATUS tFT_OpenExLoc(UInt32 devloc, UInt32 dwFlags, ref IntPtr ftHandle);
  151. [UnmanagedFunctionPointer(CallingConvention.StdCall)]
  152. private delegate FT_STATUS tFT_Close(IntPtr ftHandle);
  153. [UnmanagedFunctionPointer(CallingConvention.StdCall)]
  154. private delegate FT_STATUS tFT_Read(IntPtr ftHandle, byte[] lpBuffer, UInt32 dwBytesToRead, ref UInt32 lpdwBytesReturned);
  155. [UnmanagedFunctionPointer(CallingConvention.StdCall)]
  156. private delegate FT_STATUS tFT_Write(IntPtr ftHandle, byte[] lpBuffer, UInt32 dwBytesToWrite, ref UInt32 lpdwBytesWritten);
  157. [UnmanagedFunctionPointer(CallingConvention.StdCall)]
  158. private delegate FT_STATUS tFT_GetQueueStatus(IntPtr ftHandle, ref UInt32 lpdwAmountInRxQueue);
  159. [UnmanagedFunctionPointer(CallingConvention.StdCall)]
  160. private delegate FT_STATUS tFT_GetModemStatus(IntPtr ftHandle, ref UInt32 lpdwModemStatus);
  161. [UnmanagedFunctionPointer(CallingConvention.StdCall)]
  162. private delegate FT_STATUS tFT_GetStatus(IntPtr ftHandle, ref UInt32 lpdwAmountInRxQueue, ref UInt32 lpdwAmountInTxQueue, ref UInt32 lpdwEventStatus);
  163. [UnmanagedFunctionPointer(CallingConvention.StdCall)]
  164. private delegate FT_STATUS tFT_SetBaudRate(IntPtr ftHandle, UInt32 dwBaudRate);
  165. [UnmanagedFunctionPointer(CallingConvention.StdCall)]
  166. private delegate FT_STATUS tFT_SetDataCharacteristics(IntPtr ftHandle, byte uWordLength, byte uStopBits, byte uParity);
  167. [UnmanagedFunctionPointer(CallingConvention.StdCall)]
  168. private delegate FT_STATUS tFT_SetFlowControl(IntPtr ftHandle, UInt16 usFlowControl, byte uXon, byte uXoff);
  169. [UnmanagedFunctionPointer(CallingConvention.StdCall)]
  170. private delegate FT_STATUS tFT_SetDtr(IntPtr ftHandle);
  171. [UnmanagedFunctionPointer(CallingConvention.StdCall)]
  172. private delegate FT_STATUS tFT_ClrDtr(IntPtr ftHandle);
  173. [UnmanagedFunctionPointer(CallingConvention.StdCall)]
  174. private delegate FT_STATUS tFT_SetRts(IntPtr ftHandle);
  175. [UnmanagedFunctionPointer(CallingConvention.StdCall)]
  176. private delegate FT_STATUS tFT_ClrRts(IntPtr ftHandle);
  177. [UnmanagedFunctionPointer(CallingConvention.StdCall)]
  178. private delegate FT_STATUS tFT_ResetDevice(IntPtr ftHandle);
  179. [UnmanagedFunctionPointer(CallingConvention.StdCall)]
  180. private delegate FT_STATUS tFT_ResetPort(IntPtr ftHandle);
  181. [UnmanagedFunctionPointer(CallingConvention.StdCall)]
  182. private delegate FT_STATUS tFT_CyclePort(IntPtr ftHandle);
  183. [UnmanagedFunctionPointer(CallingConvention.StdCall)]
  184. private delegate FT_STATUS tFT_Rescan();
  185. [UnmanagedFunctionPointer(CallingConvention.StdCall)]
  186. private delegate FT_STATUS tFT_Reload(UInt16 wVID, UInt16 wPID);
  187. [UnmanagedFunctionPointer(CallingConvention.StdCall)]
  188. private delegate FT_STATUS tFT_Purge(IntPtr ftHandle, UInt32 dwMask);
  189. [UnmanagedFunctionPointer(CallingConvention.StdCall)]
  190. private delegate FT_STATUS tFT_SetTimeouts(IntPtr ftHandle, UInt32 dwReadTimeout, UInt32 dwWriteTimeout);
  191. [UnmanagedFunctionPointer(CallingConvention.StdCall)]
  192. private delegate FT_STATUS tFT_SetBreakOn(IntPtr ftHandle);
  193. [UnmanagedFunctionPointer(CallingConvention.StdCall)]
  194. private delegate FT_STATUS tFT_SetBreakOff(IntPtr ftHandle);
  195. [UnmanagedFunctionPointer(CallingConvention.StdCall)]
  196. private delegate FT_STATUS tFT_GetDeviceInfo(IntPtr ftHandle, ref FT_DEVICE pftType, ref UInt32 lpdwID, byte[] pcSerialNumber, byte[] pcDescription, IntPtr pvDummy);
  197. [UnmanagedFunctionPointer(CallingConvention.StdCall)]
  198. private delegate FT_STATUS tFT_SetResetPipeRetryCount(IntPtr ftHandle, UInt32 dwCount);
  199. [UnmanagedFunctionPointer(CallingConvention.StdCall)]
  200. private delegate FT_STATUS tFT_StopInTask(IntPtr ftHandle);
  201. [UnmanagedFunctionPointer(CallingConvention.StdCall)]
  202. private delegate FT_STATUS tFT_RestartInTask(IntPtr ftHandle);
  203. [UnmanagedFunctionPointer(CallingConvention.StdCall)]
  204. private delegate FT_STATUS tFT_GetDriverVersion(IntPtr ftHandle, ref UInt32 lpdwDriverVersion);
  205. [UnmanagedFunctionPointer(CallingConvention.StdCall)]
  206. private delegate FT_STATUS tFT_GetLibraryVersion(ref UInt32 lpdwLibraryVersion);
  207. [UnmanagedFunctionPointer(CallingConvention.StdCall)]
  208. private delegate FT_STATUS tFT_SetDeadmanTimeout(IntPtr ftHandle, UInt32 dwDeadmanTimeout);
  209. [UnmanagedFunctionPointer(CallingConvention.StdCall)]
  210. private delegate FT_STATUS tFT_SetChars(IntPtr ftHandle, byte uEventCh, byte uEventChEn, byte uErrorCh, byte uErrorChEn);
  211. [UnmanagedFunctionPointer(CallingConvention.StdCall)]
  212. private delegate FT_STATUS tFT_SetEventNotification(IntPtr ftHandle, UInt32 dwEventMask, SafeHandle hEvent);
  213. [UnmanagedFunctionPointer(CallingConvention.StdCall)]
  214. private delegate FT_STATUS tFT_GetComPortNumber(IntPtr ftHandle, ref Int32 dwComPortNumber);
  215. [UnmanagedFunctionPointer(CallingConvention.StdCall)]
  216. private delegate FT_STATUS tFT_SetLatencyTimer(IntPtr ftHandle, byte ucLatency);
  217. [UnmanagedFunctionPointer(CallingConvention.StdCall)]
  218. private delegate FT_STATUS tFT_GetLatencyTimer(IntPtr ftHandle, ref byte ucLatency);
  219. [UnmanagedFunctionPointer(CallingConvention.StdCall)]
  220. private delegate FT_STATUS tFT_SetBitMode(IntPtr ftHandle, byte ucMask, byte ucMode);
  221. [UnmanagedFunctionPointer(CallingConvention.StdCall)]
  222. private delegate FT_STATUS tFT_GetBitMode(IntPtr ftHandle, ref byte ucMode);
  223. [UnmanagedFunctionPointer(CallingConvention.StdCall)]
  224. private delegate FT_STATUS tFT_SetUSBParameters(IntPtr ftHandle, UInt32 dwInTransferSize, UInt32 dwOutTransferSize);
  225. [UnmanagedFunctionPointer(CallingConvention.StdCall)]
  226. private delegate FT_STATUS tFT_ReadEE(IntPtr ftHandle, UInt32 dwWordOffset, ref UInt16 lpwValue);
  227. [UnmanagedFunctionPointer(CallingConvention.StdCall)]
  228. private delegate FT_STATUS tFT_WriteEE(IntPtr ftHandle, UInt32 dwWordOffset, UInt16 wValue);
  229. [UnmanagedFunctionPointer(CallingConvention.StdCall)]
  230. private delegate FT_STATUS tFT_EraseEE(IntPtr ftHandle);
  231. [UnmanagedFunctionPointer(CallingConvention.StdCall)]
  232. private delegate FT_STATUS tFT_EE_UASize(IntPtr ftHandle, ref UInt32 dwSize);
  233. [UnmanagedFunctionPointer(CallingConvention.StdCall)]
  234. private delegate FT_STATUS tFT_EE_UARead(IntPtr ftHandle, byte[] pucData, Int32 dwDataLen, ref UInt32 lpdwDataRead);
  235. [UnmanagedFunctionPointer(CallingConvention.StdCall)]
  236. private delegate FT_STATUS tFT_EE_UAWrite(IntPtr ftHandle, byte[] pucData, Int32 dwDataLen);
  237. [UnmanagedFunctionPointer(CallingConvention.StdCall)]
  238. private delegate FT_STATUS tFT_EE_Read(IntPtr ftHandle, FT_PROGRAM_DATA pData);
  239. [UnmanagedFunctionPointer(CallingConvention.StdCall)]
  240. private delegate FT_STATUS tFT_EE_Program(IntPtr ftHandle, FT_PROGRAM_DATA pData);
  241. [UnmanagedFunctionPointer(CallingConvention.StdCall)]
  242. private delegate FT_STATUS tFT_EEPROM_Read(IntPtr ftHandle, IntPtr eepromData, UInt32 eepromDataSize, byte[] manufacturer, byte[] manufacturerID, byte[] description, byte[] serialnumber);
  243. [UnmanagedFunctionPointer(CallingConvention.StdCall)]
  244. private delegate FT_STATUS tFT_EEPROM_Program(IntPtr ftHandle, IntPtr eepromData, UInt32 eepromDataSize, byte[] manufacturer, byte[] manufacturerID, byte[] description, byte[] serialnumber);
  245. #endregion
  246. #region CONSTANT_VALUES
  247. // Constants for FT_STATUS
  248. /// <summary>
  249. /// Status values for FTDI devices.
  250. /// </summary>
  251. public enum FT_STATUS
  252. {
  253. /// <summary>
  254. /// Status OK
  255. /// </summary>
  256. FT_OK = 0,
  257. /// <summary>
  258. /// The device handle is invalid
  259. /// </summary>
  260. FT_INVALID_HANDLE,
  261. /// <summary>
  262. /// Device not found
  263. /// </summary>
  264. FT_DEVICE_NOT_FOUND,
  265. /// <summary>
  266. /// Device is not open
  267. /// </summary>
  268. FT_DEVICE_NOT_OPENED,
  269. /// <summary>
  270. /// IO error
  271. /// </summary>
  272. FT_IO_ERROR,
  273. /// <summary>
  274. /// Insufficient resources
  275. /// </summary>
  276. FT_INSUFFICIENT_RESOURCES,
  277. /// <summary>
  278. /// A parameter was invalid
  279. /// </summary>
  280. FT_INVALID_PARAMETER,
  281. /// <summary>
  282. /// The requested baud rate is invalid
  283. /// </summary>
  284. FT_INVALID_BAUD_RATE,
  285. /// <summary>
  286. /// Device not opened for erase
  287. /// </summary>
  288. FT_DEVICE_NOT_OPENED_FOR_ERASE,
  289. /// <summary>
  290. /// Device not poened for write
  291. /// </summary>
  292. FT_DEVICE_NOT_OPENED_FOR_WRITE,
  293. /// <summary>
  294. /// Failed to write to device
  295. /// </summary>
  296. FT_FAILED_TO_WRITE_DEVICE,
  297. /// <summary>
  298. /// Failed to read the device EEPROM
  299. /// </summary>
  300. FT_EEPROM_READ_FAILED,
  301. /// <summary>
  302. /// Failed to write the device EEPROM
  303. /// </summary>
  304. FT_EEPROM_WRITE_FAILED,
  305. /// <summary>
  306. /// Failed to erase the device EEPROM
  307. /// </summary>
  308. FT_EEPROM_ERASE_FAILED,
  309. /// <summary>
  310. /// An EEPROM is not fitted to the device
  311. /// </summary>
  312. FT_EEPROM_NOT_PRESENT,
  313. /// <summary>
  314. /// Device EEPROM is blank
  315. /// </summary>
  316. FT_EEPROM_NOT_PROGRAMMED,
  317. /// <summary>
  318. /// Invalid arguments
  319. /// </summary>
  320. FT_INVALID_ARGS,
  321. /// <summary>
  322. /// An other error has occurred
  323. /// </summary>
  324. FT_OTHER_ERROR
  325. };
  326. // Constants for other error states internal to this class library
  327. /// <summary>
  328. /// Error states not supported by FTD2XX DLL.
  329. /// </summary>
  330. private enum FT_ERROR
  331. {
  332. FT_NO_ERROR = 0,
  333. FT_INCORRECT_DEVICE,
  334. FT_INVALID_BITMODE,
  335. FT_BUFFER_SIZE
  336. };
  337. // Flags for FT_OpenEx
  338. private const UInt32 FT_OPEN_BY_SERIAL_NUMBER = 0x00000001;
  339. private const UInt32 FT_OPEN_BY_DESCRIPTION = 0x00000002;
  340. private const UInt32 FT_OPEN_BY_LOCATION = 0x00000004;
  341. // Word Lengths
  342. /// <summary>
  343. /// Permitted data bits for FTDI devices
  344. /// </summary>
  345. public class FT_DATA_BITS
  346. {
  347. /// <summary>
  348. /// 8 data bits
  349. /// </summary>
  350. public const byte FT_BITS_8 = 0x08;
  351. /// <summary>
  352. /// 7 data bits
  353. /// </summary>
  354. public const byte FT_BITS_7 = 0x07;
  355. }
  356. // Stop Bits
  357. /// <summary>
  358. /// Permitted stop bits for FTDI devices
  359. /// </summary>
  360. public class FT_STOP_BITS
  361. {
  362. /// <summary>
  363. /// 1 stop bit
  364. /// </summary>
  365. public const byte FT_STOP_BITS_1 = 0x00;
  366. /// <summary>
  367. /// 2 stop bits
  368. /// </summary>
  369. public const byte FT_STOP_BITS_2 = 0x02;
  370. }
  371. // Parity
  372. /// <summary>
  373. /// Permitted parity values for FTDI devices
  374. /// </summary>
  375. public class FT_PARITY
  376. {
  377. /// <summary>
  378. /// No parity
  379. /// </summary>
  380. public const byte FT_PARITY_NONE = 0x00;
  381. /// <summary>
  382. /// Odd parity
  383. /// </summary>
  384. public const byte FT_PARITY_ODD = 0x01;
  385. /// <summary>
  386. /// Even parity
  387. /// </summary>
  388. public const byte FT_PARITY_EVEN = 0x02;
  389. /// <summary>
  390. /// Mark parity
  391. /// </summary>
  392. public const byte FT_PARITY_MARK = 0x03;
  393. /// <summary>
  394. /// Space parity
  395. /// </summary>
  396. public const byte FT_PARITY_SPACE = 0x04;
  397. }
  398. // Flow Control
  399. /// <summary>
  400. /// Permitted flow control values for FTDI devices
  401. /// </summary>
  402. public class FT_FLOW_CONTROL
  403. {
  404. /// <summary>
  405. /// No flow control
  406. /// </summary>
  407. public const UInt16 FT_FLOW_NONE = 0x0000;
  408. /// <summary>
  409. /// RTS/CTS flow control
  410. /// </summary>
  411. public const UInt16 FT_FLOW_RTS_CTS = 0x0100;
  412. /// <summary>
  413. /// DTR/DSR flow control
  414. /// </summary>
  415. public const UInt16 FT_FLOW_DTR_DSR = 0x0200;
  416. /// <summary>
  417. /// Xon/Xoff flow control
  418. /// </summary>
  419. public const UInt16 FT_FLOW_XON_XOFF = 0x0400;
  420. }
  421. // Purge Rx and Tx buffers
  422. /// <summary>
  423. /// Purge buffer constant definitions
  424. /// </summary>
  425. public class FT_PURGE
  426. {
  427. /// <summary>
  428. /// Purge Rx buffer
  429. /// </summary>
  430. public const byte FT_PURGE_RX = 0x01;
  431. /// <summary>
  432. /// Purge Tx buffer
  433. /// </summary>
  434. public const byte FT_PURGE_TX = 0x02;
  435. }
  436. // Modem Status bits
  437. /// <summary>
  438. /// Modem status bit definitions
  439. /// </summary>
  440. public class FT_MODEM_STATUS
  441. {
  442. /// <summary>
  443. /// Clear To Send (CTS) modem status
  444. /// </summary>
  445. public const byte FT_CTS = 0x10;
  446. /// <summary>
  447. /// Data Set Ready (DSR) modem status
  448. /// </summary>
  449. public const byte FT_DSR = 0x20;
  450. /// <summary>
  451. /// Ring Indicator (RI) modem status
  452. /// </summary>
  453. public const byte FT_RI = 0x40;
  454. /// <summary>
  455. /// Data Carrier Detect (DCD) modem status
  456. /// </summary>
  457. public const byte FT_DCD = 0x80;
  458. }
  459. // Line Status bits
  460. /// <summary>
  461. /// Line status bit definitions
  462. /// </summary>
  463. public class FT_LINE_STATUS
  464. {
  465. /// <summary>
  466. /// Overrun Error (OE) line status
  467. /// </summary>
  468. public const byte FT_OE = 0x02;
  469. /// <summary>
  470. /// Parity Error (PE) line status
  471. /// </summary>
  472. public const byte FT_PE = 0x04;
  473. /// <summary>
  474. /// Framing Error (FE) line status
  475. /// </summary>
  476. public const byte FT_FE = 0x08;
  477. /// <summary>
  478. /// Break Interrupt (BI) line status
  479. /// </summary>
  480. public const byte FT_BI = 0x10;
  481. }
  482. // Events
  483. /// <summary>
  484. /// FTDI device event types that can be monitored
  485. /// </summary>
  486. public class FT_EVENTS
  487. {
  488. /// <summary>
  489. /// Event on receive character
  490. /// </summary>
  491. public const UInt32 FT_EVENT_RXCHAR = 0x00000001;
  492. /// <summary>
  493. /// Event on modem status change
  494. /// </summary>
  495. public const UInt32 FT_EVENT_MODEM_STATUS = 0x00000002;
  496. /// <summary>
  497. /// Event on line status change
  498. /// </summary>
  499. public const UInt32 FT_EVENT_LINE_STATUS = 0x00000004;
  500. }
  501. // Bit modes
  502. /// <summary>
  503. /// Permitted bit mode values for FTDI devices. For use with SetBitMode
  504. /// </summary>
  505. public class FT_BIT_MODES
  506. {
  507. /// <summary>
  508. /// Reset bit mode
  509. /// </summary>
  510. public const byte FT_BIT_MODE_RESET = 0x00;
  511. /// <summary>
  512. /// Asynchronous bit-bang mode
  513. /// </summary>
  514. public const byte FT_BIT_MODE_ASYNC_BITBANG = 0x01;
  515. /// <summary>
  516. /// MPSSE bit mode - only available on FT2232, FT2232H, FT4232H and FT232H
  517. /// </summary>
  518. public const byte FT_BIT_MODE_MPSSE = 0x02;
  519. /// <summary>
  520. /// Synchronous bit-bang mode
  521. /// </summary>
  522. public const byte FT_BIT_MODE_SYNC_BITBANG = 0x04;
  523. /// <summary>
  524. /// MCU host bus emulation mode - only available on FT2232, FT2232H, FT4232H and FT232H
  525. /// </summary>
  526. public const byte FT_BIT_MODE_MCU_HOST = 0x08;
  527. /// <summary>
  528. /// Fast opto-isolated serial mode - only available on FT2232, FT2232H, FT4232H and FT232H
  529. /// </summary>
  530. public const byte FT_BIT_MODE_FAST_SERIAL = 0x10;
  531. /// <summary>
  532. /// CBUS bit-bang mode - only available on FT232R and FT232H
  533. /// </summary>
  534. public const byte FT_BIT_MODE_CBUS_BITBANG = 0x20;
  535. /// <summary>
  536. /// Single channel synchronous 245 FIFO mode - only available on FT2232H channel A and FT232H
  537. /// </summary>
  538. public const byte FT_BIT_MODE_SYNC_FIFO = 0x40;
  539. }
  540. // FT232R CBUS Options
  541. /// <summary>
  542. /// Available functions for the FT232R CBUS pins. Controlled by FT232R EEPROM settings
  543. /// </summary>
  544. public class FT_CBUS_OPTIONS
  545. {
  546. /// <summary>
  547. /// FT232R CBUS EEPROM options - Tx Data Enable
  548. /// </summary>
  549. public const byte FT_CBUS_TXDEN = 0x00;
  550. /// <summary>
  551. /// FT232R CBUS EEPROM options - Power On
  552. /// </summary>
  553. public const byte FT_CBUS_PWRON = 0x01;
  554. /// <summary>
  555. /// FT232R CBUS EEPROM options - Rx LED
  556. /// </summary>
  557. public const byte FT_CBUS_RXLED = 0x02;
  558. /// <summary>
  559. /// FT232R CBUS EEPROM options - Tx LED
  560. /// </summary>
  561. public const byte FT_CBUS_TXLED = 0x03;
  562. /// <summary>
  563. /// FT232R CBUS EEPROM options - Tx and Rx LED
  564. /// </summary>
  565. public const byte FT_CBUS_TXRXLED = 0x04;
  566. /// <summary>
  567. /// FT232R CBUS EEPROM options - Sleep
  568. /// </summary>
  569. public const byte FT_CBUS_SLEEP = 0x05;
  570. /// <summary>
  571. /// FT232R CBUS EEPROM options - 48MHz clock
  572. /// </summary>
  573. public const byte FT_CBUS_CLK48 = 0x06;
  574. /// <summary>
  575. /// FT232R CBUS EEPROM options - 24MHz clock
  576. /// </summary>
  577. public const byte FT_CBUS_CLK24 = 0x07;
  578. /// <summary>
  579. /// FT232R CBUS EEPROM options - 12MHz clock
  580. /// </summary>
  581. public const byte FT_CBUS_CLK12 = 0x08;
  582. /// <summary>
  583. /// FT232R CBUS EEPROM options - 6MHz clock
  584. /// </summary>
  585. public const byte FT_CBUS_CLK6 = 0x09;
  586. /// <summary>
  587. /// FT232R CBUS EEPROM options - IO mode
  588. /// </summary>
  589. public const byte FT_CBUS_IOMODE = 0x0A;
  590. /// <summary>
  591. /// FT232R CBUS EEPROM options - Bit-bang write strobe
  592. /// </summary>
  593. public const byte FT_CBUS_BITBANG_WR = 0x0B;
  594. /// <summary>
  595. /// FT232R CBUS EEPROM options - Bit-bang read strobe
  596. /// </summary>
  597. public const byte FT_CBUS_BITBANG_RD = 0x0C;
  598. }
  599. // FT232H CBUS Options
  600. /// <summary>
  601. /// Available functions for the FT232H CBUS pins. Controlled by FT232H EEPROM settings
  602. /// </summary>
  603. public class FT_232H_CBUS_OPTIONS
  604. {
  605. /// <summary>
  606. /// FT232H CBUS EEPROM options - Tristate
  607. /// </summary>
  608. public const byte FT_CBUS_TRISTATE = 0x00;
  609. /// <summary>
  610. /// FT232H CBUS EEPROM options - Rx LED
  611. /// </summary>
  612. public const byte FT_CBUS_RXLED = 0x01;
  613. /// <summary>
  614. /// FT232H CBUS EEPROM options - Tx LED
  615. /// </summary>
  616. public const byte FT_CBUS_TXLED = 0x02;
  617. /// <summary>
  618. /// FT232H CBUS EEPROM options - Tx and Rx LED
  619. /// </summary>
  620. public const byte FT_CBUS_TXRXLED = 0x03;
  621. /// <summary>
  622. /// FT232H CBUS EEPROM options - Power Enable
  623. /// </summary>
  624. public const byte FT_CBUS_PWREN = 0x04;
  625. /// <summary>
  626. /// FT232H CBUS EEPROM options - Sleep
  627. /// </summary>
  628. public const byte FT_CBUS_SLEEP = 0x05;
  629. /// <summary>
  630. /// FT232H CBUS EEPROM options - Drive pin to logic 0
  631. /// </summary>
  632. public const byte FT_CBUS_DRIVE_0 = 0x06;
  633. /// <summary>
  634. /// FT232H CBUS EEPROM options - Drive pin to logic 1
  635. /// </summary>
  636. public const byte FT_CBUS_DRIVE_1 = 0x07;
  637. /// <summary>
  638. /// FT232H CBUS EEPROM options - IO Mode
  639. /// </summary>
  640. public const byte FT_CBUS_IOMODE = 0x08;
  641. /// <summary>
  642. /// FT232H CBUS EEPROM options - Tx Data Enable
  643. /// </summary>
  644. public const byte FT_CBUS_TXDEN = 0x09;
  645. /// <summary>
  646. /// FT232H CBUS EEPROM options - 30MHz clock
  647. /// </summary>
  648. public const byte FT_CBUS_CLK30 = 0x0A;
  649. /// <summary>
  650. /// FT232H CBUS EEPROM options - 15MHz clock
  651. /// </summary>
  652. public const byte FT_CBUS_CLK15 = 0x0B;/// <summary>
  653. /// FT232H CBUS EEPROM options - 7.5MHz clock
  654. /// </summary>
  655. public const byte FT_CBUS_CLK7_5 = 0x0C;
  656. }
  657. /// <summary>
  658. /// Available functions for the X-Series CBUS pins. Controlled by X-Series EEPROM settings
  659. /// </summary>
  660. public class FT_XSERIES_CBUS_OPTIONS
  661. {
  662. /// <summary>
  663. /// FT X-Series CBUS EEPROM options - Tristate
  664. /// </summary>
  665. public const byte FT_CBUS_TRISTATE = 0x00;
  666. /// <summary>
  667. /// FT X-Series CBUS EEPROM options - RxLED#
  668. /// </summary>
  669. public const byte FT_CBUS_RXLED = 0x01;
  670. /// <summary>
  671. /// FT X-Series CBUS EEPROM options - TxLED#
  672. /// </summary>
  673. public const byte FT_CBUS_TXLED = 0x02;
  674. /// <summary>
  675. /// FT X-Series CBUS EEPROM options - TxRxLED
  676. /// </summary>
  677. public const byte FT_CBUS_TXRXLED = 0x03;
  678. /// <summary>
  679. /// FT X-Series CBUS EEPROM options - PwrEn#
  680. /// </summary>
  681. public const byte FT_CBUS_PWREN = 0x04;
  682. /// <summary>
  683. /// FT X-Series CBUS EEPROM options - Sleep#
  684. /// </summary>
  685. public const byte FT_CBUS_SLEEP = 0x05;
  686. /// <summary>
  687. /// FT X-Series CBUS EEPROM options - Drive_0
  688. /// </summary>
  689. public const byte FT_CBUS_Drive_0 = 0x06;
  690. /// <summary>
  691. /// FT X-Series CBUS EEPROM options - Drive_1
  692. /// </summary>
  693. public const byte FT_CBUS_Drive_1 = 0x07;
  694. /// <summary>
  695. /// FT X-Series CBUS EEPROM options - GPIO
  696. /// </summary>
  697. public const byte FT_CBUS_GPIO = 0x08;
  698. /// <summary>
  699. /// FT X-Series CBUS EEPROM options - TxdEn
  700. /// </summary>
  701. public const byte FT_CBUS_TXDEN = 0x09;
  702. /// <summary>
  703. /// FT X-Series CBUS EEPROM options - Clk24MHz
  704. /// </summary>
  705. public const byte FT_CBUS_CLK24MHz = 0x0A;
  706. /// <summary>
  707. /// FT X-Series CBUS EEPROM options - Clk12MHz
  708. /// </summary>
  709. public const byte FT_CBUS_CLK12MHz = 0x0B;
  710. /// <summary>
  711. /// FT X-Series CBUS EEPROM options - Clk6MHz
  712. /// </summary>
  713. public const byte FT_CBUS_CLK6MHz = 0x0C;
  714. /// <summary>
  715. /// FT X-Series CBUS EEPROM options - BCD_Charger
  716. /// </summary>
  717. public const byte FT_CBUS_BCD_Charger = 0x0D;
  718. /// <summary>
  719. /// FT X-Series CBUS EEPROM options - BCD_Charger#
  720. /// </summary>
  721. public const byte FT_CBUS_BCD_Charger_N = 0x0E;
  722. /// <summary>
  723. /// FT X-Series CBUS EEPROM options - I2C_TXE#
  724. /// </summary>
  725. public const byte FT_CBUS_I2C_TXE = 0x0F;
  726. /// <summary>
  727. /// FT X-Series CBUS EEPROM options - I2C_RXF#
  728. /// </summary>
  729. public const byte FT_CBUS_I2C_RXF = 0x10;
  730. /// <summary>
  731. /// FT X-Series CBUS EEPROM options - VBUS_Sense
  732. /// </summary>
  733. public const byte FT_CBUS_VBUS_Sense = 0x11;
  734. /// <summary>
  735. /// FT X-Series CBUS EEPROM options - BitBang_WR#
  736. /// </summary>
  737. public const byte FT_CBUS_BitBang_WR = 0x12;
  738. /// <summary>
  739. /// FT X-Series CBUS EEPROM options - BitBang_RD#
  740. /// </summary>
  741. public const byte FT_CBUS_BitBang_RD = 0x13;
  742. /// <summary>
  743. /// FT X-Series CBUS EEPROM options - Time_Stampe
  744. /// </summary>
  745. public const byte FT_CBUS_Time_Stamp = 0x14;
  746. /// <summary>
  747. /// FT X-Series CBUS EEPROM options - Keep_Awake#
  748. /// </summary>
  749. public const byte FT_CBUS_Keep_Awake = 0x15;
  750. }
  751. // Flag values for FT_GetDeviceInfoDetail and FT_GetDeviceInfo
  752. /// <summary>
  753. /// Flags that provide information on the FTDI device state
  754. /// </summary>
  755. public class FT_FLAGS
  756. {
  757. /// <summary>
  758. /// Indicates that the device is open
  759. /// </summary>
  760. public const UInt32 FT_FLAGS_OPENED = 0x00000001;
  761. /// <summary>
  762. /// Indicates that the device is enumerated as a hi-speed USB device
  763. /// </summary>
  764. public const UInt32 FT_FLAGS_HISPEED = 0x00000002;
  765. }
  766. // Valid drive current values for FT2232H, FT4232H and FT232H devices
  767. /// <summary>
  768. /// Valid values for drive current options on FT2232H, FT4232H and FT232H devices.
  769. /// </summary>
  770. public class FT_DRIVE_CURRENT
  771. {
  772. /// <summary>
  773. /// 4mA drive current
  774. /// </summary>
  775. public const byte FT_DRIVE_CURRENT_4MA = 4;
  776. /// <summary>
  777. /// 8mA drive current
  778. /// </summary>
  779. public const byte FT_DRIVE_CURRENT_8MA = 8;
  780. /// <summary>
  781. /// 12mA drive current
  782. /// </summary>
  783. public const byte FT_DRIVE_CURRENT_12MA = 12;
  784. /// <summary>
  785. /// 16mA drive current
  786. /// </summary>
  787. public const byte FT_DRIVE_CURRENT_16MA = 16;
  788. }
  789. // Device type identifiers for FT_GetDeviceInfoDetail and FT_GetDeviceInfo
  790. /// <summary>
  791. /// List of FTDI device types
  792. /// </summary>
  793. public enum FT_DEVICE
  794. {
  795. /// <summary>
  796. /// FT232B or FT245B device
  797. /// </summary>
  798. FT_DEVICE_BM = 0,
  799. /// <summary>
  800. /// FT8U232AM or FT8U245AM device
  801. /// </summary>
  802. FT_DEVICE_AM,
  803. /// <summary>
  804. /// FT8U100AX device
  805. /// </summary>
  806. FT_DEVICE_100AX,
  807. /// <summary>
  808. /// Unknown device
  809. /// </summary>
  810. FT_DEVICE_UNKNOWN,
  811. /// <summary>
  812. /// FT2232 device
  813. /// </summary>
  814. FT_DEVICE_2232,
  815. /// <summary>
  816. /// FT232R or FT245R device
  817. /// </summary>
  818. FT_DEVICE_232R,
  819. /// <summary>
  820. /// FT2232H device
  821. /// </summary>
  822. FT_DEVICE_2232H,
  823. /// <summary>
  824. /// FT4232H device
  825. /// </summary>
  826. FT_DEVICE_4232H,
  827. /// <summary>
  828. /// FT232H device
  829. /// </summary>
  830. FT_DEVICE_232H,
  831. /// <summary>
  832. /// FT232X device
  833. /// </summary>
  834. FT_DEVICE_X_SERIES
  835. };
  836. #endregion
  837. #region DEFAULT_VALUES
  838. private const UInt32 FT_DEFAULT_BAUD_RATE = 9600;
  839. private const UInt32 FT_DEFAULT_DEADMAN_TIMEOUT = 5000;
  840. private const Int32 FT_COM_PORT_NOT_ASSIGNED = -1;
  841. private const UInt32 FT_DEFAULT_IN_TRANSFER_SIZE = 0x1000;
  842. private const UInt32 FT_DEFAULT_OUT_TRANSFER_SIZE = 0x1000;
  843. private const byte FT_DEFAULT_LATENCY = 16;
  844. private const UInt32 FT_DEFAULT_DEVICE_ID = 0x04036001;
  845. #endregion
  846. #region VARIABLES
  847. // Create private variables for the device within the class
  848. private IntPtr ftHandle = IntPtr.Zero;
  849. #endregion
  850. #region TYPEDEFS
  851. /// <summary>
  852. /// Type that holds device information for GetDeviceInformation method.
  853. /// Used with FT_GetDeviceInfo and FT_GetDeviceInfoDetail in FTD2XX.DLL
  854. /// </summary>
  855. public class FT_DEVICE_INFO_NODE
  856. {
  857. /// <summary>
  858. /// Indicates device state. Can be any combination of the following: FT_FLAGS_OPENED, FT_FLAGS_HISPEED
  859. /// </summary>
  860. public UInt32 Flags;
  861. /// <summary>
  862. /// Indicates the device type. Can be one of the following: FT_DEVICE_232R, FT_DEVICE_2232C, FT_DEVICE_BM, FT_DEVICE_AM, FT_DEVICE_100AX or FT_DEVICE_UNKNOWN
  863. /// </summary>
  864. public FT_DEVICE Type;
  865. /// <summary>
  866. /// The Vendor ID and Product ID of the device
  867. /// </summary>
  868. public UInt32 ID;
  869. /// <summary>
  870. /// The physical location identifier of the device
  871. /// </summary>
  872. public UInt32 LocId;
  873. /// <summary>
  874. /// The device serial number
  875. /// </summary>
  876. public string SerialNumber;
  877. /// <summary>
  878. /// The device description
  879. /// </summary>
  880. public string Description;
  881. /// <summary>
  882. /// The device handle. This value is not used externally and is provided for information only.
  883. /// If the device is not open, this value is 0.
  884. /// </summary>
  885. public IntPtr ftHandle;
  886. }
  887. #endregion
  888. #region EEPROM_STRUCTURES
  889. // Internal structure for reading and writing EEPROM contents
  890. // NOTE: NEED Pack=1 for byte alignment! Without this, data is garbage
  891. [StructLayout(LayoutKind.Sequential, Pack = 4)]
  892. private class FT_PROGRAM_DATA
  893. {
  894. public UInt32 Signature1;
  895. public UInt32 Signature2;
  896. public UInt32 Version;
  897. public UInt16 VendorID;
  898. public UInt16 ProductID;
  899. public IntPtr Manufacturer;
  900. public IntPtr ManufacturerID;
  901. public IntPtr Description;
  902. public IntPtr SerialNumber;
  903. public UInt16 MaxPower;
  904. public UInt16 PnP;
  905. public UInt16 SelfPowered;
  906. public UInt16 RemoteWakeup;
  907. // FT232B extensions
  908. public byte Rev4;
  909. public byte IsoIn;
  910. public byte IsoOut;
  911. public byte PullDownEnable;
  912. public byte SerNumEnable;
  913. public byte USBVersionEnable;
  914. public UInt16 USBVersion;
  915. // FT2232D extensions
  916. public byte Rev5;
  917. public byte IsoInA;
  918. public byte IsoInB;
  919. public byte IsoOutA;
  920. public byte IsoOutB;
  921. public byte PullDownEnable5;
  922. public byte SerNumEnable5;
  923. public byte USBVersionEnable5;
  924. public UInt16 USBVersion5;
  925. public byte AIsHighCurrent;
  926. public byte BIsHighCurrent;
  927. public byte IFAIsFifo;
  928. public byte IFAIsFifoTar;
  929. public byte IFAIsFastSer;
  930. public byte AIsVCP;
  931. public byte IFBIsFifo;
  932. public byte IFBIsFifoTar;
  933. public byte IFBIsFastSer;
  934. public byte BIsVCP;
  935. // FT232R extensions
  936. public byte UseExtOsc;
  937. public byte HighDriveIOs;
  938. public byte EndpointSize;
  939. public byte PullDownEnableR;
  940. public byte SerNumEnableR;
  941. public byte InvertTXD; // non-zero if invert TXD
  942. public byte InvertRXD; // non-zero if invert RXD
  943. public byte InvertRTS; // non-zero if invert RTS
  944. public byte InvertCTS; // non-zero if invert CTS
  945. public byte InvertDTR; // non-zero if invert DTR
  946. public byte InvertDSR; // non-zero if invert DSR
  947. public byte InvertDCD; // non-zero if invert DCD
  948. public byte InvertRI; // non-zero if invert RI
  949. public byte Cbus0; // Cbus Mux control - Ignored for FT245R
  950. public byte Cbus1; // Cbus Mux control - Ignored for FT245R
  951. public byte Cbus2; // Cbus Mux control - Ignored for FT245R
  952. public byte Cbus3; // Cbus Mux control - Ignored for FT245R
  953. public byte Cbus4; // Cbus Mux control - Ignored for FT245R
  954. public byte RIsD2XX; // Default to loading VCP
  955. // FT2232H extensions
  956. public byte PullDownEnable7;
  957. public byte SerNumEnable7;
  958. public byte ALSlowSlew; // non-zero if AL pins have slow slew
  959. public byte ALSchmittInput; // non-zero if AL pins are Schmitt input
  960. public byte ALDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA
  961. public byte AHSlowSlew; // non-zero if AH pins have slow slew
  962. public byte AHSchmittInput; // non-zero if AH pins are Schmitt input
  963. public byte AHDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA
  964. public byte BLSlowSlew; // non-zero if BL pins have slow slew
  965. public byte BLSchmittInput; // non-zero if BL pins are Schmitt input
  966. public byte BLDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA
  967. public byte BHSlowSlew; // non-zero if BH pins have slow slew
  968. public byte BHSchmittInput; // non-zero if BH pins are Schmitt input
  969. public byte BHDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA
  970. public byte IFAIsFifo7; // non-zero if interface is 245 FIFO
  971. public byte IFAIsFifoTar7; // non-zero if interface is 245 FIFO CPU target
  972. public byte IFAIsFastSer7; // non-zero if interface is Fast serial
  973. public byte AIsVCP7; // non-zero if interface is to use VCP drivers
  974. public byte IFBIsFifo7; // non-zero if interface is 245 FIFO
  975. public byte IFBIsFifoTar7; // non-zero if interface is 245 FIFO CPU target
  976. public byte IFBIsFastSer7; // non-zero if interface is Fast serial
  977. public byte BIsVCP7; // non-zero if interface is to use VCP drivers
  978. public byte PowerSaveEnable; // non-zero if using BCBUS7 to save power for self-powered designs
  979. // FT4232H extensions
  980. public byte PullDownEnable8;
  981. public byte SerNumEnable8;
  982. public byte ASlowSlew; // non-zero if AL pins have slow slew
  983. public byte ASchmittInput; // non-zero if AL pins are Schmitt input
  984. public byte ADriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA
  985. public byte BSlowSlew; // non-zero if AH pins have slow slew
  986. public byte BSchmittInput; // non-zero if AH pins are Schmitt input
  987. public byte BDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA
  988. public byte CSlowSlew; // non-zero if BL pins have slow slew
  989. public byte CSchmittInput; // non-zero if BL pins are Schmitt input
  990. public byte CDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA
  991. public byte DSlowSlew; // non-zero if BH pins have slow slew
  992. public byte DSchmittInput; // non-zero if BH pins are Schmitt input
  993. public byte DDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA
  994. public byte ARIIsTXDEN;
  995. public byte BRIIsTXDEN;
  996. public byte CRIIsTXDEN;
  997. public byte DRIIsTXDEN;
  998. public byte AIsVCP8; // non-zero if interface is to use VCP drivers
  999. public byte BIsVCP8; // non-zero if interface is to use VCP drivers
  1000. public byte CIsVCP8; // non-zero if interface is to use VCP drivers
  1001. public byte DIsVCP8; // non-zero if interface is to use VCP drivers
  1002. // FT232H extensions
  1003. public byte PullDownEnableH; // non-zero if pull down enabled
  1004. public byte SerNumEnableH; // non-zero if serial number to be used
  1005. public byte ACSlowSlewH; // non-zero if AC pins have slow slew
  1006. public byte ACSchmittInputH; // non-zero if AC pins are Schmitt input
  1007. public byte ACDriveCurrentH; // valid values are 4mA, 8mA, 12mA, 16mA
  1008. public byte ADSlowSlewH; // non-zero if AD pins have slow slew
  1009. public byte ADSchmittInputH; // non-zero if AD pins are Schmitt input
  1010. public byte ADDriveCurrentH; // valid values are 4mA, 8mA, 12mA, 16mA
  1011. public byte Cbus0H; // Cbus Mux control
  1012. public byte Cbus1H; // Cbus Mux control
  1013. public byte Cbus2H; // Cbus Mux control
  1014. public byte Cbus3H; // Cbus Mux control
  1015. public byte Cbus4H; // Cbus Mux control
  1016. public byte Cbus5H; // Cbus Mux control
  1017. public byte Cbus6H; // Cbus Mux control
  1018. public byte Cbus7H; // Cbus Mux control
  1019. public byte Cbus8H; // Cbus Mux control
  1020. public byte Cbus9H; // Cbus Mux control
  1021. public byte IsFifoH; // non-zero if interface is 245 FIFO
  1022. public byte IsFifoTarH; // non-zero if interface is 245 FIFO CPU target
  1023. public byte IsFastSerH; // non-zero if interface is Fast serial
  1024. public byte IsFT1248H; // non-zero if interface is FT1248
  1025. public byte FT1248CpolH; // FT1248 clock polarity
  1026. public byte FT1248LsbH; // FT1248 data is LSB (1) or MSB (0)
  1027. public byte FT1248FlowControlH; // FT1248 flow control enable
  1028. public byte IsVCPH; // non-zero if interface is to use VCP drivers
  1029. public byte PowerSaveEnableH; // non-zero if using ACBUS7 to save power for self-powered designs
  1030. }
  1031. [StructLayout(LayoutKind.Sequential, Pack = 4)]
  1032. struct FT_EEPROM_HEADER
  1033. {
  1034. public UInt32 deviceType; // FTxxxx device type to be programmed
  1035. // Device descriptor options
  1036. public UInt16 VendorId; // 0x0403
  1037. public UInt16 ProductId; // 0x6001
  1038. public byte SerNumEnable; // non-zero if serial number to be used
  1039. // Config descriptor options
  1040. public UInt16 MaxPower; // 0 < MaxPower <= 500
  1041. public byte SelfPowered; // 0 = bus powered, 1 = self powered
  1042. public byte RemoteWakeup; // 0 = not capable, 1 = capable
  1043. // Hardware options
  1044. public byte PullDownEnable; // non-zero if pull down in suspend enabled
  1045. }
  1046. [StructLayout(LayoutKind.Sequential, Pack = 4)]
  1047. struct FT_XSERIES_DATA
  1048. {
  1049. public FT_EEPROM_HEADER common;
  1050. public byte ACSlowSlew; // non-zero if AC bus pins have slow slew
  1051. public byte ACSchmittInput; // non-zero if AC bus pins are Schmitt input
  1052. public byte ACDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA
  1053. public byte ADSlowSlew; // non-zero if AD bus pins have slow slew
  1054. public byte ADSchmittInput; // non-zero if AD bus pins are Schmitt input
  1055. public byte ADDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA
  1056. // CBUS options
  1057. public byte Cbus0; // Cbus Mux control
  1058. public byte Cbus1; // Cbus Mux control
  1059. public byte Cbus2; // Cbus Mux control
  1060. public byte Cbus3; // Cbus Mux control
  1061. public byte Cbus4; // Cbus Mux control
  1062. public byte Cbus5; // Cbus Mux control
  1063. public byte Cbus6; // Cbus Mux control
  1064. // UART signal options
  1065. public byte InvertTXD; // non-zero if invert TXD
  1066. public byte InvertRXD; // non-zero if invert RXD
  1067. public byte InvertRTS; // non-zero if invert RTS
  1068. public byte InvertCTS; // non-zero if invert CTS
  1069. public byte InvertDTR; // non-zero if invert DTR
  1070. public byte InvertDSR; // non-zero if invert DSR
  1071. public byte InvertDCD; // non-zero if invert DCD
  1072. public byte InvertRI; // non-zero if invert RI
  1073. // Battery Charge Detect options
  1074. public byte BCDEnable; // Enable Battery Charger Detection
  1075. public byte BCDForceCbusPWREN; // asserts the power enable signal on CBUS when charging port detected
  1076. public byte BCDDisableSleep; // forces the device never to go into sleep mode
  1077. // I2C options
  1078. public UInt16 I2CSlaveAddress; // I2C slave device address
  1079. public UInt32 I2CDeviceId; // I2C device ID
  1080. public byte I2CDisableSchmitt; // Disable I2C Schmitt trigger
  1081. // FT1248 options
  1082. public byte FT1248Cpol; // FT1248 clock polarity - clock idle high (1) or clock idle low (0)
  1083. public byte FT1248Lsb; // FT1248 data is LSB (1) or MSB (0)
  1084. public byte FT1248FlowControl; // FT1248 flow control enable
  1085. // Hardware options
  1086. public byte RS485EchoSuppress; //
  1087. public byte PowerSaveEnable; //
  1088. // Driver option
  1089. public byte DriverType; //
  1090. }
  1091. // Base class for EEPROM structures - these elements are common to all devices
  1092. /// <summary>
  1093. /// Common EEPROM elements for all devices. Inherited to specific device type EEPROMs.
  1094. /// </summary>
  1095. public class FT_EEPROM_DATA
  1096. {
  1097. //private const UInt32 Signature1 = 0x00000000;
  1098. //private const UInt32 Signature2 = 0xFFFFFFFF;
  1099. //private const UInt32 Version = 0x00000002;
  1100. /// <summary>
  1101. /// Vendor ID as supplied by the USB Implementers Forum
  1102. /// </summary>
  1103. public UInt16 VendorID = 0x0403;
  1104. /// <summary>
  1105. /// Product ID
  1106. /// </summary>
  1107. public UInt16 ProductID = 0x6001;
  1108. /// <summary>
  1109. /// Manufacturer name string
  1110. /// </summary>
  1111. public string Manufacturer = "FTDI";
  1112. /// <summary>
  1113. /// Manufacturer name abbreviation to be used as a prefix for automatically generated serial numbers
  1114. /// </summary>
  1115. public string ManufacturerID = "FT";
  1116. /// <summary>
  1117. /// Device description string
  1118. /// </summary>
  1119. public string Description = "USB-Serial Converter";
  1120. /// <summary>
  1121. /// Device serial number string
  1122. /// </summary>
  1123. public string SerialNumber = "";
  1124. /// <summary>
  1125. /// Maximum power the device needs
  1126. /// </summary>
  1127. public UInt16 MaxPower = 0x0090;
  1128. //private bool PnP = true;
  1129. /// <summary>
  1130. /// Indicates if the device has its own power supply (self-powered) or gets power from the USB port (bus-powered)
  1131. /// </summary>
  1132. public bool SelfPowered = false;
  1133. /// <summary>
  1134. /// Determines if the device can wake the host PC from suspend by toggling the RI line
  1135. /// </summary>
  1136. public bool RemoteWakeup = false;
  1137. }
  1138. // EEPROM class for FT232B and FT245B
  1139. /// <summary>
  1140. /// EEPROM structure specific to FT232B and FT245B devices.
  1141. /// Inherits from FT_EEPROM_DATA.
  1142. /// </summary>
  1143. public class FT232B_EEPROM_STRUCTURE : FT_EEPROM_DATA
  1144. {
  1145. //private bool Rev4 = true;
  1146. //private bool IsoIn = false;
  1147. //private bool IsoOut = false;
  1148. /// <summary>
  1149. /// Determines if IOs are pulled down when the device is in suspend
  1150. /// </summary>
  1151. public bool PullDownEnable = false;
  1152. /// <summary>
  1153. /// Determines if the serial number is enabled
  1154. /// </summary>
  1155. public bool SerNumEnable = true;
  1156. /// <summary>
  1157. /// Determines if the USB version number is enabled
  1158. /// </summary>
  1159. public bool USBVersionEnable = true;
  1160. /// <summary>
  1161. /// The USB version number. Should be either 0x0110 (USB 1.1) or 0x0200 (USB 2.0)
  1162. /// </summary>
  1163. public UInt16 USBVersion = 0x0200;
  1164. }
  1165. // EEPROM class for FT2232C, FT2232L and FT2232D
  1166. /// <summary>
  1167. /// EEPROM structure specific to FT2232 devices.
  1168. /// Inherits from FT_EEPROM_DATA.
  1169. /// </summary>
  1170. public class FT2232_EEPROM_STRUCTURE : FT_EEPROM_DATA
  1171. {
  1172. //private bool Rev5 = true;
  1173. //private bool IsoInA = false;
  1174. //private bool IsoInB = false;
  1175. //private bool IsoOutA = false;
  1176. //private bool IsoOutB = false;
  1177. /// <summary>
  1178. /// Determines if IOs are pulled down when the device is in suspend
  1179. /// </summary>
  1180. public bool PullDownEnable = false;
  1181. /// <summary>
  1182. /// Determines if the serial number is enabled
  1183. /// </summary>
  1184. public bool SerNumEnable = true;
  1185. /// <summary>
  1186. /// Determines if the USB version number is enabled
  1187. /// </summary>
  1188. public bool USBVersionEnable = true;
  1189. /// <summary>
  1190. /// The USB version number. Should be either 0x0110 (USB 1.1) or 0x0200 (USB 2.0)
  1191. /// </summary>
  1192. public UInt16 USBVersion = 0x0200;
  1193. /// <summary>
  1194. /// Enables high current IOs on channel A
  1195. /// </summary>
  1196. public bool AIsHighCurrent = false;
  1197. /// <summary>
  1198. /// Enables high current IOs on channel B
  1199. /// </summary>
  1200. public bool BIsHighCurrent = false;
  1201. /// <summary>
  1202. /// Determines if channel A is in FIFO mode
  1203. /// </summary>
  1204. public bool IFAIsFifo = false;
  1205. /// <summary>
  1206. /// Determines if channel A is in FIFO target mode
  1207. /// </summary>
  1208. public bool IFAIsFifoTar = false;
  1209. /// <summary>
  1210. /// Determines if channel A is in fast serial mode
  1211. /// </summary>
  1212. public bool IFAIsFastSer = false;
  1213. /// <summary>
  1214. /// Determines if channel A loads the VCP driver
  1215. /// </summary>
  1216. public bool AIsVCP = true;
  1217. /// <summary>
  1218. /// Determines if channel B is in FIFO mode
  1219. /// </summary>
  1220. public bool IFBIsFifo = false;
  1221. /// <summary>
  1222. /// Determines if channel B is in FIFO target mode
  1223. /// </summary>
  1224. public bool IFBIsFifoTar = false;
  1225. /// <summary>
  1226. /// Determines if channel B is in fast serial mode
  1227. /// </summary>
  1228. public bool IFBIsFastSer = false;
  1229. /// <summary>
  1230. /// Determines if channel B loads the VCP driver
  1231. /// </summary>
  1232. public bool BIsVCP = true;
  1233. }
  1234. // EEPROM class for FT232R and FT245R
  1235. /// <summary>
  1236. /// EEPROM structure specific to FT232R and FT245R devices.
  1237. /// Inherits from FT_EEPROM_DATA.
  1238. /// </summary>
  1239. public class FT232R_EEPROM_STRUCTURE : FT_EEPROM_DATA
  1240. {
  1241. /// <summary>
  1242. /// Disables the FT232R internal clock source.
  1243. /// If the device has external oscillator enabled it must have an external oscillator fitted to function
  1244. /// </summary>
  1245. public bool UseExtOsc = false;
  1246. /// <summary>
  1247. /// Enables high current IOs
  1248. /// </summary>
  1249. public bool HighDriveIOs = false;
  1250. /// <summary>
  1251. /// Sets the endpoint size. This should always be set to 64
  1252. /// </summary>
  1253. public byte EndpointSize = 64;
  1254. /// <summary>
  1255. /// Determines if IOs are pulled down when the device is in suspend
  1256. /// </summary>
  1257. public bool PullDownEnable = false;
  1258. /// <summary>
  1259. /// Determines if the serial number is enabled
  1260. /// </summary>
  1261. public bool SerNumEnable = true;
  1262. /// <summary>
  1263. /// Inverts the sense of the TXD line
  1264. /// </summary>
  1265. public bool InvertTXD = false;
  1266. /// <summary>
  1267. /// Inverts the sense of the RXD line
  1268. /// </summary>
  1269. public bool InvertRXD = false;
  1270. /// <summary>
  1271. /// Inverts the sense of the RTS line
  1272. /// </summary>
  1273. public bool InvertRTS = false;
  1274. /// <summary>
  1275. /// Inverts the sense of the CTS line
  1276. /// </summary>
  1277. public bool InvertCTS = false;
  1278. /// <summary>
  1279. /// Inverts the sense of the DTR line
  1280. /// </summary>
  1281. public bool InvertDTR = false;
  1282. /// <summary>
  1283. /// Inverts the sense of the DSR line
  1284. /// </summary>
  1285. public bool InvertDSR = false;
  1286. /// <summary>
  1287. /// Inverts the sense of the DCD line
  1288. /// </summary>
  1289. public bool InvertDCD = false;
  1290. /// <summary>
  1291. /// Inverts the sense of the RI line
  1292. /// </summary>
  1293. public bool InvertRI = false;
  1294. /// <summary>
  1295. /// Sets the function of the CBUS0 pin for FT232R devices.
  1296. /// Valid values are FT_CBUS_TXDEN, FT_CBUS_PWRON , FT_CBUS_RXLED, FT_CBUS_TXLED,
  1297. /// FT_CBUS_TXRXLED, FT_CBUS_SLEEP, FT_CBUS_CLK48, FT_CBUS_CLK24, FT_CBUS_CLK12,
  1298. /// FT_CBUS_CLK6, FT_CBUS_IOMODE, FT_CBUS_BITBANG_WR, FT_CBUS_BITBANG_RD
  1299. /// </summary>
  1300. public byte Cbus0 = FT_CBUS_OPTIONS.FT_CBUS_SLEEP;
  1301. /// <summary>
  1302. /// Sets the function of the CBUS1 pin for FT232R devices.
  1303. /// Valid values are FT_CBUS_TXDEN, FT_CBUS_PWRON , FT_CBUS_RXLED, FT_CBUS_TXLED,
  1304. /// FT_CBUS_TXRXLED, FT_CBUS_SLEEP, FT_CBUS_CLK48, FT_CBUS_CLK24, FT_CBUS_CLK12,
  1305. /// FT_CBUS_CLK6, FT_CBUS_IOMODE, FT_CBUS_BITBANG_WR, FT_CBUS_BITBANG_RD
  1306. /// </summary>
  1307. public byte Cbus1 = FT_CBUS_OPTIONS.FT_CBUS_SLEEP;
  1308. /// <summary>
  1309. /// Sets the function of the CBUS2 pin for FT232R devices.
  1310. /// Valid values are FT_CBUS_TXDEN, FT_CBUS_PWRON , FT_CBUS_RXLED, FT_CBUS_TXLED,
  1311. /// FT_CBUS_TXRXLED, FT_CBUS_SLEEP, FT_CBUS_CLK48, FT_CBUS_CLK24, FT_CBUS_CLK12,
  1312. /// FT_CBUS_CLK6, FT_CBUS_IOMODE, FT_CBUS_BITBANG_WR, FT_CBUS_BITBANG_RD
  1313. /// </summary>
  1314. public byte Cbus2 = FT_CBUS_OPTIONS.FT_CBUS_SLEEP;
  1315. /// <summary>
  1316. /// Sets the function of the CBUS3 pin for FT232R devices.
  1317. /// Valid values are FT_CBUS_TXDEN, FT_CBUS_PWRON , FT_CBUS_RXLED, FT_CBUS_TXLED,
  1318. /// FT_CBUS_TXRXLED, FT_CBUS_SLEEP, FT_CBUS_CLK48, FT_CBUS_CLK24, FT_CBUS_CLK12,
  1319. /// FT_CBUS_CLK6, FT_CBUS_IOMODE, FT_CBUS_BITBANG_WR, FT_CBUS_BITBANG_RD
  1320. /// </summary>
  1321. public byte Cbus3 = FT_CBUS_OPTIONS.FT_CBUS_SLEEP;
  1322. /// <summary>
  1323. /// Sets the function of the CBUS4 pin for FT232R devices.
  1324. /// Valid values are FT_CBUS_TXDEN, FT_CBUS_PWRON , FT_CBUS_RXLED, FT_CBUS_TXLED,
  1325. /// FT_CBUS_TXRXLED, FT_CBUS_SLEEP, FT_CBUS_CLK48, FT_CBUS_CLK24, FT_CBUS_CLK12,
  1326. /// FT_CBUS_CLK6
  1327. /// </summary>
  1328. public byte Cbus4 = FT_CBUS_OPTIONS.FT_CBUS_SLEEP;
  1329. /// <summary>
  1330. /// Determines if the VCP driver is loaded
  1331. /// </summary>
  1332. public bool RIsD2XX = false;
  1333. }
  1334. // EEPROM class for FT2232H
  1335. /// <summary>
  1336. /// EEPROM structure specific to FT2232H devices.
  1337. /// Inherits from FT_EEPROM_DATA.
  1338. /// </summary>
  1339. public class FT2232H_EEPROM_STRUCTURE : FT_EEPROM_DATA
  1340. {
  1341. /// <summary>
  1342. /// Determines if IOs are pulled down when the device is in suspend
  1343. /// </summary>
  1344. public bool PullDownEnable = false;
  1345. /// <summary>
  1346. /// Determines if the serial number is enabled
  1347. /// </summary>
  1348. public bool SerNumEnable = true;
  1349. /// <summary>
  1350. /// Determines if AL pins have a slow slew rate
  1351. /// </summary>
  1352. public bool ALSlowSlew = false;
  1353. /// <summary>
  1354. /// Determines if the AL pins have a Schmitt input
  1355. /// </summary>
  1356. public bool ALSchmittInput = false;
  1357. /// <summary>
  1358. /// Determines the AL pins drive current in mA. Valid values are FT_DRIVE_CURRENT_4MA, FT_DRIVE_CURRENT_8MA, FT_DRIVE_CURRENT_12MA or FT_DRIVE_CURRENT_16MA
  1359. /// </summary>
  1360. public byte ALDriveCurrent = FT_DRIVE_CURRENT.FT_DRIVE_CURRENT_4MA;
  1361. /// <summary>
  1362. /// Determines if AH pins have a slow slew rate
  1363. /// </summary>
  1364. public bool AHSlowSlew = false;
  1365. /// <summary>
  1366. /// Determines if the AH pins have a Schmitt input
  1367. /// </summary>
  1368. public bool AHSchmittInput = false;
  1369. /// <summary>
  1370. /// Determines the AH pins drive current in mA. Valid values are FT_DRIVE_CURRENT_4MA, FT_DRIVE_CURRENT_8MA, FT_DRIVE_CURRENT_12MA or FT_DRIVE_CURRENT_16MA
  1371. /// </summary>
  1372. public byte AHDriveCurrent = FT_DRIVE_CURRENT.FT_DRIVE_CURRENT_4MA;
  1373. /// <summary>
  1374. /// Determines if BL pins have a slow slew rate
  1375. /// </summary>
  1376. public bool BLSlowSlew = false;
  1377. /// <summary>
  1378. /// Determines if the BL pins have a Schmitt input
  1379. /// </summary>
  1380. public bool BLSchmittInput = false;
  1381. /// <summary>
  1382. /// Determines the BL pins drive current in mA. Valid values are FT_DRIVE_CURRENT_4MA, FT_DRIVE_CURRENT_8MA, FT_DRIVE_CURRENT_12MA or FT_DRIVE_CURRENT_16MA
  1383. /// </summary>
  1384. public byte BLDriveCurrent = FT_DRIVE_CURRENT.FT_DRIVE_CURRENT_4MA;
  1385. /// <summary>
  1386. /// Determines if BH pins have a slow slew rate
  1387. /// </summary>
  1388. public bool BHSlowSlew = false;
  1389. /// <summary>
  1390. /// Determines if the BH pins have a Schmitt input
  1391. /// </summary>
  1392. public bool BHSchmittInput = false;
  1393. /// <summary>
  1394. /// Determines the BH pins drive current in mA. Valid values are FT_DRIVE_CURRENT_4MA, FT_DRIVE_CURRENT_8MA, FT_DRIVE_CURRENT_12MA or FT_DRIVE_CURRENT_16MA
  1395. /// </summary>
  1396. public byte BHDriveCurrent = FT_DRIVE_CURRENT.FT_DRIVE_CURRENT_4MA;
  1397. /// <summary>
  1398. /// Determines if channel A is in FIFO mode
  1399. /// </summary>
  1400. public bool IFAIsFifo = false;
  1401. /// <summary>
  1402. /// Determines if channel A is in FIFO target mode
  1403. /// </summary>
  1404. public bool IFAIsFifoTar = false;
  1405. /// <summary>
  1406. /// Determines if channel A is in fast serial mode
  1407. /// </summary>
  1408. public bool IFAIsFastSer = false;
  1409. /// <summary>
  1410. /// Determines if channel A loads the VCP driver
  1411. /// </summary>
  1412. public bool AIsVCP = true;
  1413. /// <summary>
  1414. /// Determines if channel B is in FIFO mode
  1415. /// </summary>
  1416. public bool IFBIsFifo = false;
  1417. /// <summary>
  1418. /// Determines if channel B is in FIFO target mode
  1419. /// </summary>
  1420. public bool IFBIsFifoTar = false;
  1421. /// <summary>
  1422. /// Determines if channel B is in fast serial mode
  1423. /// </summary>
  1424. public bool IFBIsFastSer = false;
  1425. /// <summary>
  1426. /// Determines if channel B loads the VCP driver
  1427. /// </summary>
  1428. public bool BIsVCP = true;
  1429. /// <summary>
  1430. /// For self-powered designs, keeps the FT2232H in low power state until BCBUS7 is high
  1431. /// </summary>
  1432. public bool PowerSaveEnable = false;
  1433. }
  1434. // EEPROM class for FT4232H
  1435. /// <summary>
  1436. /// EEPROM structure specific to FT4232H devices.
  1437. /// Inherits from FT_EEPROM_DATA.
  1438. /// </summary>
  1439. public class FT4232H_EEPROM_STRUCTURE : FT_EEPROM_DATA
  1440. {
  1441. /// <summary>
  1442. /// Determines if IOs are pulled down when the device is in suspend
  1443. /// </summary>
  1444. public bool PullDownEnable = false;
  1445. /// <summary>
  1446. /// Determines if the serial number is enabled
  1447. /// </summary>
  1448. public bool SerNumEnable = true;
  1449. /// <summary>
  1450. /// Determines if A pins have a slow slew rate
  1451. /// </summary>
  1452. public bool ASlowSlew = false;
  1453. /// <summary>
  1454. /// Determines if the A pins have a Schmitt input
  1455. /// </summary>
  1456. public bool ASchmittInput = false;
  1457. /// <summary>
  1458. /// Determines the A pins drive current in mA. Valid values are FT_DRIVE_CURRENT_4MA, FT_DRIVE_CURRENT_8MA, FT_DRIVE_CURRENT_12MA or FT_DRIVE_CURRENT_16MA
  1459. /// </summary>
  1460. public byte ADriveCurrent = FT_DRIVE_CURRENT.FT_DRIVE_CURRENT_4MA;
  1461. /// <summary>
  1462. /// Determines if B pins have a slow slew rate
  1463. /// </summary>
  1464. public bool BSlowSlew = false;
  1465. /// <summary>
  1466. /// Determines if the B pins have a Schmitt input
  1467. /// </summary>
  1468. public bool BSchmittInput = false;
  1469. /// <summary>
  1470. /// Determines the B pins drive current in mA. Valid values are FT_DRIVE_CURRENT_4MA, FT_DRIVE_CURRENT_8MA, FT_DRIVE_CURRENT_12MA or FT_DRIVE_CURRENT_16MA
  1471. /// </summary>
  1472. public byte BDriveCurrent = FT_DRIVE_CURRENT.FT_DRIVE_CURRENT_4MA;
  1473. /// <summary>
  1474. /// Determines if C pins have a slow slew rate
  1475. /// </summary>
  1476. public bool CSlowSlew = false;
  1477. /// <summary>
  1478. /// Determines if the C pins have a Schmitt input
  1479. /// </summary>
  1480. public bool CSchmittInput = false;
  1481. /// <summary>
  1482. /// Determines the C pins drive current in mA. Valid values are FT_DRIVE_CURRENT_4MA, FT_DRIVE_CURRENT_8MA, FT_DRIVE_CURRENT_12MA or FT_DRIVE_CURRENT_16MA
  1483. /// </summary>
  1484. public byte CDriveCurrent = FT_DRIVE_CURRENT.FT_DRIVE_CURRENT_4MA;
  1485. /// <summary>
  1486. /// Determines if D pins have a slow slew rate
  1487. /// </summary>
  1488. public bool DSlowSlew = false;
  1489. /// <summary>
  1490. /// Determines if the D pins have a Schmitt input
  1491. /// </summary>
  1492. public bool DSchmittInput = false;
  1493. /// <summary>
  1494. /// Determines the D pins drive current in mA. Valid values are FT_DRIVE_CURRENT_4MA, FT_DRIVE_CURRENT_8MA, FT_DRIVE_CURRENT_12MA or FT_DRIVE_CURRENT_16MA
  1495. /// </summary>
  1496. public byte DDriveCurrent = FT_DRIVE_CURRENT.FT_DRIVE_CURRENT_4MA;
  1497. /// <summary>
  1498. /// RI of port A acts as RS485 transmit enable (TXDEN)
  1499. /// </summary>
  1500. public bool ARIIsTXDEN = false;
  1501. /// <summary>
  1502. /// RI of port B acts as RS485 transmit enable (TXDEN)
  1503. /// </summary>
  1504. public bool BRIIsTXDEN = false;
  1505. /// <summary>
  1506. /// RI of port C acts as RS485 transmit enable (TXDEN)
  1507. /// </summary>
  1508. public bool CRIIsTXDEN = false;
  1509. /// <summary>
  1510. /// RI of port D acts as RS485 transmit enable (TXDEN)
  1511. /// </summary>
  1512. public bool DRIIsTXDEN = false;
  1513. /// <summary>
  1514. /// Determines if channel A loads the VCP driver
  1515. /// </summary>
  1516. public bool AIsVCP = true;
  1517. /// <summary>
  1518. /// Determines if channel B loads the VCP driver
  1519. /// </summary>
  1520. public bool BIsVCP = true;
  1521. /// <summary>
  1522. /// Determines if channel C loads the VCP driver
  1523. /// </summary>
  1524. public bool CIsVCP = true;
  1525. /// <summary>
  1526. /// Determines if channel D loads the VCP driver
  1527. /// </summary>
  1528. public bool DIsVCP = true;
  1529. }
  1530. // EEPROM class for FT232H
  1531. /// <summary>
  1532. /// EEPROM structure specific to FT232H devices.
  1533. /// Inherits from FT_EEPROM_DATA.
  1534. /// </summary>
  1535. public class FT232H_EEPROM_STRUCTURE : FT_EEPROM_DATA
  1536. {
  1537. /// <summary>
  1538. /// Determines if IOs are pulled down when the device is in suspend
  1539. /// </summary>
  1540. public bool PullDownEnable = false;
  1541. /// <summary>
  1542. /// Determines if the serial number is enabled
  1543. /// </summary>
  1544. public bool SerNumEnable = true;
  1545. /// <summary>
  1546. /// Determines if AC pins have a slow slew rate
  1547. /// </summary>
  1548. public bool ACSlowSlew = false;
  1549. /// <summary>
  1550. /// Determines if the AC pins have a Schmitt input
  1551. /// </summary>
  1552. public bool ACSchmittInput = false;
  1553. /// <summary>
  1554. /// Determines the AC pins drive current in mA. Valid values are FT_DRIVE_CURRENT_4MA, FT_DRIVE_CURRENT_8MA, FT_DRIVE_CURRENT_12MA or FT_DRIVE_CURRENT_16MA
  1555. /// </summary>
  1556. public byte ACDriveCurrent = FT_DRIVE_CURRENT.FT_DRIVE_CURRENT_4MA;
  1557. /// <summary>
  1558. /// Determines if AD pins have a slow slew rate
  1559. /// </summary>
  1560. public bool ADSlowSlew = false;
  1561. /// <summary>
  1562. /// Determines if the AD pins have a Schmitt input
  1563. /// </summary>
  1564. public bool ADSchmittInput = false;
  1565. /// <summary>
  1566. /// Determines the AD pins drive current in mA. Valid values are FT_DRIVE_CURRENT_4MA, FT_DRIVE_CURRENT_8MA, FT_DRIVE_CURRENT_12MA or FT_DRIVE_CURRENT_16MA
  1567. /// </summary>
  1568. public byte ADDriveCurrent = FT_DRIVE_CURRENT.FT_DRIVE_CURRENT_4MA;
  1569. /// <summary>
  1570. /// Sets the function of the CBUS0 pin for FT232H devices.
  1571. /// Valid values are FT_CBUS_TRISTATE, FT_CBUS_RXLED, FT_CBUS_TXLED, FT_CBUS_TXRXLED,
  1572. /// FT_CBUS_PWREN, FT_CBUS_SLEEP, FT_CBUS_DRIVE_0, FT_CBUS_DRIVE_1, FT_CBUS_TXDEN, FT_CBUS_CLK30,
  1573. /// FT_CBUS_CLK15, FT_CBUS_CLK7_5
  1574. /// </summary>
  1575. public byte Cbus0 = FT_232H_CBUS_OPTIONS.FT_CBUS_TRISTATE;
  1576. /// <summary>
  1577. /// Sets the function of the CBUS1 pin for FT232H devices.
  1578. /// Valid values are FT_CBUS_TRISTATE, FT_CBUS_RXLED, FT_CBUS_TXLED, FT_CBUS_TXRXLED,
  1579. /// FT_CBUS_PWREN, FT_CBUS_SLEEP, FT_CBUS_DRIVE_0, FT_CBUS_DRIVE_1, FT_CBUS_TXDEN, FT_CBUS_CLK30,
  1580. /// FT_CBUS_CLK15, FT_CBUS_CLK7_5
  1581. /// </summary>
  1582. public byte Cbus1 = FT_232H_CBUS_OPTIONS.FT_CBUS_TRISTATE;
  1583. /// <summary>
  1584. /// Sets the function of the CBUS2 pin for FT232H devices.
  1585. /// Valid values are FT_CBUS_TRISTATE, FT_CBUS_RXLED, FT_CBUS_TXLED, FT_CBUS_TXRXLED,
  1586. /// FT_CBUS_PWREN, FT_CBUS_SLEEP, FT_CBUS_DRIVE_0, FT_CBUS_DRIVE_1, FT_CBUS_TXDEN
  1587. /// </summary>
  1588. public byte Cbus2 = FT_232H_CBUS_OPTIONS.FT_CBUS_TRISTATE;
  1589. /// <summary>
  1590. /// Sets the function of the CBUS3 pin for FT232H devices.
  1591. /// Valid values are FT_CBUS_TRISTATE, FT_CBUS_RXLED, FT_CBUS_TXLED, FT_CBUS_TXRXLED,
  1592. /// FT_CBUS_PWREN, FT_CBUS_SLEEP, FT_CBUS_DRIVE_0, FT_CBUS_DRIVE_1, FT_CBUS_TXDEN
  1593. /// </summary>
  1594. public byte Cbus3 = FT_232H_CBUS_OPTIONS.FT_CBUS_TRISTATE;
  1595. /// <summary>
  1596. /// Sets the function of the CBUS4 pin for FT232H devices.
  1597. /// Valid values are FT_CBUS_TRISTATE, FT_CBUS_RXLED, FT_CBUS_TXLED, FT_CBUS_TXRXLED,
  1598. /// FT_CBUS_PWREN, FT_CBUS_SLEEP, FT_CBUS_DRIVE_0, FT_CBUS_DRIVE_1, FT_CBUS_TXDEN
  1599. /// </summary>
  1600. public byte Cbus4 = FT_232H_CBUS_OPTIONS.FT_CBUS_TRISTATE;
  1601. /// <summary>
  1602. /// Sets the function of the CBUS5 pin for FT232H devices.
  1603. /// Valid values are FT_CBUS_TRISTATE, FT_CBUS_RXLED, FT_CBUS_TXLED, FT_CBUS_TXRXLED,
  1604. /// FT_CBUS_PWREN, FT_CBUS_SLEEP, FT_CBUS_DRIVE_0, FT_CBUS_DRIVE_1, FT_CBUS_IOMODE,
  1605. /// FT_CBUS_TXDEN, FT_CBUS_CLK30, FT_CBUS_CLK15, FT_CBUS_CLK7_5
  1606. /// </summary>
  1607. public byte Cbus5 = FT_232H_CBUS_OPTIONS.FT_CBUS_TRISTATE;
  1608. /// <summary>
  1609. /// Sets the function of the CBUS6 pin for FT232H devices.
  1610. /// Valid values are FT_CBUS_TRISTATE, FT_CBUS_RXLED, FT_CBUS_TXLED, FT_CBUS_TXRXLED,
  1611. /// FT_CBUS_PWREN, FT_CBUS_SLEEP, FT_CBUS_DRIVE_0, FT_CBUS_DRIVE_1, FT_CBUS_IOMODE,
  1612. /// FT_CBUS_TXDEN, FT_CBUS_CLK30, FT_CBUS_CLK15, FT_CBUS_CLK7_5
  1613. /// </summary>
  1614. public byte Cbus6 = FT_232H_CBUS_OPTIONS.FT_CBUS_TRISTATE;
  1615. /// <summary>
  1616. /// Sets the function of the CBUS7 pin for FT232H devices.
  1617. /// Valid values are FT_CBUS_TRISTATE
  1618. /// </summary>
  1619. public byte Cbus7 = FT_232H_CBUS_OPTIONS.FT_CBUS_TRISTATE;
  1620. /// <summary>
  1621. /// Sets the function of the CBUS8 pin for FT232H devices.
  1622. /// Valid values are FT_CBUS_TRISTATE, FT_CBUS_RXLED, FT_CBUS_TXLED, FT_CBUS_TXRXLED,
  1623. /// FT_CBUS_PWREN, FT_CBUS_SLEEP, FT_CBUS_DRIVE_0, FT_CBUS_DRIVE_1, FT_CBUS_IOMODE,
  1624. /// FT_CBUS_TXDEN, FT_CBUS_CLK30, FT_CBUS_CLK15, FT_CBUS_CLK7_5
  1625. /// </summary>
  1626. public byte Cbus8 = FT_232H_CBUS_OPTIONS.FT_CBUS_TRISTATE;
  1627. /// <summary>
  1628. /// Sets the function of the CBUS9 pin for FT232H devices.
  1629. /// Valid values are FT_CBUS_TRISTATE, FT_CBUS_RXLED, FT_CBUS_TXLED, FT_CBUS_TXRXLED,
  1630. /// FT_CBUS_PWREN, FT_CBUS_SLEEP, FT_CBUS_DRIVE_0, FT_CBUS_DRIVE_1, FT_CBUS_IOMODE,
  1631. /// FT_CBUS_TXDEN, FT_CBUS_CLK30, FT_CBUS_CLK15, FT_CBUS_CLK7_5
  1632. /// </summary>
  1633. public byte Cbus9 = FT_232H_CBUS_OPTIONS.FT_CBUS_TRISTATE;
  1634. /// <summary>
  1635. /// Determines if the device is in FIFO mode
  1636. /// </summary>
  1637. public bool IsFifo = false;
  1638. /// <summary>
  1639. /// Determines if the device is in FIFO target mode
  1640. /// </summary>
  1641. public bool IsFifoTar = false;
  1642. /// <summary>
  1643. /// Determines if the device is in fast serial mode
  1644. /// </summary>
  1645. public bool IsFastSer = false;
  1646. /// <summary>
  1647. /// Determines if the device is in FT1248 mode
  1648. /// </summary>
  1649. public bool IsFT1248 = false;
  1650. /// <summary>
  1651. /// Determines FT1248 mode clock polarity
  1652. /// </summary>
  1653. public bool FT1248Cpol = false;
  1654. /// <summary>
  1655. /// Determines if data is ent MSB (0) or LSB (1) in FT1248 mode
  1656. /// </summary>
  1657. public bool FT1248Lsb = false;
  1658. /// <summary>
  1659. /// Determines if FT1248 mode uses flow control
  1660. /// </summary>
  1661. public bool FT1248FlowControl = false;
  1662. /// <summary>
  1663. /// Determines if the VCP driver is loaded
  1664. /// </summary>
  1665. public bool IsVCP = true;
  1666. /// <summary>
  1667. /// For self-powered designs, keeps the FT232H in low power state until ACBUS7 is high
  1668. /// </summary>
  1669. public bool PowerSaveEnable = false;
  1670. }
  1671. /// <summary>
  1672. /// EEPROM structure specific to X-Series devices.
  1673. /// Inherits from FT_EEPROM_DATA.
  1674. /// </summary>
  1675. public class FT_XSERIES_EEPROM_STRUCTURE : FT_EEPROM_DATA
  1676. {
  1677. /// <summary>
  1678. /// Determines if IOs are pulled down when the device is in suspend
  1679. /// </summary>
  1680. public bool PullDownEnable = false;
  1681. /// <summary>
  1682. /// Determines if the serial number is enabled
  1683. /// </summary>
  1684. public bool SerNumEnable = true;
  1685. /// <summary>
  1686. /// Determines if the USB version number is enabled
  1687. /// </summary>
  1688. public bool USBVersionEnable = true;
  1689. /// <summary>
  1690. /// The USB version number: 0x0200 (USB 2.0)
  1691. /// </summary>
  1692. public UInt16 USBVersion = 0x0200;
  1693. /// <summary>
  1694. /// Determines if AC pins have a slow slew rate
  1695. /// </summary>
  1696. public byte ACSlowSlew;
  1697. /// <summary>
  1698. /// Determines if the AC pins have a Schmitt input
  1699. /// </summary>
  1700. public byte ACSchmittInput;
  1701. /// <summary>
  1702. /// Determines the AC pins drive current in mA. Valid values are FT_DRIVE_CURRENT_4MA, FT_DRIVE_CURRENT_8MA, FT_DRIVE_CURRENT_12MA or FT_DRIVE_CURRENT_16MA
  1703. /// </summary>
  1704. public byte ACDriveCurrent;
  1705. /// <summary>
  1706. /// Determines if AD pins have a slow slew rate
  1707. /// </summary>
  1708. public byte ADSlowSlew;
  1709. /// <summary>
  1710. /// Determines if AD pins have a schmitt input
  1711. /// </summary>
  1712. public byte ADSchmittInput;
  1713. /// <summary>
  1714. /// Determines the AD pins drive current in mA. Valid values are FT_DRIVE_CURRENT_4MA, FT_DRIVE_CURRENT_8MA, FT_DRIVE_CURRENT_12MA or FT_DRIVE_CURRENT_16MA
  1715. /// </summary>
  1716. public byte ADDriveCurrent;
  1717. /// <summary>
  1718. /// Sets the function of the CBUS0 pin for FT232H devices.
  1719. /// Valid values are FT_CBUS_TRISTATE, FT_CBUS_RXLED, FT_CBUS_TXLED, FT_CBUS_TXRXLED,
  1720. /// FT_CBUS_PWREN, FT_CBUS_SLEEP, FT_CBUS_DRIVE_0, FT_CBUS_DRIVE_1, FT_CBUS_GPIO, FT_CBUS_TXDEN, FT_CBUS_CLK24,
  1721. /// FT_CBUS_CLK12, FT_CBUS_CLK6, FT_CBUS_BCD_CHARGER, FT_CBUS_BCD_CHARGER_N, FT_CBUS_VBUS_SENSE, FT_CBUS_BITBANG_WR,
  1722. /// FT_CBUS_BITBANG_RD, FT_CBUS_TIME_STAMP, FT_CBUS_KEEP_AWAKE
  1723. /// </summary>
  1724. public byte Cbus0;
  1725. /// <summary>
  1726. /// Sets the function of the CBUS1 pin for FT232H devices.
  1727. /// Valid values are FT_CBUS_TRISTATE, FT_CBUS_RXLED, FT_CBUS_TXLED, FT_CBUS_TXRXLED,
  1728. /// FT_CBUS_PWREN, FT_CBUS_SLEEP, FT_CBUS_DRIVE_0, FT_CBUS_DRIVE_1, FT_CBUS_GPIO, FT_CBUS_TXDEN, FT_CBUS_CLK24,
  1729. /// FT_CBUS_CLK12, FT_CBUS_CLK6, FT_CBUS_BCD_CHARGER, FT_CBUS_BCD_CHARGER_N, FT_CBUS_VBUS_SENSE, FT_CBUS_BITBANG_WR,
  1730. /// FT_CBUS_BITBANG_RD, FT_CBUS_TIME_STAMP, FT_CBUS_KEEP_AWAKE
  1731. /// </summary>
  1732. public byte Cbus1;
  1733. /// <summary>
  1734. /// Sets the function of the CBUS2 pin for FT232H devices.
  1735. /// Valid values are FT_CBUS_TRISTATE, FT_CBUS_RXLED, FT_CBUS_TXLED, FT_CBUS_TXRXLED,
  1736. /// FT_CBUS_PWREN, FT_CBUS_SLEEP, FT_CBUS_DRIVE_0, FT_CBUS_DRIVE_1, FT_CBUS_GPIO, FT_CBUS_TXDEN, FT_CBUS_CLK24,
  1737. /// FT_CBUS_CLK12, FT_CBUS_CLK6, FT_CBUS_BCD_CHARGER, FT_CBUS_BCD_CHARGER_N, FT_CBUS_VBUS_SENSE, FT_CBUS_BITBANG_WR,
  1738. /// FT_CBUS_BITBANG_RD, FT_CBUS_TIME_STAMP, FT_CBUS_KEEP_AWAKE
  1739. /// </summary>
  1740. public byte Cbus2;
  1741. /// <summary>
  1742. /// Sets the function of the CBUS3 pin for FT232H devices.
  1743. /// Valid values are FT_CBUS_TRISTATE, FT_CBUS_RXLED, FT_CBUS_TXLED, FT_CBUS_TXRXLED,
  1744. /// FT_CBUS_PWREN, FT_CBUS_SLEEP, FT_CBUS_DRIVE_0, FT_CBUS_DRIVE_1, FT_CBUS_GPIO, FT_CBUS_TXDEN, FT_CBUS_CLK24,
  1745. /// FT_CBUS_CLK12, FT_CBUS_CLK6, FT_CBUS_BCD_CHARGER, FT_CBUS_BCD_CHARGER_N, FT_CBUS_VBUS_SENSE, FT_CBUS_BITBANG_WR,
  1746. /// FT_CBUS_BITBANG_RD, FT_CBUS_TIME_STAMP, FT_CBUS_KEEP_AWAKE
  1747. /// </summary>
  1748. public byte Cbus3;
  1749. /// <summary>
  1750. /// Sets the function of the CBUS4 pin for FT232H devices.
  1751. /// Valid values are FT_CBUS_TRISTATE, FT_CBUS_RXLED, FT_CBUS_TXLED, FT_CBUS_TXRXLED,
  1752. /// FT_CBUS_PWREN, FT_CBUS_SLEEP, FT_CBUS_DRIVE_0, FT_CBUS_DRIVE_1, FT_CBUS_TXDEN, FT_CBUS_CLK24,
  1753. /// FT_CBUS_CLK12, FT_CBUS_CLK6, FT_CBUS_BCD_CHARGER, FT_CBUS_BCD_CHARGER_N, FT_CBUS_VBUS_SENSE, FT_CBUS_BITBANG_WR,
  1754. /// FT_CBUS_BITBANG_RD, FT_CBUS_TIME_STAMP, FT_CBUS_KEEP_AWAKE
  1755. /// </summary>
  1756. public byte Cbus4;
  1757. /// <summary>
  1758. /// Sets the function of the CBUS5 pin for FT232H devices.
  1759. /// Valid values are FT_CBUS_TRISTATE, FT_CBUS_RXLED, FT_CBUS_TXLED, FT_CBUS_TXRXLED,
  1760. /// FT_CBUS_PWREN, FT_CBUS_SLEEP, FT_CBUS_DRIVE_0, FT_CBUS_DRIVE_1, FT_CBUS_TXDEN, FT_CBUS_CLK24,
  1761. /// FT_CBUS_CLK12, FT_CBUS_CLK6, FT_CBUS_BCD_CHARGER, FT_CBUS_BCD_CHARGER_N, FT_CBUS_VBUS_SENSE, FT_CBUS_BITBANG_WR,
  1762. /// FT_CBUS_BITBANG_RD, FT_CBUS_TIME_STAMP, FT_CBUS_KEEP_AWAKE
  1763. /// </summary>
  1764. public byte Cbus5;
  1765. /// <summary>
  1766. /// Sets the function of the CBUS6 pin for FT232H devices.
  1767. /// Valid values are FT_CBUS_TRISTATE, FT_CBUS_RXLED, FT_CBUS_TXLED, FT_CBUS_TXRXLED,
  1768. /// FT_CBUS_PWREN, FT_CBUS_SLEEP, FT_CBUS_DRIVE_0, FT_CBUS_DRIVE_1, FT_CBUS_TXDEN, FT_CBUS_CLK24,
  1769. /// FT_CBUS_CLK12, FT_CBUS_CLK6, FT_CBUS_BCD_CHARGER, FT_CBUS_BCD_CHARGER_N, FT_CBUS_VBUS_SENSE, FT_CBUS_BITBANG_WR,
  1770. /// FT_CBUS_BITBANG_RD, FT_CBUS_TIME_STAMP, FT_CBUS_KEEP_AWAKE
  1771. /// </summary>
  1772. public byte Cbus6;
  1773. /// <summary>
  1774. /// Inverts the sense of the TXD line
  1775. /// </summary>
  1776. public byte InvertTXD;
  1777. /// <summary>
  1778. /// Inverts the sense of the RXD line
  1779. /// </summary>
  1780. public byte InvertRXD;
  1781. /// <summary>
  1782. /// Inverts the sense of the RTS line
  1783. /// </summary>
  1784. public byte InvertRTS;
  1785. /// <summary>
  1786. /// Inverts the sense of the CTS line
  1787. /// </summary>
  1788. public byte InvertCTS;
  1789. /// <summary>
  1790. /// Inverts the sense of the DTR line
  1791. /// </summary>
  1792. public byte InvertDTR;
  1793. /// <summary>
  1794. /// Inverts the sense of the DSR line
  1795. /// </summary>
  1796. public byte InvertDSR;
  1797. /// <summary>
  1798. /// Inverts the sense of the DCD line
  1799. /// </summary>
  1800. public byte InvertDCD;
  1801. /// <summary>
  1802. /// Inverts the sense of the RI line
  1803. /// </summary>
  1804. public byte InvertRI;
  1805. /// <summary>
  1806. /// Determines whether the Battery Charge Detection option is enabled.
  1807. /// </summary>
  1808. public byte BCDEnable;
  1809. /// <summary>
  1810. /// Asserts the power enable signal on CBUS when charging port detected.
  1811. /// </summary>
  1812. public byte BCDForceCbusPWREN;
  1813. /// <summary>
  1814. /// Forces the device never to go into sleep mode.
  1815. /// </summary>
  1816. public byte BCDDisableSleep;
  1817. /// <summary>
  1818. /// I2C slave device address.
  1819. /// </summary>
  1820. public ushort I2CSlaveAddress;
  1821. /// <summary>
  1822. /// I2C device ID
  1823. /// </summary>
  1824. public UInt32 I2CDeviceId;
  1825. /// <summary>
  1826. /// Disable I2C Schmitt trigger.
  1827. /// </summary>
  1828. public byte I2CDisableSchmitt;
  1829. /// <summary>
  1830. /// FT1248 clock polarity - clock idle high (1) or clock idle low (0)
  1831. /// </summary>
  1832. public byte FT1248Cpol;
  1833. /// <summary>
  1834. /// FT1248 data is LSB (1) or MSB (0)
  1835. /// </summary>
  1836. public byte FT1248Lsb;
  1837. /// <summary>
  1838. /// FT1248 flow control enable.
  1839. /// </summary>
  1840. public byte FT1248FlowControl;
  1841. /// <summary>
  1842. /// Enable RS485 Echo Suppression
  1843. /// </summary>
  1844. public byte RS485EchoSuppress;
  1845. /// <summary>
  1846. /// Enable Power Save mode.
  1847. /// </summary>
  1848. public byte PowerSaveEnable;
  1849. /// <summary>
  1850. /// Determines whether the VCP driver is loaded.
  1851. /// </summary>
  1852. public byte IsVCP;
  1853. }
  1854. #endregion
  1855. #region EXCEPTION_HANDLING
  1856. /// <summary>
  1857. /// Exceptions thrown by errors within the FTDI class.
  1858. /// </summary>
  1859. [global::System.Serializable]
  1860. public class FT_EXCEPTION : Exception
  1861. {
  1862. /// <summary>
  1863. ///
  1864. /// </summary>
  1865. public FT_EXCEPTION() { }
  1866. /// <summary>
  1867. ///
  1868. /// </summary>
  1869. /// <param name="message"></param>
  1870. public FT_EXCEPTION(string message) : base(message) { }
  1871. /// <summary>
  1872. ///
  1873. /// </summary>
  1874. /// <param name="message"></param>
  1875. /// <param name="inner"></param>
  1876. public FT_EXCEPTION(string message, Exception inner) : base(message, inner) { }
  1877. /// <summary>
  1878. ///
  1879. /// </summary>
  1880. /// <param name="info"></param>
  1881. /// <param name="context"></param>
  1882. protected FT_EXCEPTION(
  1883. System.Runtime.Serialization.SerializationInfo info,
  1884. System.Runtime.Serialization.StreamingContext context)
  1885. : base(info, context) { }
  1886. }
  1887. #endregion
  1888. #region FUNCTION_IMPORTS_FTD2XX.DLL
  1889. // Handle to our DLL - used with GetProcAddress to load all of our functions
  1890. IntPtr hFTD2XXDLL = IntPtr.Zero;
  1891. // Declare pointers to each of the functions we are going to use in FT2DXX.DLL
  1892. // These are assigned in our constructor and freed in our destructor.
  1893. IntPtr pFT_CreateDeviceInfoList = IntPtr.Zero;
  1894. IntPtr pFT_GetDeviceInfoDetail = IntPtr.Zero;
  1895. IntPtr pFT_Open = IntPtr.Zero;
  1896. IntPtr pFT_OpenEx = IntPtr.Zero;
  1897. IntPtr pFT_Close = IntPtr.Zero;
  1898. IntPtr pFT_Read = IntPtr.Zero;
  1899. IntPtr pFT_Write = IntPtr.Zero;
  1900. IntPtr pFT_GetQueueStatus = IntPtr.Zero;
  1901. IntPtr pFT_GetModemStatus = IntPtr.Zero;
  1902. IntPtr pFT_GetStatus = IntPtr.Zero;
  1903. IntPtr pFT_SetBaudRate = IntPtr.Zero;
  1904. IntPtr pFT_SetDataCharacteristics = IntPtr.Zero;
  1905. IntPtr pFT_SetFlowControl = IntPtr.Zero;
  1906. IntPtr pFT_SetDtr = IntPtr.Zero;
  1907. IntPtr pFT_ClrDtr = IntPtr.Zero;
  1908. IntPtr pFT_SetRts = IntPtr.Zero;
  1909. IntPtr pFT_ClrRts = IntPtr.Zero;
  1910. IntPtr pFT_ResetDevice = IntPtr.Zero;
  1911. IntPtr pFT_ResetPort = IntPtr.Zero;
  1912. IntPtr pFT_CyclePort = IntPtr.Zero;
  1913. IntPtr pFT_Rescan = IntPtr.Zero;
  1914. IntPtr pFT_Reload = IntPtr.Zero;
  1915. IntPtr pFT_Purge = IntPtr.Zero;
  1916. IntPtr pFT_SetTimeouts = IntPtr.Zero;
  1917. IntPtr pFT_SetBreakOn = IntPtr.Zero;
  1918. IntPtr pFT_SetBreakOff = IntPtr.Zero;
  1919. IntPtr pFT_GetDeviceInfo = IntPtr.Zero;
  1920. IntPtr pFT_SetResetPipeRetryCount = IntPtr.Zero;
  1921. IntPtr pFT_StopInTask = IntPtr.Zero;
  1922. IntPtr pFT_RestartInTask = IntPtr.Zero;
  1923. IntPtr pFT_GetDriverVersion = IntPtr.Zero;
  1924. IntPtr pFT_GetLibraryVersion = IntPtr.Zero;
  1925. IntPtr pFT_SetDeadmanTimeout = IntPtr.Zero;
  1926. IntPtr pFT_SetChars = IntPtr.Zero;
  1927. IntPtr pFT_SetEventNotification = IntPtr.Zero;
  1928. IntPtr pFT_GetComPortNumber = IntPtr.Zero;
  1929. IntPtr pFT_SetLatencyTimer = IntPtr.Zero;
  1930. IntPtr pFT_GetLatencyTimer = IntPtr.Zero;
  1931. IntPtr pFT_SetBitMode = IntPtr.Zero;
  1932. IntPtr pFT_GetBitMode = IntPtr.Zero;
  1933. IntPtr pFT_SetUSBParameters = IntPtr.Zero;
  1934. IntPtr pFT_ReadEE = IntPtr.Zero;
  1935. IntPtr pFT_WriteEE = IntPtr.Zero;
  1936. IntPtr pFT_EraseEE = IntPtr.Zero;
  1937. IntPtr pFT_EE_UASize = IntPtr.Zero;
  1938. IntPtr pFT_EE_UARead = IntPtr.Zero;
  1939. IntPtr pFT_EE_UAWrite = IntPtr.Zero;
  1940. IntPtr pFT_EE_Read = IntPtr.Zero;
  1941. IntPtr pFT_EE_Program = IntPtr.Zero;
  1942. IntPtr pFT_EEPROM_Read = IntPtr.Zero;
  1943. IntPtr pFT_EEPROM_Program = IntPtr.Zero;
  1944. #endregion
  1945. #region METHOD_DEFINITIONS
  1946. //**************************************************************************
  1947. // GetNumberOfDevices
  1948. //**************************************************************************
  1949. // Intellisense comments
  1950. /// <summary>
  1951. /// Gets the number of FTDI devices available.
  1952. /// </summary>
  1953. /// <returns>FT_STATUS value from FT_CreateDeviceInfoList in FTD2XX.DLL</returns>
  1954. /// <param name="devcount">The number of FTDI devices available.</param>
  1955. public FT_STATUS GetNumberOfDevices(ref UInt32 devcount)
  1956. {
  1957. // Initialise ftStatus to something other than FT_OK
  1958. FT_STATUS ftStatus = FT_STATUS.FT_OTHER_ERROR;
  1959. // If the DLL hasn't been loaded, just return here
  1960. if (hFTD2XXDLL == IntPtr.Zero)
  1961. return ftStatus;
  1962. // Check for our required function pointers being set up
  1963. if (pFT_CreateDeviceInfoList != IntPtr.Zero)
  1964. {
  1965. tFT_CreateDeviceInfoList FT_CreateDeviceInfoList = (tFT_CreateDeviceInfoList)Marshal.GetDelegateForFunctionPointer(pFT_CreateDeviceInfoList, typeof(tFT_CreateDeviceInfoList));
  1966. // Call FT_CreateDeviceInfoList
  1967. ftStatus = FT_CreateDeviceInfoList(ref devcount);
  1968. }
  1969. else
  1970. {
  1971. MessageBox.Show("Failed to load function FT_CreateDeviceInfoList.");
  1972. }
  1973. return ftStatus;
  1974. }
  1975. //**************************************************************************
  1976. // GetDeviceList
  1977. //**************************************************************************
  1978. // Intellisense comments
  1979. /// <summary>
  1980. /// Gets information on all of the FTDI devices available.
  1981. /// </summary>
  1982. /// <returns>FT_STATUS value from FT_GetDeviceInfoDetail in FTD2XX.DLL</returns>
  1983. /// <param name="devicelist">An array of type FT_DEVICE_INFO_NODE to contain the device information for all available devices.</param>
  1984. /// <exception cref="FT_EXCEPTION">Thrown when the supplied buffer is not large enough to contain the device info list.</exception>
  1985. public FT_STATUS GetDeviceList(FT_DEVICE_INFO_NODE[] devicelist)
  1986. {
  1987. // Initialise ftStatus to something other than FT_OK
  1988. FT_STATUS ftStatus = FT_STATUS.FT_OTHER_ERROR;
  1989. FT_ERROR ftErrorCondition = FT_ERROR.FT_NO_ERROR;
  1990. // If the DLL hasn't been loaded, just return here
  1991. if (hFTD2XXDLL == IntPtr.Zero)
  1992. return ftStatus;
  1993. // Check for our required function pointers being set up
  1994. if ((pFT_CreateDeviceInfoList != IntPtr.Zero) & (pFT_GetDeviceInfoDetail != IntPtr.Zero))
  1995. {
  1996. UInt32 devcount = 0;
  1997. tFT_CreateDeviceInfoList FT_CreateDeviceInfoList = (tFT_CreateDeviceInfoList)Marshal.GetDelegateForFunctionPointer(pFT_CreateDeviceInfoList, typeof(tFT_CreateDeviceInfoList));
  1998. tFT_GetDeviceInfoDetail FT_GetDeviceInfoDetail = (tFT_GetDeviceInfoDetail)Marshal.GetDelegateForFunctionPointer(pFT_GetDeviceInfoDetail, typeof(tFT_GetDeviceInfoDetail));
  1999. // Call FT_CreateDeviceInfoList
  2000. ftStatus = FT_CreateDeviceInfoList(ref devcount);
  2001. // Allocate the required storage for our list
  2002. byte[] sernum = new byte[16];
  2003. byte[] desc = new byte[64];
  2004. if (devcount > 0)
  2005. {
  2006. // Check the size of the buffer passed in is big enough
  2007. if (devicelist.Length < devcount)
  2008. {
  2009. // Buffer not big enough
  2010. ftErrorCondition = FT_ERROR.FT_BUFFER_SIZE;
  2011. // Throw exception
  2012. ErrorHandler(ftStatus, ftErrorCondition);
  2013. }
  2014. // Instantiate the array elements as FT_DEVICE_INFO_NODE
  2015. for (UInt32 i = 0; i < devcount; i++)
  2016. {
  2017. devicelist[i] = new FT_DEVICE_INFO_NODE();
  2018. // Call FT_GetDeviceInfoDetail
  2019. ftStatus = FT_GetDeviceInfoDetail(i, ref devicelist[i].Flags, ref devicelist[i].Type, ref devicelist[i].ID, ref devicelist[i].LocId, sernum, desc, ref devicelist[i].ftHandle);
  2020. // Convert byte arrays to strings
  2021. devicelist[i].SerialNumber = Encoding.ASCII.GetString(sernum);
  2022. devicelist[i].Description = Encoding.ASCII.GetString(desc);
  2023. // Trim strings to first occurrence of a null terminator character
  2024. devicelist[i].SerialNumber = devicelist[i].SerialNumber.Substring(0, devicelist[i].SerialNumber.IndexOf("\0"));
  2025. devicelist[i].Description = devicelist[i].Description.Substring(0, devicelist[i].Description.IndexOf("\0"));
  2026. }
  2027. }
  2028. }
  2029. else
  2030. {
  2031. if (pFT_CreateDeviceInfoList == IntPtr.Zero)
  2032. {
  2033. MessageBox.Show("Failed to load function FT_CreateDeviceInfoList.");
  2034. }
  2035. if (pFT_GetDeviceInfoDetail == IntPtr.Zero)
  2036. {
  2037. MessageBox.Show("Failed to load function FT_GetDeviceInfoListDetail.");
  2038. }
  2039. }
  2040. return ftStatus;
  2041. }
  2042. //**************************************************************************
  2043. // OpenByIndex
  2044. //**************************************************************************
  2045. // Intellisense comments
  2046. /// <summary>
  2047. /// Opens the FTDI device with the specified index.
  2048. /// </summary>
  2049. /// <returns>FT_STATUS value from FT_Open in FTD2XX.DLL</returns>
  2050. /// <param name="index">Index of the device to open.
  2051. /// Note that this cannot be guaranteed to open a specific device.</param>
  2052. /// <remarks>Initialises the device to 8 data bits, 1 stop bit, no parity, no flow control and 9600 Baud.</remarks>
  2053. public FT_STATUS OpenByIndex(UInt32 index)
  2054. {
  2055. // Initialise ftStatus to something other than FT_OK
  2056. FT_STATUS ftStatus = FT_STATUS.FT_OTHER_ERROR;
  2057. // If the DLL hasn't been loaded, just return here
  2058. if (hFTD2XXDLL == IntPtr.Zero)
  2059. return ftStatus;
  2060. // Check for our required function pointers being set up
  2061. if ((pFT_Open != IntPtr.Zero) & (pFT_SetDataCharacteristics != IntPtr.Zero) & (pFT_SetFlowControl != IntPtr.Zero) & (pFT_SetBaudRate != IntPtr.Zero))
  2062. {
  2063. tFT_Open FT_Open = (tFT_Open)Marshal.GetDelegateForFunctionPointer(pFT_Open, typeof(tFT_Open));
  2064. tFT_SetDataCharacteristics FT_SetDataCharacteristics = (tFT_SetDataCharacteristics)Marshal.GetDelegateForFunctionPointer(pFT_SetDataCharacteristics, typeof(tFT_SetDataCharacteristics));
  2065. tFT_SetFlowControl FT_SetFlowControl = (tFT_SetFlowControl)Marshal.GetDelegateForFunctionPointer(pFT_SetFlowControl, typeof(tFT_SetFlowControl));
  2066. tFT_SetBaudRate FT_SetBaudRate = (tFT_SetBaudRate)Marshal.GetDelegateForFunctionPointer(pFT_SetBaudRate, typeof(tFT_SetBaudRate));
  2067. // Call FT_Open
  2068. ftStatus = FT_Open(index, ref ftHandle);
  2069. // Appears that the handle value can be non-NULL on a fail, so set it explicitly
  2070. if (ftStatus != FT_STATUS.FT_OK)
  2071. ftHandle = IntPtr.Zero;
  2072. if (ftHandle != IntPtr.Zero)
  2073. {
  2074. // Initialise port data characteristics
  2075. byte WordLength = FT_DATA_BITS.FT_BITS_8;
  2076. byte StopBits = FT_STOP_BITS.FT_STOP_BITS_1;
  2077. byte Parity = FT_PARITY.FT_PARITY_NONE;
  2078. ftStatus = FT_SetDataCharacteristics(ftHandle, WordLength, StopBits, Parity);
  2079. // Initialise to no flow control
  2080. UInt16 FlowControl = FT_FLOW_CONTROL.FT_FLOW_NONE;
  2081. byte Xon = 0x11;
  2082. byte Xoff = 0x13;
  2083. ftStatus = FT_SetFlowControl(ftHandle, FlowControl, Xon, Xoff);
  2084. // Initialise Baud rate
  2085. UInt32 BaudRate = 9600;
  2086. ftStatus = FT_SetBaudRate(ftHandle, BaudRate);
  2087. }
  2088. }
  2089. else
  2090. {
  2091. if (pFT_Open == IntPtr.Zero)
  2092. {
  2093. MessageBox.Show("Failed to load function FT_Open.");
  2094. }
  2095. if (pFT_SetDataCharacteristics == IntPtr.Zero)
  2096. {
  2097. MessageBox.Show("Failed to load function FT_SetDataCharacteristics.");
  2098. }
  2099. if (pFT_SetFlowControl == IntPtr.Zero)
  2100. {
  2101. MessageBox.Show("Failed to load function FT_SetFlowControl.");
  2102. }
  2103. if (pFT_SetBaudRate == IntPtr.Zero)
  2104. {
  2105. MessageBox.Show("Failed to load function FT_SetBaudRate.");
  2106. }
  2107. }
  2108. return ftStatus;
  2109. }
  2110. //**************************************************************************
  2111. // OpenBySerialNumber
  2112. //**************************************************************************
  2113. // Intellisense comments
  2114. /// <summary>
  2115. /// Opens the FTDI device with the specified serial number.
  2116. /// </summary>
  2117. /// <returns>FT_STATUS value from FT_OpenEx in FTD2XX.DLL</returns>
  2118. /// <param name="serialnumber">Serial number of the device to open.</param>
  2119. /// <remarks>Initialises the device to 8 data bits, 1 stop bit, no parity, no flow control and 9600 Baud.</remarks>
  2120. public FT_STATUS OpenBySerialNumber(string serialnumber)
  2121. {
  2122. // Initialise ftStatus to something other than FT_OK
  2123. FT_STATUS ftStatus = FT_STATUS.FT_OTHER_ERROR;
  2124. // If the DLL hasn't been loaded, just return here
  2125. if (hFTD2XXDLL == IntPtr.Zero)
  2126. return ftStatus;
  2127. // Check for our required function pointers being set up
  2128. if ((pFT_OpenEx != IntPtr.Zero) & (pFT_SetDataCharacteristics != IntPtr.Zero) & (pFT_SetFlowControl != IntPtr.Zero) & (pFT_SetBaudRate != IntPtr.Zero))
  2129. {
  2130. tFT_OpenEx FT_OpenEx = (tFT_OpenEx)Marshal.GetDelegateForFunctionPointer(pFT_OpenEx, typeof(tFT_OpenEx));
  2131. tFT_SetDataCharacteristics FT_SetDataCharacteristics = (tFT_SetDataCharacteristics)Marshal.GetDelegateForFunctionPointer(pFT_SetDataCharacteristics, typeof(tFT_SetDataCharacteristics));
  2132. tFT_SetFlowControl FT_SetFlowControl = (tFT_SetFlowControl)Marshal.GetDelegateForFunctionPointer(pFT_SetFlowControl, typeof(tFT_SetFlowControl));
  2133. tFT_SetBaudRate FT_SetBaudRate = (tFT_SetBaudRate)Marshal.GetDelegateForFunctionPointer(pFT_SetBaudRate, typeof(tFT_SetBaudRate));
  2134. // Call FT_OpenEx
  2135. ftStatus = FT_OpenEx(serialnumber, FT_OPEN_BY_SERIAL_NUMBER, ref ftHandle);
  2136. // Appears that the handle value can be non-NULL on a fail, so set it explicitly
  2137. if (ftStatus != FT_STATUS.FT_OK)
  2138. ftHandle = IntPtr.Zero;
  2139. if (ftHandle != IntPtr.Zero)
  2140. {
  2141. // Initialise port data characteristics
  2142. byte WordLength = FT_DATA_BITS.FT_BITS_8;
  2143. byte StopBits = FT_STOP_BITS.FT_STOP_BITS_1;
  2144. byte Parity = FT_PARITY.FT_PARITY_NONE;
  2145. ftStatus = FT_SetDataCharacteristics(ftHandle, WordLength, StopBits, Parity);
  2146. // Initialise to no flow control
  2147. UInt16 FlowControl = FT_FLOW_CONTROL.FT_FLOW_NONE;
  2148. byte Xon = 0x11;
  2149. byte Xoff = 0x13;
  2150. ftStatus = FT_SetFlowControl(ftHandle, FlowControl, Xon, Xoff);
  2151. // Initialise Baud rate
  2152. UInt32 BaudRate = 9600;
  2153. ftStatus = FT_SetBaudRate(ftHandle, BaudRate);
  2154. }
  2155. }
  2156. else
  2157. {
  2158. if (pFT_OpenEx == IntPtr.Zero)
  2159. {
  2160. MessageBox.Show("Failed to load function FT_OpenEx.");
  2161. }
  2162. if (pFT_SetDataCharacteristics == IntPtr.Zero)
  2163. {
  2164. MessageBox.Show("Failed to load function FT_SetDataCharacteristics.");
  2165. }
  2166. if (pFT_SetFlowControl == IntPtr.Zero)
  2167. {
  2168. MessageBox.Show("Failed to load function FT_SetFlowControl.");
  2169. }
  2170. if (pFT_SetBaudRate == IntPtr.Zero)
  2171. {
  2172. MessageBox.Show("Failed to load function FT_SetBaudRate.");
  2173. }
  2174. }
  2175. return ftStatus;
  2176. }
  2177. //**************************************************************************
  2178. // OpenByDescription
  2179. //**************************************************************************
  2180. // Intellisense comments
  2181. /// <summary>
  2182. /// Opens the FTDI device with the specified description.
  2183. /// </summary>
  2184. /// <returns>FT_STATUS value from FT_OpenEx in FTD2XX.DLL</returns>
  2185. /// <param name="description">Description of the device to open.</param>
  2186. /// <remarks>Initialises the device to 8 data bits, 1 stop bit, no parity, no flow control and 9600 Baud.</remarks>
  2187. public FT_STATUS OpenByDescription(string description)
  2188. {
  2189. // Initialise ftStatus to something other than FT_OK
  2190. FT_STATUS ftStatus = FT_STATUS.FT_OTHER_ERROR;
  2191. // If the DLL hasn't been loaded, just return here
  2192. if (hFTD2XXDLL == IntPtr.Zero)
  2193. return ftStatus;
  2194. // Check for our required function pointers being set up
  2195. if ((pFT_OpenEx != IntPtr.Zero) & (pFT_SetDataCharacteristics != IntPtr.Zero) & (pFT_SetFlowControl != IntPtr.Zero) & (pFT_SetBaudRate != IntPtr.Zero))
  2196. {
  2197. tFT_OpenEx FT_OpenEx = (tFT_OpenEx)Marshal.GetDelegateForFunctionPointer(pFT_OpenEx, typeof(tFT_OpenEx));
  2198. tFT_SetDataCharacteristics FT_SetDataCharacteristics = (tFT_SetDataCharacteristics)Marshal.GetDelegateForFunctionPointer(pFT_SetDataCharacteristics, typeof(tFT_SetDataCharacteristics));
  2199. tFT_SetFlowControl FT_SetFlowControl = (tFT_SetFlowControl)Marshal.GetDelegateForFunctionPointer(pFT_SetFlowControl, typeof(tFT_SetFlowControl));
  2200. tFT_SetBaudRate FT_SetBaudRate = (tFT_SetBaudRate)Marshal.GetDelegateForFunctionPointer(pFT_SetBaudRate, typeof(tFT_SetBaudRate));
  2201. // Call FT_OpenEx
  2202. ftStatus = FT_OpenEx(description, FT_OPEN_BY_DESCRIPTION, ref ftHandle);
  2203. // Appears that the handle value can be non-NULL on a fail, so set it explicitly
  2204. if (ftStatus != FT_STATUS.FT_OK)
  2205. ftHandle = IntPtr.Zero;
  2206. if (ftHandle != IntPtr.Zero)
  2207. {
  2208. // Initialise port data characteristics
  2209. byte WordLength = FT_DATA_BITS.FT_BITS_8;
  2210. byte StopBits = FT_STOP_BITS.FT_STOP_BITS_1;
  2211. byte Parity = FT_PARITY.FT_PARITY_NONE;
  2212. ftStatus = FT_SetDataCharacteristics(ftHandle, WordLength, StopBits, Parity);
  2213. // Initialise to no flow control
  2214. UInt16 FlowControl = FT_FLOW_CONTROL.FT_FLOW_NONE;
  2215. byte Xon = 0x11;
  2216. byte Xoff = 0x13;
  2217. ftStatus = FT_SetFlowControl(ftHandle, FlowControl, Xon, Xoff);
  2218. // Initialise Baud rate
  2219. UInt32 BaudRate = 9600;
  2220. ftStatus = FT_SetBaudRate(ftHandle, BaudRate);
  2221. }
  2222. }
  2223. else
  2224. {
  2225. if (pFT_OpenEx == IntPtr.Zero)
  2226. {
  2227. MessageBox.Show("Failed to load function FT_OpenEx.");
  2228. }
  2229. if (pFT_SetDataCharacteristics == IntPtr.Zero)
  2230. {
  2231. MessageBox.Show("Failed to load function FT_SetDataCharacteristics.");
  2232. }
  2233. if (pFT_SetFlowControl == IntPtr.Zero)
  2234. {
  2235. MessageBox.Show("Failed to load function FT_SetFlowControl.");
  2236. }
  2237. if (pFT_SetBaudRate == IntPtr.Zero)
  2238. {
  2239. MessageBox.Show("Failed to load function FT_SetBaudRate.");
  2240. }
  2241. }
  2242. return ftStatus;
  2243. }
  2244. //**************************************************************************
  2245. // OpenByLocation
  2246. //**************************************************************************
  2247. // Intellisense comments
  2248. /// <summary>
  2249. /// Opens the FTDI device at the specified physical location.
  2250. /// </summary>
  2251. /// <returns>FT_STATUS value from FT_OpenEx in FTD2XX.DLL</returns>
  2252. /// <param name="location">Location of the device to open.</param>
  2253. /// <remarks>Initialises the device to 8 data bits, 1 stop bit, no parity, no flow control and 9600 Baud.</remarks>
  2254. public FT_STATUS OpenByLocation(UInt32 location)
  2255. {
  2256. // Initialise ftStatus to something other than FT_OK
  2257. FT_STATUS ftStatus = FT_STATUS.FT_OTHER_ERROR;
  2258. // If the DLL hasn't been loaded, just return here
  2259. if (hFTD2XXDLL == IntPtr.Zero)
  2260. return ftStatus;
  2261. // Check for our required function pointers being set up
  2262. if ((pFT_OpenEx != IntPtr.Zero) & (pFT_SetDataCharacteristics != IntPtr.Zero) & (pFT_SetFlowControl != IntPtr.Zero) & (pFT_SetBaudRate != IntPtr.Zero))
  2263. {
  2264. tFT_OpenExLoc FT_OpenEx = (tFT_OpenExLoc)Marshal.GetDelegateForFunctionPointer(pFT_OpenEx, typeof(tFT_OpenExLoc));
  2265. tFT_SetDataCharacteristics FT_SetDataCharacteristics = (tFT_SetDataCharacteristics)Marshal.GetDelegateForFunctionPointer(pFT_SetDataCharacteristics, typeof(tFT_SetDataCharacteristics));
  2266. tFT_SetFlowControl FT_SetFlowControl = (tFT_SetFlowControl)Marshal.GetDelegateForFunctionPointer(pFT_SetFlowControl, typeof(tFT_SetFlowControl));
  2267. tFT_SetBaudRate FT_SetBaudRate = (tFT_SetBaudRate)Marshal.GetDelegateForFunctionPointer(pFT_SetBaudRate, typeof(tFT_SetBaudRate));
  2268. // Call FT_OpenEx
  2269. ftStatus = FT_OpenEx(location, FT_OPEN_BY_LOCATION, ref ftHandle);
  2270. // Appears that the handle value can be non-NULL on a fail, so set it explicitly
  2271. if (ftStatus != FT_STATUS.FT_OK)
  2272. ftHandle = IntPtr.Zero;
  2273. if (ftHandle != IntPtr.Zero)
  2274. {
  2275. // Initialise port data characteristics
  2276. byte WordLength = FT_DATA_BITS.FT_BITS_8;
  2277. byte StopBits = FT_STOP_BITS.FT_STOP_BITS_1;
  2278. byte Parity = FT_PARITY.FT_PARITY_NONE;
  2279. ftStatus = FT_SetDataCharacteristics(ftHandle, WordLength, StopBits, Parity);
  2280. // Initialise to no flow control
  2281. UInt16 FlowControl = FT_FLOW_CONTROL.FT_FLOW_NONE;
  2282. byte Xon = 0x11;
  2283. byte Xoff = 0x13;
  2284. ftStatus = FT_SetFlowControl(ftHandle, FlowControl, Xon, Xoff);
  2285. // Initialise Baud rate
  2286. UInt32 BaudRate = 9600;
  2287. ftStatus = FT_SetBaudRate(ftHandle, BaudRate);
  2288. }
  2289. }
  2290. else
  2291. {
  2292. if (pFT_OpenEx == IntPtr.Zero)
  2293. {
  2294. MessageBox.Show("Failed to load function FT_OpenEx.");
  2295. }
  2296. if (pFT_SetDataCharacteristics == IntPtr.Zero)
  2297. {
  2298. MessageBox.Show("Failed to load function FT_SetDataCharacteristics.");
  2299. }
  2300. if (pFT_SetFlowControl == IntPtr.Zero)
  2301. {
  2302. MessageBox.Show("Failed to load function FT_SetFlowControl.");
  2303. }
  2304. if (pFT_SetBaudRate == IntPtr.Zero)
  2305. {
  2306. MessageBox.Show("Failed to load function FT_SetBaudRate.");
  2307. }
  2308. }
  2309. return ftStatus;
  2310. }
  2311. //**************************************************************************
  2312. // Close
  2313. //**************************************************************************
  2314. // Intellisense comments
  2315. /// <summary>
  2316. /// Closes the handle to an open FTDI device.
  2317. /// </summary>
  2318. /// <returns>FT_STATUS value from FT_Close in FTD2XX.DLL</returns>
  2319. public FT_STATUS Close()
  2320. {
  2321. // Initialise ftStatus to something other than FT_OK
  2322. FT_STATUS ftStatus = FT_STATUS.FT_OTHER_ERROR;
  2323. // If the DLL hasn't been loaded, just return here
  2324. if (hFTD2XXDLL == IntPtr.Zero)
  2325. return ftStatus;
  2326. // Check for our required function pointers being set up
  2327. if (pFT_Close != IntPtr.Zero)
  2328. {
  2329. tFT_Close FT_Close = (tFT_Close)Marshal.GetDelegateForFunctionPointer(pFT_Close, typeof(tFT_Close));
  2330. // Call FT_Close
  2331. ftStatus = FT_Close(ftHandle);
  2332. if (ftStatus == FT_STATUS.FT_OK)
  2333. {
  2334. ftHandle = IntPtr.Zero;
  2335. }
  2336. }
  2337. else
  2338. {
  2339. if (pFT_Close == IntPtr.Zero)
  2340. {
  2341. MessageBox.Show("Failed to load function FT_Close.");
  2342. }
  2343. }
  2344. return ftStatus;
  2345. }
  2346. //**************************************************************************
  2347. // Read
  2348. //**************************************************************************
  2349. // Intellisense comments
  2350. /// <summary>
  2351. /// Read data from an open FTDI device.
  2352. /// </summary>
  2353. /// <returns>FT_STATUS value from FT_Read in FTD2XX.DLL</returns>
  2354. /// <param name="dataBuffer">An array of bytes which will be populated with the data read from the device.</param>
  2355. /// <param name="numBytesToRead">The number of bytes requested from the device.</param>
  2356. /// <param name="numBytesRead">The number of bytes actually read.</param>
  2357. public FT_STATUS Read(byte[] dataBuffer, UInt32 numBytesToRead, ref UInt32 numBytesRead)
  2358. {
  2359. // Initialise ftStatus to something other than FT_OK
  2360. FT_STATUS ftStatus = FT_STATUS.FT_OTHER_ERROR;
  2361. // If the DLL hasn't been loaded, just return here
  2362. if (hFTD2XXDLL == IntPtr.Zero)
  2363. return ftStatus;
  2364. // Check for our required function pointers being set up
  2365. if (pFT_Read != IntPtr.Zero)
  2366. {
  2367. tFT_Read FT_Read = (tFT_Read)Marshal.GetDelegateForFunctionPointer(pFT_Read, typeof(tFT_Read));
  2368. // If the buffer is not big enough to receive the amount of data requested, adjust the number of bytes to read
  2369. if (dataBuffer.Length < numBytesToRead)
  2370. {
  2371. numBytesToRead = (uint)dataBuffer.Length;
  2372. }
  2373. if (ftHandle != IntPtr.Zero)
  2374. {
  2375. // Call FT_Read
  2376. ftStatus = FT_Read(ftHandle, dataBuffer, numBytesToRead, ref numBytesRead);
  2377. }
  2378. }
  2379. else
  2380. {
  2381. if (pFT_Read == IntPtr.Zero)
  2382. {
  2383. MessageBox.Show("Failed to load function FT_Read.");
  2384. }
  2385. }
  2386. return ftStatus;
  2387. }
  2388. // Intellisense comments
  2389. /// <summary>
  2390. /// Read data from an open FTDI device.
  2391. /// </summary>
  2392. /// <returns>FT_STATUS value from FT_Read in FTD2XX.DLL</returns>
  2393. /// <param name="dataBuffer">A string containing the data read</param>
  2394. /// <param name="numBytesToRead">The number of bytes requested from the device.</param>
  2395. /// <param name="numBytesRead">The number of bytes actually read.</param>
  2396. public FT_STATUS Read(out string dataBuffer, UInt32 numBytesToRead, ref UInt32 numBytesRead)
  2397. {
  2398. // Initialise ftStatus to something other than FT_OK
  2399. FT_STATUS ftStatus = FT_STATUS.FT_OTHER_ERROR;
  2400. // As dataBuffer is an OUT parameter, needs to be assigned before returning
  2401. dataBuffer = string.Empty;
  2402. // If the DLL hasn't been loaded, just return here
  2403. if (hFTD2XXDLL == IntPtr.Zero)
  2404. return ftStatus;
  2405. // Check for our required function pointers being set up
  2406. if (pFT_Read != IntPtr.Zero)
  2407. {
  2408. tFT_Read FT_Read = (tFT_Read)Marshal.GetDelegateForFunctionPointer(pFT_Read, typeof(tFT_Read));
  2409. byte[] byteDataBuffer = new byte[numBytesToRead];
  2410. if (ftHandle != IntPtr.Zero)
  2411. {
  2412. // Call FT_Read
  2413. ftStatus = FT_Read(ftHandle, byteDataBuffer, numBytesToRead, ref numBytesRead);
  2414. // Convert ASCII byte array back to Unicode string for passing back
  2415. dataBuffer = Encoding.ASCII.GetString(byteDataBuffer);
  2416. // Trim buffer to actual bytes read
  2417. dataBuffer = dataBuffer.Substring(0, (int)numBytesRead);
  2418. }
  2419. }
  2420. else
  2421. {
  2422. if (pFT_Read == IntPtr.Zero)
  2423. {
  2424. MessageBox.Show("Failed to load function FT_Read.");
  2425. }
  2426. }
  2427. return ftStatus;
  2428. }
  2429. //**************************************************************************
  2430. // Write
  2431. //**************************************************************************
  2432. // Intellisense comments
  2433. /// <summary>
  2434. /// Write data to an open FTDI device.
  2435. /// </summary>
  2436. /// <returns>FT_STATUS value from FT_Write in FTD2XX.DLL</returns>
  2437. /// <param name="dataBuffer">An array of bytes which contains the data to be written to the device.</param>
  2438. /// <param name="numBytesToWrite">The number of bytes to be written to the device.</param>
  2439. /// <param name="numBytesWritten">The number of bytes actually written to the device.</param>
  2440. public FT_STATUS Write(byte[] dataBuffer, Int32 numBytesToWrite, ref UInt32 numBytesWritten)
  2441. {
  2442. // Initialise ftStatus to something other than FT_OK
  2443. FT_STATUS ftStatus = FT_STATUS.FT_OTHER_ERROR;
  2444. // If the DLL hasn't been loaded, just return here
  2445. if (hFTD2XXDLL == IntPtr.Zero)
  2446. return ftStatus;
  2447. // Check for our required function pointers being set up
  2448. if (pFT_Write != IntPtr.Zero)
  2449. {
  2450. tFT_Write FT_Write = (tFT_Write)Marshal.GetDelegateForFunctionPointer(pFT_Write, typeof(tFT_Write));
  2451. if (ftHandle != IntPtr.Zero)
  2452. {
  2453. // Call FT_Write
  2454. ftStatus = FT_Write(ftHandle, dataBuffer, (UInt32)numBytesToWrite, ref numBytesWritten);
  2455. }
  2456. }
  2457. else
  2458. {
  2459. if (pFT_Write == IntPtr.Zero)
  2460. {
  2461. MessageBox.Show("Failed to load function FT_Write.");
  2462. }
  2463. }
  2464. return ftStatus;
  2465. }
  2466. // Intellisense comments
  2467. /// <summary>
  2468. /// Write data to an open FTDI device.
  2469. /// </summary>
  2470. /// <returns>FT_STATUS value from FT_Write in FTD2XX.DLL</returns>
  2471. /// <param name="dataBuffer">An array of bytes which contains the data to be written to the device.</param>
  2472. /// <param name="numBytesToWrite">The number of bytes to be written to the device.</param>
  2473. /// <param name="numBytesWritten">The number of bytes actually written to the device.</param>
  2474. public FT_STATUS Write(byte[] dataBuffer, UInt32 numBytesToWrite, ref UInt32 numBytesWritten)
  2475. {
  2476. // Initialise ftStatus to something other than FT_OK
  2477. FT_STATUS ftStatus = FT_STATUS.FT_OTHER_ERROR;
  2478. // If the DLL hasn't been loaded, just return here
  2479. if (hFTD2XXDLL == IntPtr.Zero)
  2480. return ftStatus;
  2481. // Check for our required function pointers being set up
  2482. if (pFT_Write != IntPtr.Zero)
  2483. {
  2484. tFT_Write FT_Write = (tFT_Write)Marshal.GetDelegateForFunctionPointer(pFT_Write, typeof(tFT_Write));
  2485. if (ftHandle != IntPtr.Zero)
  2486. {
  2487. // Call FT_Write
  2488. ftStatus = FT_Write(ftHandle, dataBuffer, numBytesToWrite, ref numBytesWritten);
  2489. }
  2490. }
  2491. else
  2492. {
  2493. if (pFT_Write == IntPtr.Zero)
  2494. {
  2495. MessageBox.Show("Failed to load function FT_Write.");
  2496. }
  2497. }
  2498. return ftStatus;
  2499. }
  2500. // Intellisense comments
  2501. /// <summary>
  2502. /// Write data to an open FTDI device.
  2503. /// </summary>
  2504. /// <returns>FT_STATUS value from FT_Write in FTD2XX.DLL</returns>
  2505. /// <param name="dataBuffer">A string which contains the data to be written to the device.</param>
  2506. /// <param name="numBytesToWrite">The number of bytes to be written to the device.</param>
  2507. /// <param name="numBytesWritten">The number of bytes actually written to the device.</param>
  2508. public FT_STATUS Write(string dataBuffer, Int32 numBytesToWrite, ref UInt32 numBytesWritten)
  2509. {
  2510. // Initialise ftStatus to something other than FT_OK
  2511. FT_STATUS ftStatus = FT_STATUS.FT_OTHER_ERROR;
  2512. // If the DLL hasn't been loaded, just return here
  2513. if (hFTD2XXDLL == IntPtr.Zero)
  2514. return ftStatus;
  2515. // Check for our required function pointers being set up
  2516. if (pFT_Write != IntPtr.Zero)
  2517. {
  2518. tFT_Write FT_Write = (tFT_Write)Marshal.GetDelegateForFunctionPointer(pFT_Write, typeof(tFT_Write));
  2519. // Convert Unicode string to ASCII byte array
  2520. byte[] byteDataBuffer = Encoding.ASCII.GetBytes(dataBuffer);
  2521. if (ftHandle != IntPtr.Zero)
  2522. {
  2523. // Call FT_Write
  2524. ftStatus = FT_Write(ftHandle, byteDataBuffer, (UInt32)numBytesToWrite, ref numBytesWritten);
  2525. }
  2526. }
  2527. else
  2528. {
  2529. if (pFT_Write == IntPtr.Zero)
  2530. {
  2531. MessageBox.Show("Failed to load function FT_Write.");
  2532. }
  2533. }
  2534. return ftStatus;
  2535. }
  2536. // Intellisense comments
  2537. /// <summary>
  2538. /// Write data to an open FTDI device.
  2539. /// </summary>
  2540. /// <returns>FT_STATUS value from FT_Write in FTD2XX.DLL</returns>
  2541. /// <param name="dataBuffer">A string which contains the data to be written to the device.</param>
  2542. /// <param name="numBytesToWrite">The number of bytes to be written to the device.</param>
  2543. /// <param name="numBytesWritten">The number of bytes actually written to the device.</param>
  2544. public FT_STATUS Write(string dataBuffer, UInt32 numBytesToWrite, ref UInt32 numBytesWritten)
  2545. {
  2546. // Initialise ftStatus to something other than FT_OK
  2547. FT_STATUS ftStatus = FT_STATUS.FT_OTHER_ERROR;
  2548. // If the DLL hasn't been loaded, just return here
  2549. if (hFTD2XXDLL == IntPtr.Zero)
  2550. return ftStatus;
  2551. // Check for our required function pointers being set up
  2552. if (pFT_Write != IntPtr.Zero)
  2553. {
  2554. tFT_Write FT_Write = (tFT_Write)Marshal.GetDelegateForFunctionPointer(pFT_Write, typeof(tFT_Write));
  2555. // Convert Unicode string to ASCII byte array
  2556. byte[] byteDataBuffer = Encoding.ASCII.GetBytes(dataBuffer);
  2557. if (ftHandle != IntPtr.Zero)
  2558. {
  2559. // Call FT_Write
  2560. ftStatus = FT_Write(ftHandle, byteDataBuffer, numBytesToWrite, ref numBytesWritten);
  2561. }
  2562. }
  2563. else
  2564. {
  2565. if (pFT_Write == IntPtr.Zero)
  2566. {
  2567. MessageBox.Show("Failed to load function FT_Write.");
  2568. }
  2569. }
  2570. return ftStatus;
  2571. }
  2572. //**************************************************************************
  2573. // ResetDevice
  2574. //**************************************************************************
  2575. // Intellisense comments
  2576. /// <summary>
  2577. /// Reset an open FTDI device.
  2578. /// </summary>
  2579. /// <returns>FT_STATUS value from FT_ResetDevice in FTD2XX.DLL</returns>
  2580. public FT_STATUS ResetDevice()
  2581. {
  2582. // Initialise ftStatus to something other than FT_OK
  2583. FT_STATUS ftStatus = FT_STATUS.FT_OTHER_ERROR;
  2584. // If the DLL hasn't been loaded, just return here
  2585. if (hFTD2XXDLL == IntPtr.Zero)
  2586. return ftStatus;
  2587. // Check for our required function pointers being set up
  2588. if (pFT_ResetDevice != IntPtr.Zero)
  2589. {
  2590. tFT_ResetDevice FT_ResetDevice = (tFT_ResetDevice)Marshal.GetDelegateForFunctionPointer(pFT_ResetDevice, typeof(tFT_ResetDevice));
  2591. if (ftHandle != IntPtr.Zero)
  2592. {
  2593. // Call FT_ResetDevice
  2594. ftStatus = FT_ResetDevice(ftHandle);
  2595. }
  2596. }
  2597. else
  2598. {
  2599. if (pFT_ResetDevice == IntPtr.Zero)
  2600. {
  2601. MessageBox.Show("Failed to load function FT_ResetDevice.");
  2602. }
  2603. }
  2604. return ftStatus;
  2605. }
  2606. //**************************************************************************
  2607. // Purge
  2608. //**************************************************************************
  2609. // Intellisense comments
  2610. /// <summary>
  2611. /// Purge data from the devices transmit and/or receive buffers.
  2612. /// </summary>
  2613. /// <returns>FT_STATUS value from FT_Purge in FTD2XX.DLL</returns>
  2614. /// <param name="purgemask">Specifies which buffer(s) to be purged. Valid values are any combination of the following flags: FT_PURGE_RX, FT_PURGE_TX</param>
  2615. public FT_STATUS Purge(UInt32 purgemask)
  2616. {
  2617. // Initialise ftStatus to something other than FT_OK
  2618. FT_STATUS ftStatus = FT_STATUS.FT_OTHER_ERROR;
  2619. // If the DLL hasn't been loaded, just return here
  2620. if (hFTD2XXDLL == IntPtr.Zero)
  2621. return ftStatus;
  2622. // Check for our required function pointers being set up
  2623. if (pFT_Purge != IntPtr.Zero)
  2624. {
  2625. tFT_Purge FT_Purge = (tFT_Purge)Marshal.GetDelegateForFunctionPointer(pFT_Purge, typeof(tFT_Purge));
  2626. if (ftHandle != IntPtr.Zero)
  2627. {
  2628. // Call FT_Purge
  2629. ftStatus = FT_Purge(ftHandle, purgemask);
  2630. }
  2631. }
  2632. else
  2633. {
  2634. if (pFT_Purge == IntPtr.Zero)
  2635. {
  2636. MessageBox.Show("Failed to load function FT_Purge.");
  2637. }
  2638. }
  2639. return ftStatus;
  2640. }
  2641. //**************************************************************************
  2642. // SetEventNotification
  2643. //**************************************************************************
  2644. // Intellisense comments
  2645. /// <summary>
  2646. /// Register for event notification.
  2647. /// </summary>
  2648. /// <returns>FT_STATUS value from FT_SetEventNotification in FTD2XX.DLL</returns>
  2649. /// <remarks>After setting event notification, the event can be caught by executing the WaitOne() method of the EventWaitHandle. If multiple event types are being monitored, the event that fired can be determined from the GetEventType method.</remarks>
  2650. /// <param name="eventmask">The type of events to signal. Can be any combination of the following: FT_EVENT_RXCHAR, FT_EVENT_MODEM_STATUS, FT_EVENT_LINE_STATUS</param>
  2651. /// <param name="eventhandle">Handle to the event that will receive the notification</param>
  2652. public FT_STATUS SetEventNotification(UInt32 eventmask, EventWaitHandle eventhandle)
  2653. {
  2654. // Initialise ftStatus to something other than FT_OK
  2655. FT_STATUS ftStatus = FT_STATUS.FT_OTHER_ERROR;
  2656. // If the DLL hasn't been loaded, just return here
  2657. if (hFTD2XXDLL == IntPtr.Zero)
  2658. return ftStatus;
  2659. // Check for our required function pointers being set up
  2660. if (pFT_SetEventNotification != IntPtr.Zero)
  2661. {
  2662. tFT_SetEventNotification FT_SetEventNotification = (tFT_SetEventNotification)Marshal.GetDelegateForFunctionPointer(pFT_SetEventNotification, typeof(tFT_SetEventNotification));
  2663. if (ftHandle != IntPtr.Zero)
  2664. {
  2665. // Call FT_SetSetEventNotification
  2666. ftStatus = FT_SetEventNotification(ftHandle, eventmask, eventhandle.SafeWaitHandle);
  2667. }
  2668. }
  2669. else
  2670. {
  2671. if (pFT_SetEventNotification == IntPtr.Zero)
  2672. {
  2673. MessageBox.Show("Failed to load function FT_SetEventNotification.");
  2674. }
  2675. }
  2676. return ftStatus;
  2677. }
  2678. //**************************************************************************
  2679. // StopInTask
  2680. //**************************************************************************
  2681. // Intellisense comments
  2682. /// <summary>
  2683. /// Stops the driver issuing USB in requests.
  2684. /// </summary>
  2685. /// <returns>FT_STATUS value from FT_StopInTask in FTD2XX.DLL</returns>
  2686. public FT_STATUS StopInTask()
  2687. {
  2688. // Initialise ftStatus to something other than FT_OK
  2689. FT_STATUS ftStatus = FT_STATUS.FT_OTHER_ERROR;
  2690. // If the DLL hasn't been loaded, just return here
  2691. if (hFTD2XXDLL == IntPtr.Zero)
  2692. return ftStatus;
  2693. // Check for our required function pointers being set up
  2694. if (pFT_StopInTask != IntPtr.Zero)
  2695. {
  2696. tFT_StopInTask FT_StopInTask = (tFT_StopInTask)Marshal.GetDelegateForFunctionPointer(pFT_StopInTask, typeof(tFT_StopInTask));
  2697. if (ftHandle != IntPtr.Zero)
  2698. {
  2699. // Call FT_StopInTask
  2700. ftStatus = FT_StopInTask(ftHandle);
  2701. }
  2702. }
  2703. else
  2704. {
  2705. if (pFT_StopInTask == IntPtr.Zero)
  2706. {
  2707. MessageBox.Show("Failed to load function FT_StopInTask.");
  2708. }
  2709. }
  2710. return ftStatus;
  2711. }
  2712. //**************************************************************************
  2713. // RestartInTask
  2714. //**************************************************************************
  2715. // Intellisense comments
  2716. /// <summary>
  2717. /// Resumes the driver issuing USB in requests.
  2718. /// </summary>
  2719. /// <returns>FT_STATUS value from FT_RestartInTask in FTD2XX.DLL</returns>
  2720. public FT_STATUS RestartInTask()
  2721. {
  2722. // Initialise ftStatus to something other than FT_OK
  2723. FT_STATUS ftStatus = FT_STATUS.FT_OTHER_ERROR;
  2724. // If the DLL hasn't been loaded, just return here
  2725. if (hFTD2XXDLL == IntPtr.Zero)
  2726. return ftStatus;
  2727. // Check for our required function pointers being set up
  2728. if (pFT_RestartInTask != IntPtr.Zero)
  2729. {
  2730. tFT_RestartInTask FT_RestartInTask = (tFT_RestartInTask)Marshal.GetDelegateForFunctionPointer(pFT_RestartInTask, typeof(tFT_RestartInTask));
  2731. if (ftHandle != IntPtr.Zero)
  2732. {
  2733. // Call FT_RestartInTask
  2734. ftStatus = FT_RestartInTask(ftHandle);
  2735. }
  2736. }
  2737. else
  2738. {
  2739. if (pFT_RestartInTask == IntPtr.Zero)
  2740. {
  2741. MessageBox.Show("Failed to load function FT_RestartInTask.");
  2742. }
  2743. }
  2744. return ftStatus;
  2745. }
  2746. //**************************************************************************
  2747. // ResetPort
  2748. //**************************************************************************
  2749. // Intellisense comments
  2750. /// <summary>
  2751. /// Resets the device port.
  2752. /// </summary>
  2753. /// <returns>FT_STATUS value from FT_ResetPort in FTD2XX.DLL</returns>
  2754. public FT_STATUS ResetPort()
  2755. {
  2756. // Initialise ftStatus to something other than FT_OK
  2757. FT_STATUS ftStatus = FT_STATUS.FT_OTHER_ERROR;
  2758. // If the DLL hasn't been loaded, just return here
  2759. if (hFTD2XXDLL == IntPtr.Zero)
  2760. return ftStatus;
  2761. // Check for our required function pointers being set up
  2762. if (pFT_ResetPort != IntPtr.Zero)
  2763. {
  2764. tFT_ResetPort FT_ResetPort = (tFT_ResetPort)Marshal.GetDelegateForFunctionPointer(pFT_ResetPort, typeof(tFT_ResetPort));
  2765. if (ftHandle != IntPtr.Zero)
  2766. {
  2767. // Call FT_ResetPort
  2768. ftStatus = FT_ResetPort(ftHandle);
  2769. }
  2770. }
  2771. else
  2772. {
  2773. if (pFT_ResetPort == IntPtr.Zero)
  2774. {
  2775. MessageBox.Show("Failed to load function FT_ResetPort.");
  2776. }
  2777. }
  2778. return ftStatus;
  2779. }
  2780. //**************************************************************************
  2781. // CyclePort
  2782. //**************************************************************************
  2783. // Intellisense comments
  2784. /// <summary>
  2785. /// Causes the device to be re-enumerated on the USB bus. This is equivalent to unplugging and replugging the device.
  2786. /// Also calls FT_Close if FT_CyclePort is successful, so no need to call this separately in the application.
  2787. /// </summary>
  2788. /// <returns>FT_STATUS value from FT_CyclePort in FTD2XX.DLL</returns>
  2789. public FT_STATUS CyclePort()
  2790. {
  2791. // Initialise ftStatus to something other than FT_OK
  2792. FT_STATUS ftStatus = FT_STATUS.FT_OTHER_ERROR;
  2793. // If the DLL hasn't been loaded, just return here
  2794. if (hFTD2XXDLL == IntPtr.Zero)
  2795. return ftStatus;
  2796. // Check for our required function pointers being set up
  2797. if ((pFT_CyclePort != IntPtr.Zero) & (pFT_Close != IntPtr.Zero))
  2798. {
  2799. tFT_CyclePort FT_CyclePort = (tFT_CyclePort)Marshal.GetDelegateForFunctionPointer(pFT_CyclePort, typeof(tFT_CyclePort));
  2800. tFT_Close FT_Close = (tFT_Close)Marshal.GetDelegateForFunctionPointer(pFT_Close, typeof(tFT_Close));
  2801. if (ftHandle != IntPtr.Zero)
  2802. {
  2803. // Call FT_CyclePort
  2804. ftStatus = FT_CyclePort(ftHandle);
  2805. if (ftStatus == FT_STATUS.FT_OK)
  2806. {
  2807. // If successful, call FT_Close
  2808. ftStatus = FT_Close(ftHandle);
  2809. if (ftStatus == FT_STATUS.FT_OK)
  2810. {
  2811. ftHandle = IntPtr.Zero;
  2812. }
  2813. }
  2814. }
  2815. }
  2816. else
  2817. {
  2818. if (pFT_CyclePort == IntPtr.Zero)
  2819. {
  2820. MessageBox.Show("Failed to load function FT_CyclePort.");
  2821. }
  2822. if (pFT_Close == IntPtr.Zero)
  2823. {
  2824. MessageBox.Show("Failed to load function FT_Close.");
  2825. }
  2826. }
  2827. return ftStatus;
  2828. }
  2829. //**************************************************************************
  2830. // Rescan
  2831. //**************************************************************************
  2832. // Intellisense comments
  2833. /// <summary>
  2834. /// Causes the system to check for USB hardware changes. This is equivalent to clicking on the "Scan for hardware changes" button in the Device Manager.
  2835. /// </summary>
  2836. /// <returns>FT_STATUS value from FT_Rescan in FTD2XX.DLL</returns>
  2837. public FT_STATUS Rescan()
  2838. {
  2839. // Initialise ftStatus to something other than FT_OK
  2840. FT_STATUS ftStatus = FT_STATUS.FT_OTHER_ERROR;
  2841. // If the DLL hasn't been loaded, just return here
  2842. if (hFTD2XXDLL == IntPtr.Zero)
  2843. return ftStatus;
  2844. // Check for our required function pointers being set up
  2845. if (pFT_Rescan != IntPtr.Zero)
  2846. {
  2847. tFT_Rescan FT_Rescan = (tFT_Rescan)Marshal.GetDelegateForFunctionPointer(pFT_Rescan, typeof(tFT_Rescan));
  2848. // Call FT_Rescan
  2849. ftStatus = FT_Rescan();
  2850. }
  2851. else
  2852. {
  2853. if (pFT_Rescan == IntPtr.Zero)
  2854. {
  2855. MessageBox.Show("Failed to load function FT_Rescan.");
  2856. }
  2857. }
  2858. return ftStatus;
  2859. }
  2860. //**************************************************************************
  2861. // Reload
  2862. //**************************************************************************
  2863. // Intellisense comments
  2864. /// <summary>
  2865. /// Forces a reload of the driver for devices with a specific VID and PID combination.
  2866. /// </summary>
  2867. /// <returns>FT_STATUS value from FT_Reload in FTD2XX.DLL</returns>
  2868. /// <remarks>If the VID and PID parameters are 0, the drivers for USB root hubs will be reloaded, causing all USB devices connected to reload their drivers</remarks>
  2869. /// <param name="VendorID">Vendor ID of the devices to have the driver reloaded</param>
  2870. /// <param name="ProductID">Product ID of the devices to have the driver reloaded</param>
  2871. public FT_STATUS Reload(UInt16 VendorID, UInt16 ProductID)
  2872. {
  2873. // Initialise ftStatus to something other than FT_OK
  2874. FT_STATUS ftStatus = FT_STATUS.FT_OTHER_ERROR;
  2875. // If the DLL hasn't been loaded, just return here
  2876. if (hFTD2XXDLL == IntPtr.Zero)
  2877. return ftStatus;
  2878. // Check for our required function pointers being set up
  2879. if (pFT_Reload != IntPtr.Zero)
  2880. {
  2881. tFT_Reload FT_Reload = (tFT_Reload)Marshal.GetDelegateForFunctionPointer(pFT_Reload, typeof(tFT_Reload));
  2882. // Call FT_Reload
  2883. ftStatus = FT_Reload(VendorID, ProductID);
  2884. }
  2885. else
  2886. {
  2887. if (pFT_Reload == IntPtr.Zero)
  2888. {
  2889. MessageBox.Show("Failed to load function FT_Reload.");
  2890. }
  2891. }
  2892. return ftStatus;
  2893. }
  2894. //**************************************************************************
  2895. // SetBitMode
  2896. //**************************************************************************
  2897. // Intellisense comments
  2898. /// <summary>
  2899. /// Puts the device in a mode other than the default UART or FIFO mode.
  2900. /// </summary>
  2901. /// <returns>FT_STATUS value from FT_SetBitMode in FTD2XX.DLL</returns>
  2902. /// <param name="Mask">Sets up which bits are inputs and which are outputs. A bit value of 0 sets the corresponding pin to an input, a bit value of 1 sets the corresponding pin to an output.
  2903. /// In the case of CBUS Bit Bang, the upper nibble of this value controls which pins are inputs and outputs, while the lower nibble controls which of the outputs are high and low.</param>
  2904. /// <param name="BitMode"> For FT232H devices, valid values are FT_BIT_MODE_RESET, FT_BIT_MODE_ASYNC_BITBANG, FT_BIT_MODE_MPSSE, FT_BIT_MODE_SYNC_BITBANG, FT_BIT_MODE_CBUS_BITBANG, FT_BIT_MODE_MCU_HOST, FT_BIT_MODE_FAST_SERIAL, FT_BIT_MODE_SYNC_FIFO.
  2905. /// For FT2232H devices, valid values are FT_BIT_MODE_RESET, FT_BIT_MODE_ASYNC_BITBANG, FT_BIT_MODE_MPSSE, FT_BIT_MODE_SYNC_BITBANG, FT_BIT_MODE_MCU_HOST, FT_BIT_MODE_FAST_SERIAL, FT_BIT_MODE_SYNC_FIFO.
  2906. /// For FT4232H devices, valid values are FT_BIT_MODE_RESET, FT_BIT_MODE_ASYNC_BITBANG, FT_BIT_MODE_MPSSE, FT_BIT_MODE_SYNC_BITBANG.
  2907. /// For FT232R devices, valid values are FT_BIT_MODE_RESET, FT_BIT_MODE_ASYNC_BITBANG, FT_BIT_MODE_SYNC_BITBANG, FT_BIT_MODE_CBUS_BITBANG.
  2908. /// For FT245R devices, valid values are FT_BIT_MODE_RESET, FT_BIT_MODE_ASYNC_BITBANG, FT_BIT_MODE_SYNC_BITBANG.
  2909. /// For FT2232 devices, valid values are FT_BIT_MODE_RESET, FT_BIT_MODE_ASYNC_BITBANG, FT_BIT_MODE_MPSSE, FT_BIT_MODE_SYNC_BITBANG, FT_BIT_MODE_MCU_HOST, FT_BIT_MODE_FAST_SERIAL.
  2910. /// For FT232B and FT245B devices, valid values are FT_BIT_MODE_RESET, FT_BIT_MODE_ASYNC_BITBANG.</param>
  2911. /// <exception cref="FT_EXCEPTION">Thrown when the current device does not support the requested bit mode.</exception>
  2912. public FT_STATUS SetBitMode(byte Mask, byte BitMode)
  2913. {
  2914. // Initialise ftStatus to something other than FT_OK
  2915. FT_STATUS ftStatus = FT_STATUS.FT_OTHER_ERROR;
  2916. FT_ERROR ftErrorCondition = FT_ERROR.FT_NO_ERROR;
  2917. // If the DLL hasn't been loaded, just return here
  2918. if (hFTD2XXDLL == IntPtr.Zero)
  2919. return ftStatus;
  2920. // Check for our required function pointers being set up
  2921. if (pFT_SetBitMode != IntPtr.Zero)
  2922. {
  2923. tFT_SetBitMode FT_SetBitMode = (tFT_SetBitMode)Marshal.GetDelegateForFunctionPointer(pFT_SetBitMode, typeof(tFT_SetBitMode));
  2924. if (ftHandle != IntPtr.Zero)
  2925. {
  2926. FT_DEVICE DeviceType = FT_DEVICE.FT_DEVICE_UNKNOWN;
  2927. // Set Bit Mode does not apply to FT8U232AM, FT8U245AM or FT8U100AX devices
  2928. GetDeviceType(ref DeviceType);
  2929. if (DeviceType == FT_DEVICE.FT_DEVICE_AM)
  2930. {
  2931. // Throw an exception
  2932. ftErrorCondition = FT_ERROR.FT_INVALID_BITMODE;
  2933. ErrorHandler(ftStatus, ftErrorCondition);
  2934. }
  2935. else if (DeviceType == FT_DEVICE.FT_DEVICE_100AX)
  2936. {
  2937. // Throw an exception
  2938. ftErrorCondition = FT_ERROR.FT_INVALID_BITMODE;
  2939. ErrorHandler(ftStatus, ftErrorCondition);
  2940. }
  2941. else if ((DeviceType == FT_DEVICE.FT_DEVICE_BM) && (BitMode != FT_BIT_MODES.FT_BIT_MODE_RESET))
  2942. {
  2943. if ((BitMode & (FT_BIT_MODES.FT_BIT_MODE_ASYNC_BITBANG)) == 0)
  2944. {
  2945. // Throw an exception
  2946. ftErrorCondition = FT_ERROR.FT_INVALID_BITMODE;
  2947. ErrorHandler(ftStatus, ftErrorCondition);
  2948. }
  2949. }
  2950. else if ((DeviceType == FT_DEVICE.FT_DEVICE_2232) && (BitMode != FT_BIT_MODES.FT_BIT_MODE_RESET))
  2951. {
  2952. if ((BitMode & (FT_BIT_MODES.FT_BIT_MODE_ASYNC_BITBANG | FT_BIT_MODES.FT_BIT_MODE_MPSSE | FT_BIT_MODES.FT_BIT_MODE_SYNC_BITBANG | FT_BIT_MODES.FT_BIT_MODE_MCU_HOST | FT_BIT_MODES.FT_BIT_MODE_FAST_SERIAL)) == 0)
  2953. {
  2954. // Throw an exception
  2955. ftErrorCondition = FT_ERROR.FT_INVALID_BITMODE;
  2956. ErrorHandler(ftStatus, ftErrorCondition);
  2957. }
  2958. if ((BitMode == FT_BIT_MODES.FT_BIT_MODE_MPSSE) & (InterfaceIdentifier != "A"))
  2959. {
  2960. // MPSSE mode is only available on channel A
  2961. // Throw an exception
  2962. ftErrorCondition = FT_ERROR.FT_INVALID_BITMODE;
  2963. ErrorHandler(ftStatus, ftErrorCondition);
  2964. }
  2965. }
  2966. else if ((DeviceType == FT_DEVICE.FT_DEVICE_232R) && (BitMode != FT_BIT_MODES.FT_BIT_MODE_RESET))
  2967. {
  2968. if ((BitMode & (FT_BIT_MODES.FT_BIT_MODE_ASYNC_BITBANG | FT_BIT_MODES.FT_BIT_MODE_SYNC_BITBANG | FT_BIT_MODES.FT_BIT_MODE_CBUS_BITBANG)) == 0)
  2969. {
  2970. // Throw an exception
  2971. ftErrorCondition = FT_ERROR.FT_INVALID_BITMODE;
  2972. ErrorHandler(ftStatus, ftErrorCondition);
  2973. }
  2974. }
  2975. else if ((DeviceType == FT_DEVICE.FT_DEVICE_2232H) && (BitMode != FT_BIT_MODES.FT_BIT_MODE_RESET))
  2976. {
  2977. if ((BitMode & (FT_BIT_MODES.FT_BIT_MODE_ASYNC_BITBANG | FT_BIT_MODES.FT_BIT_MODE_MPSSE | FT_BIT_MODES.FT_BIT_MODE_SYNC_BITBANG | FT_BIT_MODES.FT_BIT_MODE_MCU_HOST | FT_BIT_MODES.FT_BIT_MODE_FAST_SERIAL | FT_BIT_MODES.FT_BIT_MODE_SYNC_FIFO)) == 0)
  2978. {
  2979. // Throw an exception
  2980. ftErrorCondition = FT_ERROR.FT_INVALID_BITMODE;
  2981. ErrorHandler(ftStatus, ftErrorCondition);
  2982. }
  2983. if (((BitMode == FT_BIT_MODES.FT_BIT_MODE_MCU_HOST) | (BitMode == FT_BIT_MODES.FT_BIT_MODE_SYNC_FIFO)) & (InterfaceIdentifier != "A"))
  2984. {
  2985. // MCU Host Emulation and Single channel synchronous 245 FIFO mode is only available on channel A
  2986. // Throw an exception
  2987. ftErrorCondition = FT_ERROR.FT_INVALID_BITMODE;
  2988. ErrorHandler(ftStatus, ftErrorCondition);
  2989. }
  2990. }
  2991. else if ((DeviceType == FT_DEVICE.FT_DEVICE_4232H) && (BitMode != FT_BIT_MODES.FT_BIT_MODE_RESET))
  2992. {
  2993. if ((BitMode & (FT_BIT_MODES.FT_BIT_MODE_ASYNC_BITBANG | FT_BIT_MODES.FT_BIT_MODE_MPSSE | FT_BIT_MODES.FT_BIT_MODE_SYNC_BITBANG)) == 0)
  2994. {
  2995. // Throw an exception
  2996. ftErrorCondition = FT_ERROR.FT_INVALID_BITMODE;
  2997. ErrorHandler(ftStatus, ftErrorCondition);
  2998. }
  2999. if ((BitMode == FT_BIT_MODES.FT_BIT_MODE_MPSSE) & ((InterfaceIdentifier != "A") & (InterfaceIdentifier != "B")))
  3000. {
  3001. // MPSSE mode is only available on channel A and B
  3002. // Throw an exception
  3003. ftErrorCondition = FT_ERROR.FT_INVALID_BITMODE;
  3004. ErrorHandler(ftStatus, ftErrorCondition);
  3005. }
  3006. }
  3007. else if ((DeviceType == FT_DEVICE.FT_DEVICE_232H) && (BitMode != FT_BIT_MODES.FT_BIT_MODE_RESET))
  3008. {
  3009. // FT232H supports all current bit modes!
  3010. if (BitMode > FT_BIT_MODES.FT_BIT_MODE_SYNC_FIFO)
  3011. {
  3012. // Throw an exception
  3013. ftErrorCondition = FT_ERROR.FT_INVALID_BITMODE;
  3014. ErrorHandler(ftStatus, ftErrorCondition);
  3015. }
  3016. }
  3017. // Requested bit mode is supported
  3018. // Note FT_BIT_MODES.FT_BIT_MODE_RESET falls through to here - no bits set so cannot check for AND
  3019. // Call FT_SetBitMode
  3020. ftStatus = FT_SetBitMode(ftHandle, Mask, BitMode);
  3021. }
  3022. }
  3023. else
  3024. {
  3025. if (pFT_SetBitMode == IntPtr.Zero)
  3026. {
  3027. MessageBox.Show("Failed to load function FT_SetBitMode.");
  3028. }
  3029. }
  3030. return ftStatus;
  3031. }
  3032. //**************************************************************************
  3033. // GetPinStates
  3034. //**************************************************************************
  3035. // Intellisense comments
  3036. /// <summary>
  3037. /// Gets the instantaneous state of the device IO pins.
  3038. /// </summary>
  3039. /// <returns>FT_STATUS value from FT_GetBitMode in FTD2XX.DLL</returns>
  3040. /// <param name="BitMode">A bitmap value containing the instantaneous state of the device IO pins</param>
  3041. public FT_STATUS GetPinStates(ref byte BitMode)
  3042. {
  3043. // Initialise ftStatus to something other than FT_OK
  3044. FT_STATUS ftStatus = FT_STATUS.FT_OTHER_ERROR;
  3045. // If the DLL hasn't been loaded, just return here
  3046. if (hFTD2XXDLL == IntPtr.Zero)
  3047. return ftStatus;
  3048. // Check for our required function pointers being set up
  3049. if (pFT_GetBitMode != IntPtr.Zero)
  3050. {
  3051. tFT_GetBitMode FT_GetBitMode = (tFT_GetBitMode)Marshal.GetDelegateForFunctionPointer(pFT_GetBitMode, typeof(tFT_GetBitMode));
  3052. if (ftHandle != IntPtr.Zero)
  3053. {
  3054. // Call FT_GetBitMode
  3055. ftStatus = FT_GetBitMode(ftHandle, ref BitMode);
  3056. }
  3057. }
  3058. else
  3059. {
  3060. if (pFT_GetBitMode == IntPtr.Zero)
  3061. {
  3062. MessageBox.Show("Failed to load function FT_GetBitMode.");
  3063. }
  3064. }
  3065. return ftStatus;
  3066. }
  3067. //**************************************************************************
  3068. // ReadEEPROMLocation
  3069. //**************************************************************************
  3070. // Intellisense comments
  3071. /// <summary>
  3072. /// Reads an individual word value from a specified location in the device's EEPROM.
  3073. /// </summary>
  3074. /// <returns>FT_STATUS value from FT_ReadEE in FTD2XX.DLL</returns>
  3075. /// <param name="Address">The EEPROM location to read data from</param>
  3076. /// <param name="EEValue">The WORD value read from the EEPROM location specified in the Address paramter</param>
  3077. public FT_STATUS ReadEEPROMLocation(UInt32 Address, ref UInt16 EEValue)
  3078. {
  3079. // Initialise ftStatus to something other than FT_OK
  3080. FT_STATUS ftStatus = FT_STATUS.FT_OTHER_ERROR;
  3081. // If the DLL hasn't been loaded, just return here
  3082. if (hFTD2XXDLL == IntPtr.Zero)
  3083. return ftStatus;
  3084. // Check for our required function pointers being set up
  3085. if (pFT_ReadEE != IntPtr.Zero)
  3086. {
  3087. tFT_ReadEE FT_ReadEE = (tFT_ReadEE)Marshal.GetDelegateForFunctionPointer(pFT_ReadEE, typeof(tFT_ReadEE));
  3088. if (ftHandle != IntPtr.Zero)
  3089. {
  3090. // Call FT_ReadEE
  3091. ftStatus = FT_ReadEE(ftHandle, Address, ref EEValue);
  3092. }
  3093. }
  3094. else
  3095. {
  3096. if (pFT_ReadEE == IntPtr.Zero)
  3097. {
  3098. MessageBox.Show("Failed to load function FT_ReadEE.");
  3099. }
  3100. }
  3101. return ftStatus;
  3102. }
  3103. //**************************************************************************
  3104. // WriteEEPROMLocation
  3105. //**************************************************************************
  3106. // Intellisense comments
  3107. /// <summary>
  3108. /// Writes an individual word value to a specified location in the device's EEPROM.
  3109. /// </summary>
  3110. /// <returns>FT_STATUS value from FT_WriteEE in FTD2XX.DLL</returns>
  3111. /// <param name="Address">The EEPROM location to read data from</param>
  3112. /// <param name="EEValue">The WORD value to write to the EEPROM location specified by the Address parameter</param>
  3113. public FT_STATUS WriteEEPROMLocation(UInt32 Address, UInt16 EEValue)
  3114. {
  3115. // Initialise ftStatus to something other than FT_OK
  3116. FT_STATUS ftStatus = FT_STATUS.FT_OTHER_ERROR;
  3117. // If the DLL hasn't been loaded, just return here
  3118. if (hFTD2XXDLL == IntPtr.Zero)
  3119. return ftStatus;
  3120. // Check for our required function pointers being set up
  3121. if (pFT_WriteEE != IntPtr.Zero)
  3122. {
  3123. tFT_WriteEE FT_WriteEE = (tFT_WriteEE)Marshal.GetDelegateForFunctionPointer(pFT_WriteEE, typeof(tFT_WriteEE));
  3124. if (ftHandle != IntPtr.Zero)
  3125. {
  3126. // Call FT_WriteEE
  3127. ftStatus = FT_WriteEE(ftHandle, Address, EEValue);
  3128. }
  3129. }
  3130. else
  3131. {
  3132. if (pFT_WriteEE == IntPtr.Zero)
  3133. {
  3134. MessageBox.Show("Failed to load function FT_WriteEE.");
  3135. }
  3136. }
  3137. return ftStatus;
  3138. }
  3139. //**************************************************************************
  3140. // EraseEEPROM
  3141. //**************************************************************************
  3142. // Intellisense comments
  3143. /// <summary>
  3144. /// Erases the device EEPROM.
  3145. /// </summary>
  3146. /// <returns>FT_STATUS value from FT_EraseEE in FTD2XX.DLL</returns>
  3147. /// <exception cref="FT_EXCEPTION">Thrown when attempting to erase the EEPROM of a device with an internal EEPROM such as an FT232R or FT245R.</exception>
  3148. public FT_STATUS EraseEEPROM()
  3149. {
  3150. // Initialise ftStatus to something other than FT_OK
  3151. FT_STATUS ftStatus = FT_STATUS.FT_OTHER_ERROR;
  3152. FT_ERROR ftErrorCondition = FT_ERROR.FT_NO_ERROR;
  3153. // If the DLL hasn't been loaded, just return here
  3154. if (hFTD2XXDLL == IntPtr.Zero)
  3155. return ftStatus;
  3156. // Check for our required function pointers being set up
  3157. if (pFT_EraseEE != IntPtr.Zero)
  3158. {
  3159. tFT_EraseEE FT_EraseEE = (tFT_EraseEE)Marshal.GetDelegateForFunctionPointer(pFT_EraseEE, typeof(tFT_EraseEE));
  3160. if (ftHandle != IntPtr.Zero)
  3161. {
  3162. FT_DEVICE DeviceType = FT_DEVICE.FT_DEVICE_UNKNOWN;
  3163. // Check that it is not an FT232R or FT245R that we are trying to erase
  3164. GetDeviceType(ref DeviceType);
  3165. if (DeviceType == FT_DEVICE.FT_DEVICE_232R)
  3166. {
  3167. // If it is a device with an internal EEPROM, throw an exception
  3168. ftErrorCondition = FT_ERROR.FT_INCORRECT_DEVICE;
  3169. ErrorHandler(ftStatus, ftErrorCondition);
  3170. }
  3171. // Call FT_EraseEE
  3172. ftStatus = FT_EraseEE(ftHandle);
  3173. }
  3174. }
  3175. else
  3176. {
  3177. if (pFT_EraseEE == IntPtr.Zero)
  3178. {
  3179. MessageBox.Show("Failed to load function FT_EraseEE.");
  3180. }
  3181. }
  3182. return ftStatus;
  3183. }
  3184. //**************************************************************************
  3185. // ReadFT232BEEPROM
  3186. //**************************************************************************
  3187. // Intellisense comments
  3188. /// <summary>
  3189. /// Reads the EEPROM contents of an FT232B or FT245B device.
  3190. /// </summary>
  3191. /// <returns>FT_STATUS value from FT_EE_Read in FTD2XX DLL</returns>
  3192. /// <param name="ee232b">An FT232B_EEPROM_STRUCTURE which contains only the relevant information for an FT232B and FT245B device.</param>
  3193. /// <exception cref="FT_EXCEPTION">Thrown when the current device does not match the type required by this method.</exception>
  3194. public FT_STATUS ReadFT232BEEPROM(FT232B_EEPROM_STRUCTURE ee232b)
  3195. {
  3196. // Initialise ftStatus to something other than FT_OK
  3197. FT_STATUS ftStatus = FT_STATUS.FT_OTHER_ERROR;
  3198. FT_ERROR ftErrorCondition = FT_ERROR.FT_NO_ERROR;
  3199. // If the DLL hasn't been loaded, just return here
  3200. if (hFTD2XXDLL == IntPtr.Zero)
  3201. return ftStatus;
  3202. // Check for our required function pointers being set up
  3203. if (pFT_EE_Read != IntPtr.Zero)
  3204. {
  3205. tFT_EE_Read FT_EE_Read = (tFT_EE_Read)Marshal.GetDelegateForFunctionPointer(pFT_EE_Read, typeof(tFT_EE_Read));
  3206. if (ftHandle != IntPtr.Zero)
  3207. {
  3208. FT_DEVICE DeviceType = FT_DEVICE.FT_DEVICE_UNKNOWN;
  3209. // Check that it is an FT232B or FT245B that we are trying to read
  3210. GetDeviceType(ref DeviceType);
  3211. if (DeviceType != FT_DEVICE.FT_DEVICE_BM)
  3212. {
  3213. // If it is not, throw an exception
  3214. ftErrorCondition = FT_ERROR.FT_INCORRECT_DEVICE;
  3215. ErrorHandler(ftStatus, ftErrorCondition);
  3216. }
  3217. FT_PROGRAM_DATA eedata = new FT_PROGRAM_DATA();
  3218. // Set up structure headers
  3219. eedata.Signature1 = 0x00000000;
  3220. eedata.Signature2 = 0xFFFFFFFF;
  3221. eedata.Version = 2;
  3222. // Allocate space from unmanaged heap
  3223. eedata.Manufacturer = Marshal.AllocHGlobal(32);
  3224. eedata.ManufacturerID = Marshal.AllocHGlobal(16);
  3225. eedata.Description = Marshal.AllocHGlobal(64);
  3226. eedata.SerialNumber = Marshal.AllocHGlobal(16);
  3227. // Call FT_EE_Read
  3228. ftStatus = FT_EE_Read(ftHandle, eedata);
  3229. // Retrieve string values
  3230. ee232b.Manufacturer = Marshal.PtrToStringAnsi(eedata.Manufacturer);
  3231. ee232b.ManufacturerID = Marshal.PtrToStringAnsi(eedata.ManufacturerID);
  3232. ee232b.Description = Marshal.PtrToStringAnsi(eedata.Description);
  3233. ee232b.SerialNumber = Marshal.PtrToStringAnsi(eedata.SerialNumber);
  3234. // Free unmanaged buffers
  3235. Marshal.FreeHGlobal(eedata.Manufacturer);
  3236. Marshal.FreeHGlobal(eedata.ManufacturerID);
  3237. Marshal.FreeHGlobal(eedata.Description);
  3238. Marshal.FreeHGlobal(eedata.SerialNumber);
  3239. // Map non-string elements to structure to be returned
  3240. // Standard elements
  3241. ee232b.VendorID = eedata.VendorID;
  3242. ee232b.ProductID = eedata.ProductID;
  3243. ee232b.MaxPower = eedata.MaxPower;
  3244. ee232b.SelfPowered = Convert.ToBoolean(eedata.SelfPowered);
  3245. ee232b.RemoteWakeup = Convert.ToBoolean(eedata.RemoteWakeup);
  3246. // B specific fields
  3247. ee232b.PullDownEnable = Convert.ToBoolean(eedata.PullDownEnable);
  3248. ee232b.SerNumEnable = Convert.ToBoolean(eedata.SerNumEnable);
  3249. ee232b.USBVersionEnable = Convert.ToBoolean(eedata.USBVersionEnable);
  3250. ee232b.USBVersion = eedata.USBVersion;
  3251. }
  3252. }
  3253. else
  3254. {
  3255. if (pFT_EE_Read == IntPtr.Zero)
  3256. {
  3257. MessageBox.Show("Failed to load function FT_EE_Read.");
  3258. }
  3259. }
  3260. return ftStatus;
  3261. }
  3262. //**************************************************************************
  3263. // ReadFT2232EEPROM
  3264. //**************************************************************************
  3265. // Intellisense comments
  3266. /// <summary>
  3267. /// Reads the EEPROM contents of an FT2232 device.
  3268. /// </summary>
  3269. /// <returns>FT_STATUS value from FT_EE_Read in FTD2XX DLL</returns>
  3270. /// <param name="ee2232">An FT2232_EEPROM_STRUCTURE which contains only the relevant information for an FT2232 device.</param>
  3271. /// <exception cref="FT_EXCEPTION">Thrown when the current device does not match the type required by this method.</exception>
  3272. public FT_STATUS ReadFT2232EEPROM(FT2232_EEPROM_STRUCTURE ee2232)
  3273. {
  3274. // Initialise ftStatus to something other than FT_OK
  3275. FT_STATUS ftStatus = FT_STATUS.FT_OTHER_ERROR;
  3276. FT_ERROR ftErrorCondition = FT_ERROR.FT_NO_ERROR;
  3277. // If the DLL hasn't been loaded, just return here
  3278. if (hFTD2XXDLL == IntPtr.Zero)
  3279. return ftStatus;
  3280. // Check for our required function pointers being set up
  3281. if (pFT_EE_Read != IntPtr.Zero)
  3282. {
  3283. tFT_EE_Read FT_EE_Read = (tFT_EE_Read)Marshal.GetDelegateForFunctionPointer(pFT_EE_Read, typeof(tFT_EE_Read));
  3284. if (ftHandle != IntPtr.Zero)
  3285. {
  3286. FT_DEVICE DeviceType = FT_DEVICE.FT_DEVICE_UNKNOWN;
  3287. // Check that it is an FT2232 that we are trying to read
  3288. GetDeviceType(ref DeviceType);
  3289. if (DeviceType != FT_DEVICE.FT_DEVICE_2232)
  3290. {
  3291. // If it is not, throw an exception
  3292. ftErrorCondition = FT_ERROR.FT_INCORRECT_DEVICE;
  3293. ErrorHandler(ftStatus, ftErrorCondition);
  3294. }
  3295. FT_PROGRAM_DATA eedata = new FT_PROGRAM_DATA();
  3296. // Set up structure headers
  3297. eedata.Signature1 = 0x00000000;
  3298. eedata.Signature2 = 0xFFFFFFFF;
  3299. eedata.Version = 2;
  3300. // Allocate space from unmanaged heap
  3301. eedata.Manufacturer = Marshal.AllocHGlobal(32);
  3302. eedata.ManufacturerID = Marshal.AllocHGlobal(16);
  3303. eedata.Description = Marshal.AllocHGlobal(64);
  3304. eedata.SerialNumber = Marshal.AllocHGlobal(16);
  3305. // Call FT_EE_Read
  3306. ftStatus = FT_EE_Read(ftHandle, eedata);
  3307. // Retrieve string values
  3308. ee2232.Manufacturer = Marshal.PtrToStringAnsi(eedata.Manufacturer);
  3309. ee2232.ManufacturerID = Marshal.PtrToStringAnsi(eedata.ManufacturerID);
  3310. ee2232.Description = Marshal.PtrToStringAnsi(eedata.Description);
  3311. ee2232.SerialNumber = Marshal.PtrToStringAnsi(eedata.SerialNumber);
  3312. // Free unmanaged buffers
  3313. Marshal.FreeHGlobal(eedata.Manufacturer);
  3314. Marshal.FreeHGlobal(eedata.ManufacturerID);
  3315. Marshal.FreeHGlobal(eedata.Description);
  3316. Marshal.FreeHGlobal(eedata.SerialNumber);
  3317. // Map non-string elements to structure to be returned
  3318. // Standard elements
  3319. ee2232.VendorID = eedata.VendorID;
  3320. ee2232.ProductID = eedata.ProductID;
  3321. ee2232.MaxPower = eedata.MaxPower;
  3322. ee2232.SelfPowered = Convert.ToBoolean(eedata.SelfPowered);
  3323. ee2232.RemoteWakeup = Convert.ToBoolean(eedata.RemoteWakeup);
  3324. // 2232 specific fields
  3325. ee2232.PullDownEnable = Convert.ToBoolean(eedata.PullDownEnable5);
  3326. ee2232.SerNumEnable = Convert.ToBoolean(eedata.SerNumEnable5);
  3327. ee2232.USBVersionEnable = Convert.ToBoolean(eedata.USBVersionEnable5);
  3328. ee2232.USBVersion = eedata.USBVersion5;
  3329. ee2232.AIsHighCurrent = Convert.ToBoolean(eedata.AIsHighCurrent);
  3330. ee2232.BIsHighCurrent = Convert.ToBoolean(eedata.BIsHighCurrent);
  3331. ee2232.IFAIsFifo = Convert.ToBoolean(eedata.IFAIsFifo);
  3332. ee2232.IFAIsFifoTar = Convert.ToBoolean(eedata.IFAIsFifoTar);
  3333. ee2232.IFAIsFastSer = Convert.ToBoolean(eedata.IFAIsFastSer);
  3334. ee2232.AIsVCP = Convert.ToBoolean(eedata.AIsVCP);
  3335. ee2232.IFBIsFifo = Convert.ToBoolean(eedata.IFBIsFifo);
  3336. ee2232.IFBIsFifoTar = Convert.ToBoolean(eedata.IFBIsFifoTar);
  3337. ee2232.IFBIsFastSer = Convert.ToBoolean(eedata.IFBIsFastSer);
  3338. ee2232.BIsVCP = Convert.ToBoolean(eedata.BIsVCP);
  3339. }
  3340. }
  3341. else
  3342. {
  3343. if (pFT_EE_Read == IntPtr.Zero)
  3344. {
  3345. MessageBox.Show("Failed to load function FT_EE_Read.");
  3346. }
  3347. }
  3348. return ftStatus;
  3349. }
  3350. //**************************************************************************
  3351. // ReadFT232REEPROM
  3352. //**************************************************************************
  3353. // Intellisense comments
  3354. /// <summary>
  3355. /// Reads the EEPROM contents of an FT232R or FT245R device.
  3356. /// Calls FT_EE_Read in FTD2XX DLL
  3357. /// </summary>
  3358. /// <returns>An FT232R_EEPROM_STRUCTURE which contains only the relevant information for an FT232R and FT245R device.</returns>
  3359. /// <exception cref="FT_EXCEPTION">Thrown when the current device does not match the type required by this method.</exception>
  3360. public FT_STATUS ReadFT232REEPROM(FT232R_EEPROM_STRUCTURE ee232r)
  3361. {
  3362. // Initialise ftStatus to something other than FT_OK
  3363. FT_STATUS ftStatus = FT_STATUS.FT_OTHER_ERROR;
  3364. FT_ERROR ftErrorCondition = FT_ERROR.FT_NO_ERROR;
  3365. // If the DLL hasn't been loaded, just return here
  3366. if (hFTD2XXDLL == IntPtr.Zero)
  3367. return ftStatus;
  3368. // Check for our required function pointers being set up
  3369. if (pFT_EE_Read != IntPtr.Zero)
  3370. {
  3371. tFT_EE_Read FT_EE_Read = (tFT_EE_Read)Marshal.GetDelegateForFunctionPointer(pFT_EE_Read, typeof(tFT_EE_Read));
  3372. if (ftHandle != IntPtr.Zero)
  3373. {
  3374. FT_DEVICE DeviceType = FT_DEVICE.FT_DEVICE_UNKNOWN;
  3375. // Check that it is an FT232R or FT245R that we are trying to read
  3376. GetDeviceType(ref DeviceType);
  3377. if (DeviceType != FT_DEVICE.FT_DEVICE_232R)
  3378. {
  3379. // If it is not, throw an exception
  3380. ftErrorCondition = FT_ERROR.FT_INCORRECT_DEVICE;
  3381. ErrorHandler(ftStatus, ftErrorCondition);
  3382. }
  3383. FT_PROGRAM_DATA eedata = new FT_PROGRAM_DATA();
  3384. // Set up structure headers
  3385. eedata.Signature1 = 0x00000000;
  3386. eedata.Signature2 = 0xFFFFFFFF;
  3387. eedata.Version = 2;
  3388. // Allocate space from unmanaged heap
  3389. eedata.Manufacturer = Marshal.AllocHGlobal(32);
  3390. eedata.ManufacturerID = Marshal.AllocHGlobal(16);
  3391. eedata.Description = Marshal.AllocHGlobal(64);
  3392. eedata.SerialNumber = Marshal.AllocHGlobal(16);
  3393. // Call FT_EE_Read
  3394. ftStatus = FT_EE_Read(ftHandle, eedata);
  3395. // Retrieve string values
  3396. ee232r.Manufacturer = Marshal.PtrToStringAnsi(eedata.Manufacturer);
  3397. ee232r.ManufacturerID = Marshal.PtrToStringAnsi(eedata.ManufacturerID);
  3398. ee232r.Description = Marshal.PtrToStringAnsi(eedata.Description);
  3399. ee232r.SerialNumber = Marshal.PtrToStringAnsi(eedata.SerialNumber);
  3400. // Free unmanaged buffers
  3401. Marshal.FreeHGlobal(eedata.Manufacturer);
  3402. Marshal.FreeHGlobal(eedata.ManufacturerID);
  3403. Marshal.FreeHGlobal(eedata.Description);
  3404. Marshal.FreeHGlobal(eedata.SerialNumber);
  3405. // Map non-string elements to structure to be returned
  3406. // Standard elements
  3407. ee232r.VendorID = eedata.VendorID;
  3408. ee232r.ProductID = eedata.ProductID;
  3409. ee232r.MaxPower = eedata.MaxPower;
  3410. ee232r.SelfPowered = Convert.ToBoolean(eedata.SelfPowered);
  3411. ee232r.RemoteWakeup = Convert.ToBoolean(eedata.RemoteWakeup);
  3412. // 232R specific fields
  3413. ee232r.UseExtOsc = Convert.ToBoolean(eedata.UseExtOsc);
  3414. ee232r.HighDriveIOs = Convert.ToBoolean(eedata.HighDriveIOs);
  3415. ee232r.EndpointSize = eedata.EndpointSize;
  3416. ee232r.PullDownEnable = Convert.ToBoolean(eedata.PullDownEnableR);
  3417. ee232r.SerNumEnable = Convert.ToBoolean(eedata.SerNumEnableR);
  3418. ee232r.InvertTXD = Convert.ToBoolean(eedata.InvertTXD);
  3419. ee232r.InvertRXD = Convert.ToBoolean(eedata.InvertRXD);
  3420. ee232r.InvertRTS = Convert.ToBoolean(eedata.InvertRTS);
  3421. ee232r.InvertCTS = Convert.ToBoolean(eedata.InvertCTS);
  3422. ee232r.InvertDTR = Convert.ToBoolean(eedata.InvertDTR);
  3423. ee232r.InvertDSR = Convert.ToBoolean(eedata.InvertDSR);
  3424. ee232r.InvertDCD = Convert.ToBoolean(eedata.InvertDCD);
  3425. ee232r.InvertRI = Convert.ToBoolean(eedata.InvertRI);
  3426. ee232r.Cbus0 = eedata.Cbus0;
  3427. ee232r.Cbus1 = eedata.Cbus1;
  3428. ee232r.Cbus2 = eedata.Cbus2;
  3429. ee232r.Cbus3 = eedata.Cbus3;
  3430. ee232r.Cbus4 = eedata.Cbus4;
  3431. ee232r.RIsD2XX = Convert.ToBoolean(eedata.RIsD2XX);
  3432. }
  3433. }
  3434. else
  3435. {
  3436. if (pFT_EE_Read == IntPtr.Zero)
  3437. {
  3438. MessageBox.Show("Failed to load function FT_EE_Read.");
  3439. }
  3440. }
  3441. return ftStatus;
  3442. }
  3443. //**************************************************************************
  3444. // ReadFT2232HEEPROM
  3445. //**************************************************************************
  3446. // Intellisense comments
  3447. /// <summary>
  3448. /// Reads the EEPROM contents of an FT2232H device.
  3449. /// </summary>
  3450. /// <returns>FT_STATUS value from FT_EE_Read in FTD2XX DLL</returns>
  3451. /// <param name="ee2232h">An FT2232H_EEPROM_STRUCTURE which contains only the relevant information for an FT2232H device.</param>
  3452. /// <exception cref="FT_EXCEPTION">Thrown when the current device does not match the type required by this method.</exception>
  3453. public FT_STATUS ReadFT2232HEEPROM(FT2232H_EEPROM_STRUCTURE ee2232h)
  3454. {
  3455. // Initialise ftStatus to something other than FT_OK
  3456. FT_STATUS ftStatus = FT_STATUS.FT_OTHER_ERROR;
  3457. FT_ERROR ftErrorCondition = FT_ERROR.FT_NO_ERROR;
  3458. // If the DLL hasn't been loaded, just return here
  3459. if (hFTD2XXDLL == IntPtr.Zero)
  3460. return ftStatus;
  3461. // Check for our required function pointers being set up
  3462. if (pFT_EE_Read != IntPtr.Zero)
  3463. {
  3464. tFT_EE_Read FT_EE_Read = (tFT_EE_Read)Marshal.GetDelegateForFunctionPointer(pFT_EE_Read, typeof(tFT_EE_Read));
  3465. if (ftHandle != IntPtr.Zero)
  3466. {
  3467. FT_DEVICE DeviceType = FT_DEVICE.FT_DEVICE_UNKNOWN;
  3468. // Check that it is an FT2232H that we are trying to read
  3469. GetDeviceType(ref DeviceType);
  3470. if (DeviceType != FT_DEVICE.FT_DEVICE_2232H)
  3471. {
  3472. // If it is not, throw an exception
  3473. ftErrorCondition = FT_ERROR.FT_INCORRECT_DEVICE;
  3474. ErrorHandler(ftStatus, ftErrorCondition);
  3475. }
  3476. FT_PROGRAM_DATA eedata = new FT_PROGRAM_DATA();
  3477. // Set up structure headers
  3478. eedata.Signature1 = 0x00000000;
  3479. eedata.Signature2 = 0xFFFFFFFF;
  3480. eedata.Version = 3;
  3481. // Allocate space from unmanaged heap
  3482. eedata.Manufacturer = Marshal.AllocHGlobal(32);
  3483. eedata.ManufacturerID = Marshal.AllocHGlobal(16);
  3484. eedata.Description = Marshal.AllocHGlobal(64);
  3485. eedata.SerialNumber = Marshal.AllocHGlobal(16);
  3486. // Call FT_EE_Read
  3487. ftStatus = FT_EE_Read(ftHandle, eedata);
  3488. // Retrieve string values
  3489. ee2232h.Manufacturer = Marshal.PtrToStringAnsi(eedata.Manufacturer);
  3490. ee2232h.ManufacturerID = Marshal.PtrToStringAnsi(eedata.ManufacturerID);
  3491. ee2232h.Description = Marshal.PtrToStringAnsi(eedata.Description);
  3492. ee2232h.SerialNumber = Marshal.PtrToStringAnsi(eedata.SerialNumber);
  3493. // Free unmanaged buffers
  3494. Marshal.FreeHGlobal(eedata.Manufacturer);
  3495. Marshal.FreeHGlobal(eedata.ManufacturerID);
  3496. Marshal.FreeHGlobal(eedata.Description);
  3497. Marshal.FreeHGlobal(eedata.SerialNumber);
  3498. // Map non-string elements to structure to be returned
  3499. // Standard elements
  3500. ee2232h.VendorID = eedata.VendorID;
  3501. ee2232h.ProductID = eedata.ProductID;
  3502. ee2232h.MaxPower = eedata.MaxPower;
  3503. ee2232h.SelfPowered = Convert.ToBoolean(eedata.SelfPowered);
  3504. ee2232h.RemoteWakeup = Convert.ToBoolean(eedata.RemoteWakeup);
  3505. // 2232H specific fields
  3506. ee2232h.PullDownEnable = Convert.ToBoolean(eedata.PullDownEnable7);
  3507. ee2232h.SerNumEnable = Convert.ToBoolean(eedata.SerNumEnable7);
  3508. ee2232h.ALSlowSlew = Convert.ToBoolean(eedata.ALSlowSlew);
  3509. ee2232h.ALSchmittInput = Convert.ToBoolean(eedata.ALSchmittInput);
  3510. ee2232h.ALDriveCurrent = eedata.ALDriveCurrent;
  3511. ee2232h.AHSlowSlew = Convert.ToBoolean(eedata.AHSlowSlew);
  3512. ee2232h.AHSchmittInput = Convert.ToBoolean(eedata.AHSchmittInput);
  3513. ee2232h.AHDriveCurrent = eedata.AHDriveCurrent;
  3514. ee2232h.BLSlowSlew = Convert.ToBoolean(eedata.BLSlowSlew);
  3515. ee2232h.BLSchmittInput = Convert.ToBoolean(eedata.BLSchmittInput);
  3516. ee2232h.BLDriveCurrent = eedata.BLDriveCurrent;
  3517. ee2232h.BHSlowSlew = Convert.ToBoolean(eedata.BHSlowSlew);
  3518. ee2232h.BHSchmittInput = Convert.ToBoolean(eedata.BHSchmittInput);
  3519. ee2232h.BHDriveCurrent = eedata.BHDriveCurrent;
  3520. ee2232h.IFAIsFifo = Convert.ToBoolean(eedata.IFAIsFifo7);
  3521. ee2232h.IFAIsFifoTar = Convert.ToBoolean(eedata.IFAIsFifoTar7);
  3522. ee2232h.IFAIsFastSer = Convert.ToBoolean(eedata.IFAIsFastSer7);
  3523. ee2232h.AIsVCP = Convert.ToBoolean(eedata.AIsVCP7);
  3524. ee2232h.IFBIsFifo = Convert.ToBoolean(eedata.IFBIsFifo7);
  3525. ee2232h.IFBIsFifoTar = Convert.ToBoolean(eedata.IFBIsFifoTar7);
  3526. ee2232h.IFBIsFastSer = Convert.ToBoolean(eedata.IFBIsFastSer7);
  3527. ee2232h.BIsVCP = Convert.ToBoolean(eedata.BIsVCP7);
  3528. ee2232h.PowerSaveEnable = Convert.ToBoolean(eedata.PowerSaveEnable);
  3529. }
  3530. }
  3531. else
  3532. {
  3533. if (pFT_EE_Read == IntPtr.Zero)
  3534. {
  3535. MessageBox.Show("Failed to load function FT_EE_Read.");
  3536. }
  3537. }
  3538. return ftStatus;
  3539. }
  3540. //**************************************************************************
  3541. // ReadFT4232HEEPROM
  3542. //**************************************************************************
  3543. // Intellisense comments
  3544. /// <summary>
  3545. /// Reads the EEPROM contents of an FT4232H device.
  3546. /// </summary>
  3547. /// <returns>FT_STATUS value from FT_EE_Read in FTD2XX DLL</returns>
  3548. /// <param name="ee4232h">An FT4232H_EEPROM_STRUCTURE which contains only the relevant information for an FT4232H device.</param>
  3549. /// <exception cref="FT_EXCEPTION">Thrown when the current device does not match the type required by this method.</exception>
  3550. public FT_STATUS ReadFT4232HEEPROM(FT4232H_EEPROM_STRUCTURE ee4232h)
  3551. {
  3552. // Initialise ftStatus to something other than FT_OK
  3553. FT_STATUS ftStatus = FT_STATUS.FT_OTHER_ERROR;
  3554. FT_ERROR ftErrorCondition = FT_ERROR.FT_NO_ERROR;
  3555. // If the DLL hasn't been loaded, just return here
  3556. if (hFTD2XXDLL == IntPtr.Zero)
  3557. return ftStatus;
  3558. // Check for our required function pointers being set up
  3559. if (pFT_EE_Read != IntPtr.Zero)
  3560. {
  3561. tFT_EE_Read FT_EE_Read = (tFT_EE_Read)Marshal.GetDelegateForFunctionPointer(pFT_EE_Read, typeof(tFT_EE_Read));
  3562. if (ftHandle != IntPtr.Zero)
  3563. {
  3564. FT_DEVICE DeviceType = FT_DEVICE.FT_DEVICE_UNKNOWN;
  3565. // Check that it is an FT4232H that we are trying to read
  3566. GetDeviceType(ref DeviceType);
  3567. if (DeviceType != FT_DEVICE.FT_DEVICE_4232H)
  3568. {
  3569. // If it is not, throw an exception
  3570. ftErrorCondition = FT_ERROR.FT_INCORRECT_DEVICE;
  3571. ErrorHandler(ftStatus, ftErrorCondition);
  3572. }
  3573. FT_PROGRAM_DATA eedata = new FT_PROGRAM_DATA();
  3574. // Set up structure headers
  3575. eedata.Signature1 = 0x00000000;
  3576. eedata.Signature2 = 0xFFFFFFFF;
  3577. eedata.Version = 4;
  3578. // Allocate space from unmanaged heap
  3579. eedata.Manufacturer = Marshal.AllocHGlobal(32);
  3580. eedata.ManufacturerID = Marshal.AllocHGlobal(16);
  3581. eedata.Description = Marshal.AllocHGlobal(64);
  3582. eedata.SerialNumber = Marshal.AllocHGlobal(16);
  3583. // Call FT_EE_Read
  3584. ftStatus = FT_EE_Read(ftHandle, eedata);
  3585. // Retrieve string values
  3586. ee4232h.Manufacturer = Marshal.PtrToStringAnsi(eedata.Manufacturer);
  3587. ee4232h.ManufacturerID = Marshal.PtrToStringAnsi(eedata.ManufacturerID);
  3588. ee4232h.Description = Marshal.PtrToStringAnsi(eedata.Description);
  3589. ee4232h.SerialNumber = Marshal.PtrToStringAnsi(eedata.SerialNumber);
  3590. // Free unmanaged buffers
  3591. Marshal.FreeHGlobal(eedata.Manufacturer);
  3592. Marshal.FreeHGlobal(eedata.ManufacturerID);
  3593. Marshal.FreeHGlobal(eedata.Description);
  3594. Marshal.FreeHGlobal(eedata.SerialNumber);
  3595. // Map non-string elements to structure to be returned
  3596. // Standard elements
  3597. ee4232h.VendorID = eedata.VendorID;
  3598. ee4232h.ProductID = eedata.ProductID;
  3599. ee4232h.MaxPower = eedata.MaxPower;
  3600. ee4232h.SelfPowered = Convert.ToBoolean(eedata.SelfPowered);
  3601. ee4232h.RemoteWakeup = Convert.ToBoolean(eedata.RemoteWakeup);
  3602. // 4232H specific fields
  3603. ee4232h.PullDownEnable = Convert.ToBoolean(eedata.PullDownEnable8);
  3604. ee4232h.SerNumEnable = Convert.ToBoolean(eedata.SerNumEnable8);
  3605. ee4232h.ASlowSlew = Convert.ToBoolean(eedata.ASlowSlew);
  3606. ee4232h.ASchmittInput = Convert.ToBoolean(eedata.ASchmittInput);
  3607. ee4232h.ADriveCurrent = eedata.ADriveCurrent;
  3608. ee4232h.BSlowSlew = Convert.ToBoolean(eedata.BSlowSlew);
  3609. ee4232h.BSchmittInput = Convert.ToBoolean(eedata.BSchmittInput);
  3610. ee4232h.BDriveCurrent = eedata.BDriveCurrent;
  3611. ee4232h.CSlowSlew = Convert.ToBoolean(eedata.CSlowSlew);
  3612. ee4232h.CSchmittInput = Convert.ToBoolean(eedata.CSchmittInput);
  3613. ee4232h.CDriveCurrent = eedata.CDriveCurrent;
  3614. ee4232h.DSlowSlew = Convert.ToBoolean(eedata.DSlowSlew);
  3615. ee4232h.DSchmittInput = Convert.ToBoolean(eedata.DSchmittInput);
  3616. ee4232h.DDriveCurrent = eedata.DDriveCurrent;
  3617. ee4232h.ARIIsTXDEN = Convert.ToBoolean(eedata.ARIIsTXDEN);
  3618. ee4232h.BRIIsTXDEN = Convert.ToBoolean(eedata.BRIIsTXDEN);
  3619. ee4232h.CRIIsTXDEN = Convert.ToBoolean(eedata.CRIIsTXDEN);
  3620. ee4232h.DRIIsTXDEN = Convert.ToBoolean(eedata.DRIIsTXDEN);
  3621. ee4232h.AIsVCP = Convert.ToBoolean(eedata.AIsVCP8);
  3622. ee4232h.BIsVCP = Convert.ToBoolean(eedata.BIsVCP8);
  3623. ee4232h.CIsVCP = Convert.ToBoolean(eedata.CIsVCP8);
  3624. ee4232h.DIsVCP = Convert.ToBoolean(eedata.DIsVCP8);
  3625. }
  3626. }
  3627. else
  3628. {
  3629. if (pFT_EE_Read == IntPtr.Zero)
  3630. {
  3631. MessageBox.Show("Failed to load function FT_EE_Read.");
  3632. }
  3633. }
  3634. return ftStatus;
  3635. }
  3636. //**************************************************************************
  3637. // ReadFT232HEEPROM
  3638. //**************************************************************************
  3639. // Intellisense comments
  3640. /// <summary>
  3641. /// Reads the EEPROM contents of an FT232H device.
  3642. /// </summary>
  3643. /// <returns>FT_STATUS value from FT_EE_Read in FTD2XX DLL</returns>
  3644. /// <param name="ee232h">An FT232H_EEPROM_STRUCTURE which contains only the relevant information for an FT232H device.</param>
  3645. /// <exception cref="FT_EXCEPTION">Thrown when the current device does not match the type required by this method.</exception>
  3646. public FT_STATUS ReadFT232HEEPROM(FT232H_EEPROM_STRUCTURE ee232h)
  3647. {
  3648. // Initialise ftStatus to something other than FT_OK
  3649. FT_STATUS ftStatus = FT_STATUS.FT_OTHER_ERROR;
  3650. FT_ERROR ftErrorCondition = FT_ERROR.FT_NO_ERROR;
  3651. // If the DLL hasn't been loaded, just return here
  3652. if (hFTD2XXDLL == IntPtr.Zero)
  3653. return ftStatus;
  3654. // Check for our required function pointers being set up
  3655. if (pFT_EE_Read != IntPtr.Zero)
  3656. {
  3657. tFT_EE_Read FT_EE_Read = (tFT_EE_Read)Marshal.GetDelegateForFunctionPointer(pFT_EE_Read, typeof(tFT_EE_Read));
  3658. if (ftHandle != IntPtr.Zero)
  3659. {
  3660. FT_DEVICE DeviceType = FT_DEVICE.FT_DEVICE_UNKNOWN;
  3661. // Check that it is an FT232H that we are trying to read
  3662. GetDeviceType(ref DeviceType);
  3663. if (DeviceType != FT_DEVICE.FT_DEVICE_232H)
  3664. {
  3665. // If it is not, throw an exception
  3666. ftErrorCondition = FT_ERROR.FT_INCORRECT_DEVICE;
  3667. ErrorHandler(ftStatus, ftErrorCondition);
  3668. }
  3669. FT_PROGRAM_DATA eedata = new FT_PROGRAM_DATA();
  3670. // Set up structure headers
  3671. eedata.Signature1 = 0x00000000;
  3672. eedata.Signature2 = 0xFFFFFFFF;
  3673. eedata.Version = 5;
  3674. // Allocate space from unmanaged heap
  3675. eedata.Manufacturer = Marshal.AllocHGlobal(32);
  3676. eedata.ManufacturerID = Marshal.AllocHGlobal(16);
  3677. eedata.Description = Marshal.AllocHGlobal(64);
  3678. eedata.SerialNumber = Marshal.AllocHGlobal(16);
  3679. // Call FT_EE_Read
  3680. ftStatus = FT_EE_Read(ftHandle, eedata);
  3681. // Retrieve string values
  3682. ee232h.Manufacturer = Marshal.PtrToStringAnsi(eedata.Manufacturer);
  3683. ee232h.ManufacturerID = Marshal.PtrToStringAnsi(eedata.ManufacturerID);
  3684. ee232h.Description = Marshal.PtrToStringAnsi(eedata.Description);
  3685. ee232h.SerialNumber = Marshal.PtrToStringAnsi(eedata.SerialNumber);
  3686. // Free unmanaged buffers
  3687. Marshal.FreeHGlobal(eedata.Manufacturer);
  3688. Marshal.FreeHGlobal(eedata.ManufacturerID);
  3689. Marshal.FreeHGlobal(eedata.Description);
  3690. Marshal.FreeHGlobal(eedata.SerialNumber);
  3691. // Map non-string elements to structure to be returned
  3692. // Standard elements
  3693. ee232h.VendorID = eedata.VendorID;
  3694. ee232h.ProductID = eedata.ProductID;
  3695. ee232h.MaxPower = eedata.MaxPower;
  3696. ee232h.SelfPowered = Convert.ToBoolean(eedata.SelfPowered);
  3697. ee232h.RemoteWakeup = Convert.ToBoolean(eedata.RemoteWakeup);
  3698. // 232H specific fields
  3699. ee232h.PullDownEnable = Convert.ToBoolean(eedata.PullDownEnableH);
  3700. ee232h.SerNumEnable = Convert.ToBoolean(eedata.SerNumEnableH);
  3701. ee232h.ACSlowSlew = Convert.ToBoolean(eedata.ACSlowSlewH);
  3702. ee232h.ACSchmittInput = Convert.ToBoolean(eedata.ACSchmittInputH);
  3703. ee232h.ACDriveCurrent = eedata.ACDriveCurrentH;
  3704. ee232h.ADSlowSlew = Convert.ToBoolean(eedata.ADSlowSlewH);
  3705. ee232h.ADSchmittInput = Convert.ToBoolean(eedata.ADSchmittInputH);
  3706. ee232h.ADDriveCurrent = eedata.ADDriveCurrentH;
  3707. ee232h.Cbus0 = eedata.Cbus0H;
  3708. ee232h.Cbus1 = eedata.Cbus1H;
  3709. ee232h.Cbus2 = eedata.Cbus2H;
  3710. ee232h.Cbus3 = eedata.Cbus3H;
  3711. ee232h.Cbus4 = eedata.Cbus4H;
  3712. ee232h.Cbus5 = eedata.Cbus5H;
  3713. ee232h.Cbus6 = eedata.Cbus6H;
  3714. ee232h.Cbus7 = eedata.Cbus7H;
  3715. ee232h.Cbus8 = eedata.Cbus8H;
  3716. ee232h.Cbus9 = eedata.Cbus9H;
  3717. ee232h.IsFifo = Convert.ToBoolean(eedata.IsFifoH);
  3718. ee232h.IsFifoTar = Convert.ToBoolean(eedata.IsFifoTarH);
  3719. ee232h.IsFastSer = Convert.ToBoolean(eedata.IsFastSerH);
  3720. ee232h.IsFT1248 = Convert.ToBoolean(eedata.IsFT1248H);
  3721. ee232h.FT1248Cpol = Convert.ToBoolean(eedata.FT1248CpolH);
  3722. ee232h.FT1248Lsb = Convert.ToBoolean(eedata.FT1248LsbH);
  3723. ee232h.FT1248FlowControl = Convert.ToBoolean(eedata.FT1248FlowControlH);
  3724. ee232h.IsVCP = Convert.ToBoolean(eedata.IsVCPH);
  3725. ee232h.PowerSaveEnable = Convert.ToBoolean(eedata.PowerSaveEnableH);
  3726. }
  3727. }
  3728. else
  3729. {
  3730. if (pFT_EE_Read == IntPtr.Zero)
  3731. {
  3732. MessageBox.Show("Failed to load function FT_EE_Read.");
  3733. }
  3734. }
  3735. return ftStatus;
  3736. }
  3737. //**************************************************************************
  3738. // ReadXSeriesEEPROM
  3739. //**************************************************************************
  3740. // Intellisense comments
  3741. /// <summary>
  3742. /// Reads the EEPROM contents of an X-Series device.
  3743. /// </summary>
  3744. /// <returns>FT_STATUS value from FT_EEPROM_Read in FTD2XX DLL</returns>
  3745. /// <param name="eeX">An FT_XSERIES_EEPROM_STRUCTURE which contains only the relevant information for an X-Series device.</param>
  3746. /// <exception cref="FT_EXCEPTION">Thrown when the current device does not match the type required by this method.</exception>
  3747. public FT_STATUS ReadXSeriesEEPROM(FT_XSERIES_EEPROM_STRUCTURE eeX)
  3748. {
  3749. // Initialise ftStatus to something other than FT_OK
  3750. FT_STATUS ftStatus = FT_STATUS.FT_OTHER_ERROR;
  3751. FT_ERROR ftErrorCondition = FT_ERROR.FT_NO_ERROR;
  3752. // If the DLL hasn't been loaded, just return here
  3753. if (hFTD2XXDLL == IntPtr.Zero)
  3754. return ftStatus;
  3755. // Check for our required function pointers being set up
  3756. if (pFT_EEPROM_Read != IntPtr.Zero)
  3757. {
  3758. tFT_EEPROM_Read FT_EEPROM_Read = (tFT_EEPROM_Read)Marshal.GetDelegateForFunctionPointer(pFT_EEPROM_Read, typeof(tFT_EEPROM_Read));
  3759. if (ftHandle != IntPtr.Zero)
  3760. {
  3761. FT_DEVICE DeviceType = FT_DEVICE.FT_DEVICE_UNKNOWN;
  3762. // Check that it is an FT232H that we are trying to read
  3763. GetDeviceType(ref DeviceType);
  3764. if (DeviceType != FT_DEVICE.FT_DEVICE_X_SERIES)
  3765. {
  3766. // If it is not, throw an exception
  3767. ftErrorCondition = FT_ERROR.FT_INCORRECT_DEVICE;
  3768. ErrorHandler(ftStatus, ftErrorCondition);
  3769. }
  3770. FT_XSERIES_DATA eeData = new FT_XSERIES_DATA();
  3771. FT_EEPROM_HEADER eeHeader = new FT_EEPROM_HEADER();
  3772. byte[] manufacturer = new byte[32];
  3773. byte[] manufacturerID = new byte[16];
  3774. byte[] description = new byte[64];
  3775. byte[] serialNumber = new byte[16];
  3776. eeHeader.deviceType = (uint)FT_DEVICE.FT_DEVICE_X_SERIES;
  3777. eeData.common = eeHeader;
  3778. // Calculate the size of our data structure...
  3779. int size = Marshal.SizeOf(eeData);
  3780. // Allocate space for our pointer...
  3781. IntPtr eeDataMarshal = Marshal.AllocHGlobal(size);
  3782. Marshal.StructureToPtr(eeData, eeDataMarshal, false);
  3783. // Call FT_EEPROM_Read
  3784. ftStatus = FT_EEPROM_Read(ftHandle, eeDataMarshal, (uint)size, manufacturer, manufacturerID, description, serialNumber);
  3785. if (ftStatus == FT_STATUS.FT_OK)
  3786. {
  3787. // Get the data back from the pointer...
  3788. eeData = (FT_XSERIES_DATA)Marshal.PtrToStructure(eeDataMarshal, typeof(FT_XSERIES_DATA));
  3789. // Retrieve string values
  3790. System.Text.UTF8Encoding enc = new System.Text.UTF8Encoding();
  3791. eeX.Manufacturer = enc.GetString(manufacturer);
  3792. eeX.ManufacturerID = enc.GetString(manufacturerID);
  3793. eeX.Description = enc.GetString(description);
  3794. eeX.SerialNumber = enc.GetString(serialNumber);
  3795. // Map non-string elements to structure to be returned
  3796. // Standard elements
  3797. eeX.VendorID = eeData.common.VendorId;
  3798. eeX.ProductID = eeData.common.ProductId;
  3799. eeX.MaxPower = eeData.common.MaxPower;
  3800. eeX.SelfPowered = Convert.ToBoolean(eeData.common.SelfPowered);
  3801. eeX.RemoteWakeup = Convert.ToBoolean(eeData.common.RemoteWakeup);
  3802. eeX.SerNumEnable = Convert.ToBoolean(eeData.common.SerNumEnable);
  3803. eeX.PullDownEnable = Convert.ToBoolean(eeData.common.PullDownEnable);
  3804. // X-Series specific fields
  3805. // CBUS
  3806. eeX.Cbus0 = eeData.Cbus0;
  3807. eeX.Cbus1 = eeData.Cbus1;
  3808. eeX.Cbus2 = eeData.Cbus2;
  3809. eeX.Cbus3 = eeData.Cbus3;
  3810. eeX.Cbus4 = eeData.Cbus4;
  3811. eeX.Cbus5 = eeData.Cbus5;
  3812. eeX.Cbus6 = eeData.Cbus6;
  3813. // Drive Options
  3814. eeX.ACDriveCurrent = eeData.ACDriveCurrent;
  3815. eeX.ACSchmittInput = eeData.ACSchmittInput;
  3816. eeX.ACSlowSlew = eeData.ACSlowSlew;
  3817. eeX.ADDriveCurrent = eeData.ADDriveCurrent;
  3818. eeX.ADSchmittInput = eeData.ADSchmittInput;
  3819. eeX.ADSlowSlew = eeData.ADSlowSlew;
  3820. // BCD
  3821. eeX.BCDDisableSleep = eeData.BCDDisableSleep;
  3822. eeX.BCDEnable = eeData.BCDEnable;
  3823. eeX.BCDForceCbusPWREN = eeData.BCDForceCbusPWREN;
  3824. // FT1248
  3825. eeX.FT1248Cpol = eeData.FT1248Cpol;
  3826. eeX.FT1248FlowControl = eeData.FT1248FlowControl;
  3827. eeX.FT1248Lsb = eeData.FT1248Lsb;
  3828. // I2C
  3829. eeX.I2CDeviceId = eeData.I2CDeviceId;
  3830. eeX.I2CDisableSchmitt = eeData.I2CDisableSchmitt;
  3831. eeX.I2CSlaveAddress = eeData.I2CSlaveAddress;
  3832. // RS232 Signals
  3833. eeX.InvertCTS = eeData.InvertCTS;
  3834. eeX.InvertDCD = eeData.InvertDCD;
  3835. eeX.InvertDSR = eeData.InvertDSR;
  3836. eeX.InvertDTR = eeData.InvertDTR;
  3837. eeX.InvertRI = eeData.InvertRI;
  3838. eeX.InvertRTS = eeData.InvertRTS;
  3839. eeX.InvertRXD = eeData.InvertRXD;
  3840. eeX.InvertTXD = eeData.InvertTXD;
  3841. // Hardware Options
  3842. eeX.PowerSaveEnable = eeData.PowerSaveEnable;
  3843. eeX.RS485EchoSuppress = eeData.RS485EchoSuppress;
  3844. // Driver Option
  3845. eeX.IsVCP = eeData.DriverType;
  3846. }
  3847. }
  3848. }
  3849. else
  3850. {
  3851. if (pFT_EE_Read == IntPtr.Zero)
  3852. {
  3853. MessageBox.Show("Failed to load function FT_EE_Read.");
  3854. }
  3855. }
  3856. return ftStatus;
  3857. }
  3858. //**************************************************************************
  3859. // WriteFT232BEEPROM
  3860. //**************************************************************************
  3861. // Intellisense comments
  3862. /// <summary>
  3863. /// Writes the specified values to the EEPROM of an FT232B or FT245B device.
  3864. /// </summary>
  3865. /// <returns>FT_STATUS value from FT_EE_Program in FTD2XX DLL</returns>
  3866. /// <param name="ee232b">The EEPROM settings to be written to the device</param>
  3867. /// <remarks>If the strings are too long, they will be truncated to their maximum permitted lengths</remarks>
  3868. /// <exception cref="FT_EXCEPTION">Thrown when the current device does not match the type required by this method.</exception>
  3869. public FT_STATUS WriteFT232BEEPROM(FT232B_EEPROM_STRUCTURE ee232b)
  3870. {
  3871. // Initialise ftStatus to something other than FT_OK
  3872. FT_STATUS ftStatus = FT_STATUS.FT_OTHER_ERROR;
  3873. FT_ERROR ftErrorCondition = FT_ERROR.FT_NO_ERROR;
  3874. // If the DLL hasn't been loaded, just return here
  3875. if (hFTD2XXDLL == IntPtr.Zero)
  3876. return ftStatus;
  3877. // Check for our required function pointers being set up
  3878. if (pFT_EE_Program != IntPtr.Zero)
  3879. {
  3880. tFT_EE_Program FT_EE_Program = (tFT_EE_Program)Marshal.GetDelegateForFunctionPointer(pFT_EE_Program, typeof(tFT_EE_Program));
  3881. if (ftHandle != IntPtr.Zero)
  3882. {
  3883. FT_DEVICE DeviceType = FT_DEVICE.FT_DEVICE_UNKNOWN;
  3884. // Check that it is an FT232B or FT245B that we are trying to write
  3885. GetDeviceType(ref DeviceType);
  3886. if (DeviceType != FT_DEVICE.FT_DEVICE_BM)
  3887. {
  3888. // If it is not, throw an exception
  3889. ftErrorCondition = FT_ERROR.FT_INCORRECT_DEVICE;
  3890. ErrorHandler(ftStatus, ftErrorCondition);
  3891. }
  3892. // Check for VID and PID of 0x0000
  3893. if ((ee232b.VendorID == 0x0000) | (ee232b.ProductID == 0x0000))
  3894. {
  3895. // Do not allow users to program the device with VID or PID of 0x0000
  3896. return FT_STATUS.FT_INVALID_PARAMETER;
  3897. }
  3898. FT_PROGRAM_DATA eedata = new FT_PROGRAM_DATA();
  3899. // Set up structure headers
  3900. eedata.Signature1 = 0x00000000;
  3901. eedata.Signature2 = 0xFFFFFFFF;
  3902. eedata.Version = 2;
  3903. // Allocate space from unmanaged heap
  3904. eedata.Manufacturer = Marshal.AllocHGlobal(32);
  3905. eedata.ManufacturerID = Marshal.AllocHGlobal(16);
  3906. eedata.Description = Marshal.AllocHGlobal(64);
  3907. eedata.SerialNumber = Marshal.AllocHGlobal(16);
  3908. // Check lengths of strings to make sure that they are within our limits
  3909. // If not, trim them to make them our maximum length
  3910. if (ee232b.Manufacturer.Length > 32)
  3911. ee232b.Manufacturer = ee232b.Manufacturer.Substring(0, 32);
  3912. if (ee232b.ManufacturerID.Length > 16)
  3913. ee232b.ManufacturerID = ee232b.ManufacturerID.Substring(0, 16);
  3914. if (ee232b.Description.Length > 64)
  3915. ee232b.Description = ee232b.Description.Substring(0, 64);
  3916. if (ee232b.SerialNumber.Length > 16)
  3917. ee232b.SerialNumber = ee232b.SerialNumber.Substring(0, 16);
  3918. // Set string values
  3919. eedata.Manufacturer = Marshal.StringToHGlobalAnsi(ee232b.Manufacturer);
  3920. eedata.ManufacturerID = Marshal.StringToHGlobalAnsi(ee232b.ManufacturerID);
  3921. eedata.Description = Marshal.StringToHGlobalAnsi(ee232b.Description);
  3922. eedata.SerialNumber = Marshal.StringToHGlobalAnsi(ee232b.SerialNumber);
  3923. // Map non-string elements to structure
  3924. // Standard elements
  3925. eedata.VendorID = ee232b.VendorID;
  3926. eedata.ProductID = ee232b.ProductID;
  3927. eedata.MaxPower = ee232b.MaxPower;
  3928. eedata.SelfPowered = Convert.ToUInt16(ee232b.SelfPowered);
  3929. eedata.RemoteWakeup = Convert.ToUInt16(ee232b.RemoteWakeup);
  3930. // B specific fields
  3931. eedata.Rev4 = Convert.ToByte(true);
  3932. eedata.PullDownEnable = Convert.ToByte(ee232b.PullDownEnable);
  3933. eedata.SerNumEnable = Convert.ToByte(ee232b.SerNumEnable);
  3934. eedata.USBVersionEnable = Convert.ToByte(ee232b.USBVersionEnable);
  3935. eedata.USBVersion = ee232b.USBVersion;
  3936. // Call FT_EE_Program
  3937. ftStatus = FT_EE_Program(ftHandle, eedata);
  3938. // Free unmanaged buffers
  3939. Marshal.FreeHGlobal(eedata.Manufacturer);
  3940. Marshal.FreeHGlobal(eedata.ManufacturerID);
  3941. Marshal.FreeHGlobal(eedata.Description);
  3942. Marshal.FreeHGlobal(eedata.SerialNumber);
  3943. }
  3944. }
  3945. else
  3946. {
  3947. if (pFT_EE_Program == IntPtr.Zero)
  3948. {
  3949. MessageBox.Show("Failed to load function FT_EE_Program.");
  3950. }
  3951. }
  3952. return ftStatus;
  3953. }
  3954. //**************************************************************************
  3955. // WriteFT2232EEPROM
  3956. //**************************************************************************
  3957. // Intellisense comments
  3958. /// <summary>
  3959. /// Writes the specified values to the EEPROM of an FT2232 device.
  3960. /// Calls FT_EE_Program in FTD2XX DLL
  3961. /// </summary>
  3962. /// <returns>FT_STATUS value from FT_EE_Program in FTD2XX DLL</returns>
  3963. /// <param name="ee2232">The EEPROM settings to be written to the device</param>
  3964. /// <remarks>If the strings are too long, they will be truncated to their maximum permitted lengths</remarks>
  3965. /// <exception cref="FT_EXCEPTION">Thrown when the current device does not match the type required by this method.</exception>
  3966. public FT_STATUS WriteFT2232EEPROM(FT2232_EEPROM_STRUCTURE ee2232)
  3967. {
  3968. // Initialise ftStatus to something other than FT_OK
  3969. FT_STATUS ftStatus = FT_STATUS.FT_OTHER_ERROR;
  3970. FT_ERROR ftErrorCondition = FT_ERROR.FT_NO_ERROR;
  3971. // If the DLL hasn't been loaded, just return here
  3972. if (hFTD2XXDLL == IntPtr.Zero)
  3973. return ftStatus;
  3974. // Check for our required function pointers being set up
  3975. if (pFT_EE_Program != IntPtr.Zero)
  3976. {
  3977. tFT_EE_Program FT_EE_Program = (tFT_EE_Program)Marshal.GetDelegateForFunctionPointer(pFT_EE_Program, typeof(tFT_EE_Program));
  3978. if (ftHandle != IntPtr.Zero)
  3979. {
  3980. FT_DEVICE DeviceType = FT_DEVICE.FT_DEVICE_UNKNOWN;
  3981. // Check that it is an FT2232 that we are trying to write
  3982. GetDeviceType(ref DeviceType);
  3983. if (DeviceType != FT_DEVICE.FT_DEVICE_2232)
  3984. {
  3985. // If it is not, throw an exception
  3986. ftErrorCondition = FT_ERROR.FT_INCORRECT_DEVICE;
  3987. ErrorHandler(ftStatus, ftErrorCondition);
  3988. }
  3989. // Check for VID and PID of 0x0000
  3990. if ((ee2232.VendorID == 0x0000) | (ee2232.ProductID == 0x0000))
  3991. {
  3992. // Do not allow users to program the device with VID or PID of 0x0000
  3993. return FT_STATUS.FT_INVALID_PARAMETER;
  3994. }
  3995. FT_PROGRAM_DATA eedata = new FT_PROGRAM_DATA();
  3996. // Set up structure headers
  3997. eedata.Signature1 = 0x00000000;
  3998. eedata.Signature2 = 0xFFFFFFFF;
  3999. eedata.Version = 2;
  4000. // Allocate space from unmanaged heap
  4001. eedata.Manufacturer = Marshal.AllocHGlobal(32);
  4002. eedata.ManufacturerID = Marshal.AllocHGlobal(16);
  4003. eedata.Description = Marshal.AllocHGlobal(64);
  4004. eedata.SerialNumber = Marshal.AllocHGlobal(16);
  4005. // Check lengths of strings to make sure that they are within our limits
  4006. // If not, trim them to make them our maximum length
  4007. if (ee2232.Manufacturer.Length > 32)
  4008. ee2232.Manufacturer = ee2232.Manufacturer.Substring(0, 32);
  4009. if (ee2232.ManufacturerID.Length > 16)
  4010. ee2232.ManufacturerID = ee2232.ManufacturerID.Substring(0, 16);
  4011. if (ee2232.Description.Length > 64)
  4012. ee2232.Description = ee2232.Description.Substring(0, 64);
  4013. if (ee2232.SerialNumber.Length > 16)
  4014. ee2232.SerialNumber = ee2232.SerialNumber.Substring(0, 16);
  4015. // Set string values
  4016. eedata.Manufacturer = Marshal.StringToHGlobalAnsi(ee2232.Manufacturer);
  4017. eedata.ManufacturerID = Marshal.StringToHGlobalAnsi(ee2232.ManufacturerID);
  4018. eedata.Description = Marshal.StringToHGlobalAnsi(ee2232.Description);
  4019. eedata.SerialNumber = Marshal.StringToHGlobalAnsi(ee2232.SerialNumber);
  4020. // Map non-string elements to structure
  4021. // Standard elements
  4022. eedata.VendorID = ee2232.VendorID;
  4023. eedata.ProductID = ee2232.ProductID;
  4024. eedata.MaxPower = ee2232.MaxPower;
  4025. eedata.SelfPowered = Convert.ToUInt16(ee2232.SelfPowered);
  4026. eedata.RemoteWakeup = Convert.ToUInt16(ee2232.RemoteWakeup);
  4027. // 2232 specific fields
  4028. eedata.Rev5 = Convert.ToByte(true);
  4029. eedata.PullDownEnable5 = Convert.ToByte(ee2232.PullDownEnable);
  4030. eedata.SerNumEnable5 = Convert.ToByte(ee2232.SerNumEnable);
  4031. eedata.USBVersionEnable5 = Convert.ToByte(ee2232.USBVersionEnable);
  4032. eedata.USBVersion5 = ee2232.USBVersion;
  4033. eedata.AIsHighCurrent = Convert.ToByte(ee2232.AIsHighCurrent);
  4034. eedata.BIsHighCurrent = Convert.ToByte(ee2232.BIsHighCurrent);
  4035. eedata.IFAIsFifo = Convert.ToByte(ee2232.IFAIsFifo);
  4036. eedata.IFAIsFifoTar = Convert.ToByte(ee2232.IFAIsFifoTar);
  4037. eedata.IFAIsFastSer = Convert.ToByte(ee2232.IFAIsFastSer);
  4038. eedata.AIsVCP = Convert.ToByte(ee2232.AIsVCP);
  4039. eedata.IFBIsFifo = Convert.ToByte(ee2232.IFBIsFifo);
  4040. eedata.IFBIsFifoTar = Convert.ToByte(ee2232.IFBIsFifoTar);
  4041. eedata.IFBIsFastSer = Convert.ToByte(ee2232.IFBIsFastSer);
  4042. eedata.BIsVCP = Convert.ToByte(ee2232.BIsVCP);
  4043. // Call FT_EE_Program
  4044. ftStatus = FT_EE_Program(ftHandle, eedata);
  4045. // Free unmanaged buffers
  4046. Marshal.FreeHGlobal(eedata.Manufacturer);
  4047. Marshal.FreeHGlobal(eedata.ManufacturerID);
  4048. Marshal.FreeHGlobal(eedata.Description);
  4049. Marshal.FreeHGlobal(eedata.SerialNumber);
  4050. }
  4051. }
  4052. else
  4053. {
  4054. if (pFT_EE_Program == IntPtr.Zero)
  4055. {
  4056. MessageBox.Show("Failed to load function FT_EE_Program.");
  4057. }
  4058. }
  4059. return ftStatus;
  4060. }
  4061. //**************************************************************************
  4062. // WriteFT232REEPROM
  4063. //**************************************************************************
  4064. // Intellisense comments
  4065. /// <summary>
  4066. /// Writes the specified values to the EEPROM of an FT232R or FT245R device.
  4067. /// Calls FT_EE_Program in FTD2XX DLL
  4068. /// </summary>
  4069. /// <returns>FT_STATUS value from FT_EE_Program in FTD2XX DLL</returns>
  4070. /// <param name="ee232r">The EEPROM settings to be written to the device</param>
  4071. /// <remarks>If the strings are too long, they will be truncated to their maximum permitted lengths</remarks>
  4072. /// <exception cref="FT_EXCEPTION">Thrown when the current device does not match the type required by this method.</exception>
  4073. public FT_STATUS WriteFT232REEPROM(FT232R_EEPROM_STRUCTURE ee232r)
  4074. {
  4075. // Initialise ftStatus to something other than FT_OK
  4076. FT_STATUS ftStatus = FT_STATUS.FT_OTHER_ERROR;
  4077. FT_ERROR ftErrorCondition = FT_ERROR.FT_NO_ERROR;
  4078. // If the DLL hasn't been loaded, just return here
  4079. if (hFTD2XXDLL == IntPtr.Zero)
  4080. return ftStatus;
  4081. // Check for our required function pointers being set up
  4082. if (pFT_EE_Program != IntPtr.Zero)
  4083. {
  4084. tFT_EE_Program FT_EE_Program = (tFT_EE_Program)Marshal.GetDelegateForFunctionPointer(pFT_EE_Program, typeof(tFT_EE_Program));
  4085. if (ftHandle != IntPtr.Zero)
  4086. {
  4087. FT_DEVICE DeviceType = FT_DEVICE.FT_DEVICE_UNKNOWN;
  4088. // Check that it is an FT232R or FT245R that we are trying to write
  4089. GetDeviceType(ref DeviceType);
  4090. if (DeviceType != FT_DEVICE.FT_DEVICE_232R)
  4091. {
  4092. // If it is not, throw an exception
  4093. ftErrorCondition = FT_ERROR.FT_INCORRECT_DEVICE;
  4094. ErrorHandler(ftStatus, ftErrorCondition);
  4095. }
  4096. // Check for VID and PID of 0x0000
  4097. if ((ee232r.VendorID == 0x0000) | (ee232r.ProductID == 0x0000))
  4098. {
  4099. // Do not allow users to program the device with VID or PID of 0x0000
  4100. return FT_STATUS.FT_INVALID_PARAMETER;
  4101. }
  4102. FT_PROGRAM_DATA eedata = new FT_PROGRAM_DATA();
  4103. // Set up structure headers
  4104. eedata.Signature1 = 0x00000000;
  4105. eedata.Signature2 = 0xFFFFFFFF;
  4106. eedata.Version = 2;
  4107. // Allocate space from unmanaged heap
  4108. eedata.Manufacturer = Marshal.AllocHGlobal(32);
  4109. eedata.ManufacturerID = Marshal.AllocHGlobal(16);
  4110. eedata.Description = Marshal.AllocHGlobal(64);
  4111. eedata.SerialNumber = Marshal.AllocHGlobal(16);
  4112. // Check lengths of strings to make sure that they are within our limits
  4113. // If not, trim them to make them our maximum length
  4114. if (ee232r.Manufacturer.Length > 32)
  4115. ee232r.Manufacturer = ee232r.Manufacturer.Substring(0, 32);
  4116. if (ee232r.ManufacturerID.Length > 16)
  4117. ee232r.ManufacturerID = ee232r.ManufacturerID.Substring(0, 16);
  4118. if (ee232r.Description.Length > 64)
  4119. ee232r.Description = ee232r.Description.Substring(0, 64);
  4120. if (ee232r.SerialNumber.Length > 16)
  4121. ee232r.SerialNumber = ee232r.SerialNumber.Substring(0, 16);
  4122. // Set string values
  4123. eedata.Manufacturer = Marshal.StringToHGlobalAnsi(ee232r.Manufacturer);
  4124. eedata.ManufacturerID = Marshal.StringToHGlobalAnsi(ee232r.ManufacturerID);
  4125. eedata.Description = Marshal.StringToHGlobalAnsi(ee232r.Description);
  4126. eedata.SerialNumber = Marshal.StringToHGlobalAnsi(ee232r.SerialNumber);
  4127. // Map non-string elements to structure
  4128. // Standard elements
  4129. eedata.VendorID = ee232r.VendorID;
  4130. eedata.ProductID = ee232r.ProductID;
  4131. eedata.MaxPower = ee232r.MaxPower;
  4132. eedata.SelfPowered = Convert.ToUInt16(ee232r.SelfPowered);
  4133. eedata.RemoteWakeup = Convert.ToUInt16(ee232r.RemoteWakeup);
  4134. // 232R specific fields
  4135. eedata.PullDownEnableR = Convert.ToByte(ee232r.PullDownEnable);
  4136. eedata.SerNumEnableR = Convert.ToByte(ee232r.SerNumEnable);
  4137. eedata.UseExtOsc = Convert.ToByte(ee232r.UseExtOsc);
  4138. eedata.HighDriveIOs = Convert.ToByte(ee232r.HighDriveIOs);
  4139. // Override any endpoint size the user has selected and force 64 bytes
  4140. // Some users have been known to wreck devices by setting 0 here...
  4141. eedata.EndpointSize = 64;
  4142. eedata.PullDownEnableR = Convert.ToByte(ee232r.PullDownEnable);
  4143. eedata.SerNumEnableR = Convert.ToByte(ee232r.SerNumEnable);
  4144. eedata.InvertTXD = Convert.ToByte(ee232r.InvertTXD);
  4145. eedata.InvertRXD = Convert.ToByte(ee232r.InvertRXD);
  4146. eedata.InvertRTS = Convert.ToByte(ee232r.InvertRTS);
  4147. eedata.InvertCTS = Convert.ToByte(ee232r.InvertCTS);
  4148. eedata.InvertDTR = Convert.ToByte(ee232r.InvertDTR);
  4149. eedata.InvertDSR = Convert.ToByte(ee232r.InvertDSR);
  4150. eedata.InvertDCD = Convert.ToByte(ee232r.InvertDCD);
  4151. eedata.InvertRI = Convert.ToByte(ee232r.InvertRI);
  4152. eedata.Cbus0 = ee232r.Cbus0;
  4153. eedata.Cbus1 = ee232r.Cbus1;
  4154. eedata.Cbus2 = ee232r.Cbus2;
  4155. eedata.Cbus3 = ee232r.Cbus3;
  4156. eedata.Cbus4 = ee232r.Cbus4;
  4157. eedata.RIsD2XX = Convert.ToByte(ee232r.RIsD2XX);
  4158. // Call FT_EE_Program
  4159. ftStatus = FT_EE_Program(ftHandle, eedata);
  4160. // Free unmanaged buffers
  4161. Marshal.FreeHGlobal(eedata.Manufacturer);
  4162. Marshal.FreeHGlobal(eedata.ManufacturerID);
  4163. Marshal.FreeHGlobal(eedata.Description);
  4164. Marshal.FreeHGlobal(eedata.SerialNumber);
  4165. }
  4166. }
  4167. else
  4168. {
  4169. if (pFT_EE_Program == IntPtr.Zero)
  4170. {
  4171. MessageBox.Show("Failed to load function FT_EE_Program.");
  4172. }
  4173. }
  4174. return ftStatus;
  4175. }
  4176. //**************************************************************************
  4177. // WriteFT2232HEEPROM
  4178. //**************************************************************************
  4179. // Intellisense comments
  4180. /// <summary>
  4181. /// Writes the specified values to the EEPROM of an FT2232H device.
  4182. /// Calls FT_EE_Program in FTD2XX DLL
  4183. /// </summary>
  4184. /// <returns>FT_STATUS value from FT_EE_Program in FTD2XX DLL</returns>
  4185. /// <param name="ee2232h">The EEPROM settings to be written to the device</param>
  4186. /// <remarks>If the strings are too long, they will be truncated to their maximum permitted lengths</remarks>
  4187. /// <exception cref="FT_EXCEPTION">Thrown when the current device does not match the type required by this method.</exception>
  4188. public FT_STATUS WriteFT2232HEEPROM(FT2232H_EEPROM_STRUCTURE ee2232h)
  4189. {
  4190. // Initialise ftStatus to something other than FT_OK
  4191. FT_STATUS ftStatus = FT_STATUS.FT_OTHER_ERROR;
  4192. FT_ERROR ftErrorCondition = FT_ERROR.FT_NO_ERROR;
  4193. // If the DLL hasn't been loaded, just return here
  4194. if (hFTD2XXDLL == IntPtr.Zero)
  4195. return ftStatus;
  4196. // Check for our required function pointers being set up
  4197. if (pFT_EE_Program != IntPtr.Zero)
  4198. {
  4199. tFT_EE_Program FT_EE_Program = (tFT_EE_Program)Marshal.GetDelegateForFunctionPointer(pFT_EE_Program, typeof(tFT_EE_Program));
  4200. if (ftHandle != IntPtr.Zero)
  4201. {
  4202. FT_DEVICE DeviceType = FT_DEVICE.FT_DEVICE_UNKNOWN;
  4203. // Check that it is an FT2232H that we are trying to write
  4204. GetDeviceType(ref DeviceType);
  4205. if (DeviceType != FT_DEVICE.FT_DEVICE_2232H)
  4206. {
  4207. // If it is not, throw an exception
  4208. ftErrorCondition = FT_ERROR.FT_INCORRECT_DEVICE;
  4209. ErrorHandler(ftStatus, ftErrorCondition);
  4210. }
  4211. // Check for VID and PID of 0x0000
  4212. if ((ee2232h.VendorID == 0x0000) | (ee2232h.ProductID == 0x0000))
  4213. {
  4214. // Do not allow users to program the device with VID or PID of 0x0000
  4215. return FT_STATUS.FT_INVALID_PARAMETER;
  4216. }
  4217. FT_PROGRAM_DATA eedata = new FT_PROGRAM_DATA();
  4218. // Set up structure headers
  4219. eedata.Signature1 = 0x00000000;
  4220. eedata.Signature2 = 0xFFFFFFFF;
  4221. eedata.Version = 3;
  4222. // Allocate space from unmanaged heap
  4223. eedata.Manufacturer = Marshal.AllocHGlobal(32);
  4224. eedata.ManufacturerID = Marshal.AllocHGlobal(16);
  4225. eedata.Description = Marshal.AllocHGlobal(64);
  4226. eedata.SerialNumber = Marshal.AllocHGlobal(16);
  4227. // Check lengths of strings to make sure that they are within our limits
  4228. // If not, trim them to make them our maximum length
  4229. if (ee2232h.Manufacturer.Length > 32)
  4230. ee2232h.Manufacturer = ee2232h.Manufacturer.Substring(0, 32);
  4231. if (ee2232h.ManufacturerID.Length > 16)
  4232. ee2232h.ManufacturerID = ee2232h.ManufacturerID.Substring(0, 16);
  4233. if (ee2232h.Description.Length > 64)
  4234. ee2232h.Description = ee2232h.Description.Substring(0, 64);
  4235. if (ee2232h.SerialNumber.Length > 16)
  4236. ee2232h.SerialNumber = ee2232h.SerialNumber.Substring(0, 16);
  4237. // Set string values
  4238. eedata.Manufacturer = Marshal.StringToHGlobalAnsi(ee2232h.Manufacturer);
  4239. eedata.ManufacturerID = Marshal.StringToHGlobalAnsi(ee2232h.ManufacturerID);
  4240. eedata.Description = Marshal.StringToHGlobalAnsi(ee2232h.Description);
  4241. eedata.SerialNumber = Marshal.StringToHGlobalAnsi(ee2232h.SerialNumber);
  4242. // Map non-string elements to structure
  4243. // Standard elements
  4244. eedata.VendorID = ee2232h.VendorID;
  4245. eedata.ProductID = ee2232h.ProductID;
  4246. eedata.MaxPower = ee2232h.MaxPower;
  4247. eedata.SelfPowered = Convert.ToUInt16(ee2232h.SelfPowered);
  4248. eedata.RemoteWakeup = Convert.ToUInt16(ee2232h.RemoteWakeup);
  4249. // 2232H specific fields
  4250. eedata.PullDownEnable7 = Convert.ToByte(ee2232h.PullDownEnable);
  4251. eedata.SerNumEnable7 = Convert.ToByte(ee2232h.SerNumEnable);
  4252. eedata.ALSlowSlew = Convert.ToByte(ee2232h.ALSlowSlew);
  4253. eedata.ALSchmittInput = Convert.ToByte(ee2232h.ALSchmittInput);
  4254. eedata.ALDriveCurrent = ee2232h.ALDriveCurrent;
  4255. eedata.AHSlowSlew = Convert.ToByte(ee2232h.AHSlowSlew);
  4256. eedata.AHSchmittInput = Convert.ToByte(ee2232h.AHSchmittInput);
  4257. eedata.AHDriveCurrent = ee2232h.AHDriveCurrent;
  4258. eedata.BLSlowSlew = Convert.ToByte(ee2232h.BLSlowSlew);
  4259. eedata.BLSchmittInput = Convert.ToByte(ee2232h.BLSchmittInput);
  4260. eedata.BLDriveCurrent = ee2232h.BLDriveCurrent;
  4261. eedata.BHSlowSlew = Convert.ToByte(ee2232h.BHSlowSlew);
  4262. eedata.BHSchmittInput = Convert.ToByte(ee2232h.BHSchmittInput);
  4263. eedata.BHDriveCurrent = ee2232h.BHDriveCurrent;
  4264. eedata.IFAIsFifo7 = Convert.ToByte(ee2232h.IFAIsFifo);
  4265. eedata.IFAIsFifoTar7 = Convert.ToByte(ee2232h.IFAIsFifoTar);
  4266. eedata.IFAIsFastSer7 = Convert.ToByte(ee2232h.IFAIsFastSer);
  4267. eedata.AIsVCP7 = Convert.ToByte(ee2232h.AIsVCP);
  4268. eedata.IFBIsFifo7 = Convert.ToByte(ee2232h.IFBIsFifo);
  4269. eedata.IFBIsFifoTar7 = Convert.ToByte(ee2232h.IFBIsFifoTar);
  4270. eedata.IFBIsFastSer7 = Convert.ToByte(ee2232h.IFBIsFastSer);
  4271. eedata.BIsVCP7 = Convert.ToByte(ee2232h.BIsVCP);
  4272. eedata.PowerSaveEnable = Convert.ToByte(ee2232h.PowerSaveEnable);
  4273. // Call FT_EE_Program
  4274. ftStatus = FT_EE_Program(ftHandle, eedata);
  4275. // Free unmanaged buffers
  4276. Marshal.FreeHGlobal(eedata.Manufacturer);
  4277. Marshal.FreeHGlobal(eedata.ManufacturerID);
  4278. Marshal.FreeHGlobal(eedata.Description);
  4279. Marshal.FreeHGlobal(eedata.SerialNumber);
  4280. }
  4281. }
  4282. else
  4283. {
  4284. if (pFT_EE_Program == IntPtr.Zero)
  4285. {
  4286. MessageBox.Show("Failed to load function FT_EE_Program.");
  4287. }
  4288. }
  4289. return ftStatus;
  4290. }
  4291. //**************************************************************************
  4292. // WriteFT4232HEEPROM
  4293. //**************************************************************************
  4294. // Intellisense comments
  4295. /// <summary>
  4296. /// Writes the specified values to the EEPROM of an FT4232H device.
  4297. /// Calls FT_EE_Program in FTD2XX DLL
  4298. /// </summary>
  4299. /// <returns>FT_STATUS value from FT_EE_Program in FTD2XX DLL</returns>
  4300. /// <param name="ee4232h">The EEPROM settings to be written to the device</param>
  4301. /// <remarks>If the strings are too long, they will be truncated to their maximum permitted lengths</remarks>
  4302. /// <exception cref="FT_EXCEPTION">Thrown when the current device does not match the type required by this method.</exception>
  4303. public FT_STATUS WriteFT4232HEEPROM(FT4232H_EEPROM_STRUCTURE ee4232h)
  4304. {
  4305. // Initialise ftStatus to something other than FT_OK
  4306. FT_STATUS ftStatus = FT_STATUS.FT_OTHER_ERROR;
  4307. FT_ERROR ftErrorCondition = FT_ERROR.FT_NO_ERROR;
  4308. // If the DLL hasn't been loaded, just return here
  4309. if (hFTD2XXDLL == IntPtr.Zero)
  4310. return ftStatus;
  4311. // Check for our required function pointers being set up
  4312. if (pFT_EE_Program != IntPtr.Zero)
  4313. {
  4314. tFT_EE_Program FT_EE_Program = (tFT_EE_Program)Marshal.GetDelegateForFunctionPointer(pFT_EE_Program, typeof(tFT_EE_Program));
  4315. if (ftHandle != IntPtr.Zero)
  4316. {
  4317. FT_DEVICE DeviceType = FT_DEVICE.FT_DEVICE_UNKNOWN;
  4318. // Check that it is an FT4232H that we are trying to write
  4319. GetDeviceType(ref DeviceType);
  4320. if (DeviceType != FT_DEVICE.FT_DEVICE_4232H)
  4321. {
  4322. // If it is not, throw an exception
  4323. ftErrorCondition = FT_ERROR.FT_INCORRECT_DEVICE;
  4324. ErrorHandler(ftStatus, ftErrorCondition);
  4325. }
  4326. // Check for VID and PID of 0x0000
  4327. if ((ee4232h.VendorID == 0x0000) | (ee4232h.ProductID == 0x0000))
  4328. {
  4329. // Do not allow users to program the device with VID or PID of 0x0000
  4330. return FT_STATUS.FT_INVALID_PARAMETER;
  4331. }
  4332. FT_PROGRAM_DATA eedata = new FT_PROGRAM_DATA();
  4333. // Set up structure headers
  4334. eedata.Signature1 = 0x00000000;
  4335. eedata.Signature2 = 0xFFFFFFFF;
  4336. eedata.Version = 4;
  4337. // Allocate space from unmanaged heap
  4338. eedata.Manufacturer = Marshal.AllocHGlobal(32);
  4339. eedata.ManufacturerID = Marshal.AllocHGlobal(16);
  4340. eedata.Description = Marshal.AllocHGlobal(64);
  4341. eedata.SerialNumber = Marshal.AllocHGlobal(16);
  4342. // Check lengths of strings to make sure that they are within our limits
  4343. // If not, trim them to make them our maximum length
  4344. if (ee4232h.Manufacturer.Length > 32)
  4345. ee4232h.Manufacturer = ee4232h.Manufacturer.Substring(0, 32);
  4346. if (ee4232h.ManufacturerID.Length > 16)
  4347. ee4232h.ManufacturerID = ee4232h.ManufacturerID.Substring(0, 16);
  4348. if (ee4232h.Description.Length > 64)
  4349. ee4232h.Description = ee4232h.Description.Substring(0, 64);
  4350. if (ee4232h.SerialNumber.Length > 16)
  4351. ee4232h.SerialNumber = ee4232h.SerialNumber.Substring(0, 16);
  4352. // Set string values
  4353. eedata.Manufacturer = Marshal.StringToHGlobalAnsi(ee4232h.Manufacturer);
  4354. eedata.ManufacturerID = Marshal.StringToHGlobalAnsi(ee4232h.ManufacturerID);
  4355. eedata.Description = Marshal.StringToHGlobalAnsi(ee4232h.Description);
  4356. eedata.SerialNumber = Marshal.StringToHGlobalAnsi(ee4232h.SerialNumber);
  4357. // Map non-string elements to structure
  4358. // Standard elements
  4359. eedata.VendorID = ee4232h.VendorID;
  4360. eedata.ProductID = ee4232h.ProductID;
  4361. eedata.MaxPower = ee4232h.MaxPower;
  4362. eedata.SelfPowered = Convert.ToUInt16(ee4232h.SelfPowered);
  4363. eedata.RemoteWakeup = Convert.ToUInt16(ee4232h.RemoteWakeup);
  4364. // 4232H specific fields
  4365. eedata.PullDownEnable8 = Convert.ToByte(ee4232h.PullDownEnable);
  4366. eedata.SerNumEnable8 = Convert.ToByte(ee4232h.SerNumEnable);
  4367. eedata.ASlowSlew = Convert.ToByte(ee4232h.ASlowSlew);
  4368. eedata.ASchmittInput = Convert.ToByte(ee4232h.ASchmittInput);
  4369. eedata.ADriveCurrent = ee4232h.ADriveCurrent;
  4370. eedata.BSlowSlew = Convert.ToByte(ee4232h.BSlowSlew);
  4371. eedata.BSchmittInput = Convert.ToByte(ee4232h.BSchmittInput);
  4372. eedata.BDriveCurrent = ee4232h.BDriveCurrent;
  4373. eedata.CSlowSlew = Convert.ToByte(ee4232h.CSlowSlew);
  4374. eedata.CSchmittInput = Convert.ToByte(ee4232h.CSchmittInput);
  4375. eedata.CDriveCurrent = ee4232h.CDriveCurrent;
  4376. eedata.DSlowSlew = Convert.ToByte(ee4232h.DSlowSlew);
  4377. eedata.DSchmittInput = Convert.ToByte(ee4232h.DSchmittInput);
  4378. eedata.DDriveCurrent = ee4232h.DDriveCurrent;
  4379. eedata.ARIIsTXDEN = Convert.ToByte(ee4232h.ARIIsTXDEN);
  4380. eedata.BRIIsTXDEN = Convert.ToByte(ee4232h.BRIIsTXDEN);
  4381. eedata.CRIIsTXDEN = Convert.ToByte(ee4232h.CRIIsTXDEN);
  4382. eedata.DRIIsTXDEN = Convert.ToByte(ee4232h.DRIIsTXDEN);
  4383. eedata.AIsVCP8 = Convert.ToByte(ee4232h.AIsVCP);
  4384. eedata.BIsVCP8 = Convert.ToByte(ee4232h.BIsVCP);
  4385. eedata.CIsVCP8 = Convert.ToByte(ee4232h.CIsVCP);
  4386. eedata.DIsVCP8 = Convert.ToByte(ee4232h.DIsVCP);
  4387. // Call FT_EE_Program
  4388. ftStatus = FT_EE_Program(ftHandle, eedata);
  4389. // Free unmanaged buffers
  4390. Marshal.FreeHGlobal(eedata.Manufacturer);
  4391. Marshal.FreeHGlobal(eedata.ManufacturerID);
  4392. Marshal.FreeHGlobal(eedata.Description);
  4393. Marshal.FreeHGlobal(eedata.SerialNumber);
  4394. }
  4395. }
  4396. else
  4397. {
  4398. if (pFT_EE_Program == IntPtr.Zero)
  4399. {
  4400. MessageBox.Show("Failed to load function FT_EE_Program.");
  4401. }
  4402. }
  4403. return ftStatus;
  4404. }
  4405. //**************************************************************************
  4406. // WriteFT232HEEPROM
  4407. //**************************************************************************
  4408. // Intellisense comments
  4409. /// <summary>
  4410. /// Writes the specified values to the EEPROM of an FT232H device.
  4411. /// Calls FT_EE_Program in FTD2XX DLL
  4412. /// </summary>
  4413. /// <returns>FT_STATUS value from FT_EE_Program in FTD2XX DLL</returns>
  4414. /// <param name="ee232h">The EEPROM settings to be written to the device</param>
  4415. /// <remarks>If the strings are too long, they will be truncated to their maximum permitted lengths</remarks>
  4416. /// <exception cref="FT_EXCEPTION">Thrown when the current device does not match the type required by this method.</exception>
  4417. public FT_STATUS WriteFT232HEEPROM(FT232H_EEPROM_STRUCTURE ee232h)
  4418. {
  4419. // Initialise ftStatus to something other than FT_OK
  4420. FT_STATUS ftStatus = FT_STATUS.FT_OTHER_ERROR;
  4421. FT_ERROR ftErrorCondition = FT_ERROR.FT_NO_ERROR;
  4422. // If the DLL hasn't been loaded, just return here
  4423. if (hFTD2XXDLL == IntPtr.Zero)
  4424. return ftStatus;
  4425. // Check for our required function pointers being set up
  4426. if (pFT_EE_Program != IntPtr.Zero)
  4427. {
  4428. tFT_EE_Program FT_EE_Program = (tFT_EE_Program)Marshal.GetDelegateForFunctionPointer(pFT_EE_Program, typeof(tFT_EE_Program));
  4429. if (ftHandle != IntPtr.Zero)
  4430. {
  4431. FT_DEVICE DeviceType = FT_DEVICE.FT_DEVICE_UNKNOWN;
  4432. // Check that it is an FT232H that we are trying to write
  4433. GetDeviceType(ref DeviceType);
  4434. if (DeviceType != FT_DEVICE.FT_DEVICE_232H)
  4435. {
  4436. // If it is not, throw an exception
  4437. ftErrorCondition = FT_ERROR.FT_INCORRECT_DEVICE;
  4438. ErrorHandler(ftStatus, ftErrorCondition);
  4439. }
  4440. // Check for VID and PID of 0x0000
  4441. if ((ee232h.VendorID == 0x0000) | (ee232h.ProductID == 0x0000))
  4442. {
  4443. // Do not allow users to program the device with VID or PID of 0x0000
  4444. return FT_STATUS.FT_INVALID_PARAMETER;
  4445. }
  4446. FT_PROGRAM_DATA eedata = new FT_PROGRAM_DATA();
  4447. // Set up structure headers
  4448. eedata.Signature1 = 0x00000000;
  4449. eedata.Signature2 = 0xFFFFFFFF;
  4450. eedata.Version = 5;
  4451. // Allocate space from unmanaged heap
  4452. eedata.Manufacturer = Marshal.AllocHGlobal(32);
  4453. eedata.ManufacturerID = Marshal.AllocHGlobal(16);
  4454. eedata.Description = Marshal.AllocHGlobal(64);
  4455. eedata.SerialNumber = Marshal.AllocHGlobal(16);
  4456. // Check lengths of strings to make sure that they are within our limits
  4457. // If not, trim them to make them our maximum length
  4458. if (ee232h.Manufacturer.Length > 32)
  4459. ee232h.Manufacturer = ee232h.Manufacturer.Substring(0, 32);
  4460. if (ee232h.ManufacturerID.Length > 16)
  4461. ee232h.ManufacturerID = ee232h.ManufacturerID.Substring(0, 16);
  4462. if (ee232h.Description.Length > 64)
  4463. ee232h.Description = ee232h.Description.Substring(0, 64);
  4464. if (ee232h.SerialNumber.Length > 16)
  4465. ee232h.SerialNumber = ee232h.SerialNumber.Substring(0, 16);
  4466. // Set string values
  4467. eedata.Manufacturer = Marshal.StringToHGlobalAnsi(ee232h.Manufacturer);
  4468. eedata.ManufacturerID = Marshal.StringToHGlobalAnsi(ee232h.ManufacturerID);
  4469. eedata.Description = Marshal.StringToHGlobalAnsi(ee232h.Description);
  4470. eedata.SerialNumber = Marshal.StringToHGlobalAnsi(ee232h.SerialNumber);
  4471. // Map non-string elements to structure
  4472. // Standard elements
  4473. eedata.VendorID = ee232h.VendorID;
  4474. eedata.ProductID = ee232h.ProductID;
  4475. eedata.MaxPower = ee232h.MaxPower;
  4476. eedata.SelfPowered = Convert.ToUInt16(ee232h.SelfPowered);
  4477. eedata.RemoteWakeup = Convert.ToUInt16(ee232h.RemoteWakeup);
  4478. // 232H specific fields
  4479. eedata.PullDownEnableH = Convert.ToByte(ee232h.PullDownEnable);
  4480. eedata.SerNumEnableH = Convert.ToByte(ee232h.SerNumEnable);
  4481. eedata.ACSlowSlewH = Convert.ToByte(ee232h.ACSlowSlew);
  4482. eedata.ACSchmittInputH = Convert.ToByte(ee232h.ACSchmittInput);
  4483. eedata.ACDriveCurrentH = Convert.ToByte(ee232h.ACDriveCurrent);
  4484. eedata.ADSlowSlewH = Convert.ToByte(ee232h.ADSlowSlew);
  4485. eedata.ADSchmittInputH = Convert.ToByte(ee232h.ADSchmittInput);
  4486. eedata.ADDriveCurrentH = Convert.ToByte(ee232h.ADDriveCurrent);
  4487. eedata.Cbus0H = Convert.ToByte(ee232h.Cbus0);
  4488. eedata.Cbus1H = Convert.ToByte(ee232h.Cbus1);
  4489. eedata.Cbus2H = Convert.ToByte(ee232h.Cbus2);
  4490. eedata.Cbus3H = Convert.ToByte(ee232h.Cbus3);
  4491. eedata.Cbus4H = Convert.ToByte(ee232h.Cbus4);
  4492. eedata.Cbus5H = Convert.ToByte(ee232h.Cbus5);
  4493. eedata.Cbus6H = Convert.ToByte(ee232h.Cbus6);
  4494. eedata.Cbus7H = Convert.ToByte(ee232h.Cbus7);
  4495. eedata.Cbus8H = Convert.ToByte(ee232h.Cbus8);
  4496. eedata.Cbus9H = Convert.ToByte(ee232h.Cbus9);
  4497. eedata.IsFifoH = Convert.ToByte(ee232h.IsFifo);
  4498. eedata.IsFifoTarH = Convert.ToByte(ee232h.IsFifoTar);
  4499. eedata.IsFastSerH = Convert.ToByte(ee232h.IsFastSer);
  4500. eedata.IsFT1248H = Convert.ToByte(ee232h.IsFT1248);
  4501. eedata.FT1248CpolH = Convert.ToByte(ee232h.FT1248Cpol);
  4502. eedata.FT1248LsbH = Convert.ToByte(ee232h.FT1248Lsb);
  4503. eedata.FT1248FlowControlH = Convert.ToByte(ee232h.FT1248FlowControl);
  4504. eedata.IsVCPH = Convert.ToByte(ee232h.IsVCP);
  4505. eedata.PowerSaveEnableH = Convert.ToByte(ee232h.PowerSaveEnable);
  4506. // Call FT_EE_Program
  4507. ftStatus = FT_EE_Program(ftHandle, eedata);
  4508. // Free unmanaged buffers
  4509. Marshal.FreeHGlobal(eedata.Manufacturer);
  4510. Marshal.FreeHGlobal(eedata.ManufacturerID);
  4511. Marshal.FreeHGlobal(eedata.Description);
  4512. Marshal.FreeHGlobal(eedata.SerialNumber);
  4513. }
  4514. }
  4515. else
  4516. {
  4517. if (pFT_EE_Program == IntPtr.Zero)
  4518. {
  4519. MessageBox.Show("Failed to load function FT_EE_Program.");
  4520. }
  4521. }
  4522. return ftStatus;
  4523. }
  4524. //**************************************************************************
  4525. // WriteXSeriesEEPROM
  4526. //**************************************************************************
  4527. // Intellisense comments
  4528. /// <summary>
  4529. /// Writes the specified values to the EEPROM of an X-Series device.
  4530. /// Calls FT_EEPROM_Program in FTD2XX DLL
  4531. /// </summary>
  4532. /// <returns>FT_STATUS value from FT_EEPROM_Program in FTD2XX DLL</returns>
  4533. /// <param name="eeX">The EEPROM settings to be written to the device</param>
  4534. /// <remarks>If the strings are too long, they will be truncated to their maximum permitted lengths</remarks>
  4535. /// <exception cref="FT_EXCEPTION">Thrown when the current device does not match the type required by this method.</exception>
  4536. public FT_STATUS WriteXSeriesEEPROM(FT_XSERIES_EEPROM_STRUCTURE eeX)
  4537. {
  4538. // Initialise ftStatus to something other than FT_OK
  4539. FT_STATUS ftStatus = FT_STATUS.FT_OTHER_ERROR;
  4540. FT_ERROR ftErrorCondition = FT_ERROR.FT_NO_ERROR;
  4541. byte[] manufacturer, manufacturerID, description, serialNumber;
  4542. // If the DLL hasn't been loaded, just return here
  4543. if (hFTD2XXDLL == IntPtr.Zero)
  4544. return ftStatus;
  4545. // Check for our required function pointers being set up
  4546. if (pFT_EEPROM_Program != IntPtr.Zero)
  4547. {
  4548. tFT_EEPROM_Program FT_EEPROM_Program = (tFT_EEPROM_Program)Marshal.GetDelegateForFunctionPointer(pFT_EEPROM_Program, typeof(tFT_EEPROM_Program));
  4549. if (ftHandle != IntPtr.Zero)
  4550. {
  4551. FT_DEVICE DeviceType = FT_DEVICE.FT_DEVICE_UNKNOWN;
  4552. // Check that it is an FT232H that we are trying to write
  4553. GetDeviceType(ref DeviceType);
  4554. if (DeviceType != FT_DEVICE.FT_DEVICE_X_SERIES)
  4555. {
  4556. // If it is not, throw an exception
  4557. ftErrorCondition = FT_ERROR.FT_INCORRECT_DEVICE;
  4558. ErrorHandler(ftStatus, ftErrorCondition);
  4559. }
  4560. // Check for VID and PID of 0x0000
  4561. if ((eeX.VendorID == 0x0000) | (eeX.ProductID == 0x0000))
  4562. {
  4563. // Do not allow users to program the device with VID or PID of 0x0000
  4564. return FT_STATUS.FT_INVALID_PARAMETER;
  4565. }
  4566. FT_XSERIES_DATA eeData = new FT_XSERIES_DATA();
  4567. // String manipulation...
  4568. // Allocate space from unmanaged heap
  4569. manufacturer = new byte[32];
  4570. manufacturerID = new byte[16];
  4571. description = new byte[64];
  4572. serialNumber = new byte[16];
  4573. // Check lengths of strings to make sure that they are within our limits
  4574. // If not, trim them to make them our maximum length
  4575. if (eeX.Manufacturer.Length > 32)
  4576. eeX.Manufacturer = eeX.Manufacturer.Substring(0, 32);
  4577. if (eeX.ManufacturerID.Length > 16)
  4578. eeX.ManufacturerID = eeX.ManufacturerID.Substring(0, 16);
  4579. if (eeX.Description.Length > 64)
  4580. eeX.Description = eeX.Description.Substring(0, 64);
  4581. if (eeX.SerialNumber.Length > 16)
  4582. eeX.SerialNumber = eeX.SerialNumber.Substring(0, 16);
  4583. // Set string values
  4584. System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
  4585. manufacturer = encoding.GetBytes(eeX.Manufacturer);
  4586. manufacturerID = encoding.GetBytes(eeX.ManufacturerID);
  4587. description = encoding.GetBytes(eeX.Description);
  4588. serialNumber = encoding.GetBytes(eeX.SerialNumber);
  4589. // Map non-string elements to structure to be returned
  4590. // Standard elements
  4591. eeData.common.deviceType = (uint)FT_DEVICE.FT_DEVICE_X_SERIES;
  4592. eeData.common.VendorId = eeX.VendorID;
  4593. eeData.common.ProductId = eeX.ProductID;
  4594. eeData.common.MaxPower = eeX.MaxPower;
  4595. eeData.common.SelfPowered = Convert.ToByte(eeX.SelfPowered);
  4596. eeData.common.RemoteWakeup = Convert.ToByte(eeX.RemoteWakeup);
  4597. eeData.common.SerNumEnable = Convert.ToByte(eeX.SerNumEnable);
  4598. eeData.common.PullDownEnable = Convert.ToByte(eeX.PullDownEnable);
  4599. // X-Series specific fields
  4600. // CBUS
  4601. eeData.Cbus0 = eeX.Cbus0;
  4602. eeData.Cbus1 = eeX.Cbus1;
  4603. eeData.Cbus2 = eeX.Cbus2;
  4604. eeData.Cbus3 = eeX.Cbus3;
  4605. eeData.Cbus4 = eeX.Cbus4;
  4606. eeData.Cbus5 = eeX.Cbus5;
  4607. eeData.Cbus6 = eeX.Cbus6;
  4608. // Drive Options
  4609. eeData.ACDriveCurrent = eeX.ACDriveCurrent;
  4610. eeData.ACSchmittInput = eeX.ACSchmittInput;
  4611. eeData.ACSlowSlew = eeX.ACSlowSlew;
  4612. eeData.ADDriveCurrent = eeX.ADDriveCurrent;
  4613. eeData.ADSchmittInput = eeX.ADSchmittInput;
  4614. eeData.ADSlowSlew = eeX.ADSlowSlew;
  4615. // BCD
  4616. eeData.BCDDisableSleep = eeX.BCDDisableSleep;
  4617. eeData.BCDEnable = eeX.BCDEnable;
  4618. eeData.BCDForceCbusPWREN = eeX.BCDForceCbusPWREN;
  4619. // FT1248
  4620. eeData.FT1248Cpol = eeX.FT1248Cpol;
  4621. eeData.FT1248FlowControl = eeX.FT1248FlowControl;
  4622. eeData.FT1248Lsb = eeX.FT1248Lsb;
  4623. // I2C
  4624. eeData.I2CDeviceId = eeX.I2CDeviceId;
  4625. eeData.I2CDisableSchmitt = eeX.I2CDisableSchmitt;
  4626. eeData.I2CSlaveAddress = eeX.I2CSlaveAddress;
  4627. // RS232 Signals
  4628. eeData.InvertCTS = eeX.InvertCTS;
  4629. eeData.InvertDCD = eeX.InvertDCD;
  4630. eeData.InvertDSR = eeX.InvertDSR;
  4631. eeData.InvertDTR = eeX.InvertDTR;
  4632. eeData.InvertRI = eeX.InvertRI;
  4633. eeData.InvertRTS = eeX.InvertRTS;
  4634. eeData.InvertRXD = eeX.InvertRXD;
  4635. eeData.InvertTXD = eeX.InvertTXD;
  4636. // Hardware Options
  4637. eeData.PowerSaveEnable = eeX.PowerSaveEnable;
  4638. eeData.RS485EchoSuppress = eeX.RS485EchoSuppress;
  4639. // Driver Option
  4640. eeData.DriverType = eeX.IsVCP;
  4641. // Check the size of the structure...
  4642. int size = Marshal.SizeOf(eeData);
  4643. // Allocate space for our pointer...
  4644. IntPtr eeDataMarshal = Marshal.AllocHGlobal(size);
  4645. Marshal.StructureToPtr(eeData, eeDataMarshal, false);
  4646. ftStatus = FT_EEPROM_Program(ftHandle, eeDataMarshal, (uint)size, manufacturer, manufacturerID, description, serialNumber);
  4647. }
  4648. }
  4649. return FT_STATUS.FT_DEVICE_NOT_FOUND;
  4650. }
  4651. //**************************************************************************
  4652. // EEReadUserArea
  4653. //**************************************************************************
  4654. // Intellisense comments
  4655. /// <summary>
  4656. /// Reads data from the user area of the device EEPROM.
  4657. /// </summary>
  4658. /// <returns>FT_STATUS from FT_UARead in FTD2XX.DLL</returns>
  4659. /// <param name="UserAreaDataBuffer">An array of bytes which will be populated with the data read from the device EEPROM user area.</param>
  4660. /// <param name="numBytesRead">The number of bytes actually read from the EEPROM user area.</param>
  4661. public FT_STATUS EEReadUserArea(byte[] UserAreaDataBuffer, ref UInt32 numBytesRead)
  4662. {
  4663. // Initialise ftStatus to something other than FT_OK
  4664. FT_STATUS ftStatus = FT_STATUS.FT_OTHER_ERROR;
  4665. // If the DLL hasn't been loaded, just return here
  4666. if (hFTD2XXDLL == IntPtr.Zero)
  4667. return ftStatus;
  4668. // Check for our required function pointers being set up
  4669. if ((pFT_EE_UASize != IntPtr.Zero) & (pFT_EE_UARead != IntPtr.Zero))
  4670. {
  4671. tFT_EE_UASize FT_EE_UASize = (tFT_EE_UASize)Marshal.GetDelegateForFunctionPointer(pFT_EE_UASize, typeof(tFT_EE_UASize));
  4672. tFT_EE_UARead FT_EE_UARead = (tFT_EE_UARead)Marshal.GetDelegateForFunctionPointer(pFT_EE_UARead, typeof(tFT_EE_UARead));
  4673. if (ftHandle != IntPtr.Zero)
  4674. {
  4675. UInt32 UASize = 0;
  4676. // Get size of user area to allocate an array of the correct size.
  4677. // The application must also get the UA size for its copy
  4678. ftStatus = FT_EE_UASize(ftHandle, ref UASize);
  4679. // Make sure we have enough storage for the whole user area
  4680. if (UserAreaDataBuffer.Length >= UASize)
  4681. {
  4682. // Call FT_EE_UARead
  4683. ftStatus = FT_EE_UARead(ftHandle, UserAreaDataBuffer, UserAreaDataBuffer.Length, ref numBytesRead);
  4684. }
  4685. }
  4686. }
  4687. else
  4688. {
  4689. if (pFT_EE_UASize == IntPtr.Zero)
  4690. {
  4691. MessageBox.Show("Failed to load function FT_EE_UASize.");
  4692. }
  4693. if (pFT_EE_UARead == IntPtr.Zero)
  4694. {
  4695. MessageBox.Show("Failed to load function FT_EE_UARead.");
  4696. }
  4697. }
  4698. return ftStatus;
  4699. }
  4700. //**************************************************************************
  4701. // EEWriteUserArea
  4702. //**************************************************************************
  4703. // Intellisense comments
  4704. /// <summary>
  4705. /// Writes data to the user area of the device EEPROM.
  4706. /// </summary>
  4707. /// <returns>FT_STATUS value from FT_UAWrite in FTD2XX.DLL</returns>
  4708. /// <param name="UserAreaDataBuffer">An array of bytes which will be written to the device EEPROM user area.</param>
  4709. public FT_STATUS EEWriteUserArea(byte[] UserAreaDataBuffer)
  4710. {
  4711. // Initialise ftStatus to something other than FT_OK
  4712. FT_STATUS ftStatus = FT_STATUS.FT_OTHER_ERROR;
  4713. // If the DLL hasn't been loaded, just return here
  4714. if (hFTD2XXDLL == IntPtr.Zero)
  4715. return ftStatus;
  4716. // Check for our required function pointers being set up
  4717. if ((pFT_EE_UASize != IntPtr.Zero) & (pFT_EE_UAWrite != IntPtr.Zero))
  4718. {
  4719. tFT_EE_UASize FT_EE_UASize = (tFT_EE_UASize)Marshal.GetDelegateForFunctionPointer(pFT_EE_UASize, typeof(tFT_EE_UASize));
  4720. tFT_EE_UAWrite FT_EE_UAWrite = (tFT_EE_UAWrite)Marshal.GetDelegateForFunctionPointer(pFT_EE_UAWrite, typeof(tFT_EE_UAWrite));
  4721. if (ftHandle != IntPtr.Zero)
  4722. {
  4723. UInt32 UASize = 0;
  4724. // Get size of user area to allocate an array of the correct size.
  4725. // The application must also get the UA size for its copy
  4726. ftStatus = FT_EE_UASize(ftHandle, ref UASize);
  4727. // Make sure we have enough storage for all the data in the EEPROM
  4728. if (UserAreaDataBuffer.Length <= UASize)
  4729. {
  4730. // Call FT_EE_UAWrite
  4731. ftStatus = FT_EE_UAWrite(ftHandle, UserAreaDataBuffer, UserAreaDataBuffer.Length);
  4732. }
  4733. }
  4734. }
  4735. else
  4736. {
  4737. if (pFT_EE_UASize == IntPtr.Zero)
  4738. {
  4739. MessageBox.Show("Failed to load function FT_EE_UASize.");
  4740. }
  4741. if (pFT_EE_UAWrite == IntPtr.Zero)
  4742. {
  4743. MessageBox.Show("Failed to load function FT_EE_UAWrite.");
  4744. }
  4745. }
  4746. return ftStatus;
  4747. }
  4748. //**************************************************************************
  4749. // GetDeviceType
  4750. //**************************************************************************
  4751. // Intellisense comments
  4752. /// <summary>
  4753. /// Gets the chip type of the current device.
  4754. /// </summary>
  4755. /// <returns>FT_STATUS value from FT_GetDeviceInfo in FTD2XX.DLL</returns>
  4756. /// <param name="DeviceType">The FTDI chip type of the current device.</param>
  4757. public FT_STATUS GetDeviceType(ref FT_DEVICE DeviceType)
  4758. {
  4759. // Initialise ftStatus to something other than FT_OK
  4760. FT_STATUS ftStatus = FT_STATUS.FT_OTHER_ERROR;
  4761. // If the DLL hasn't been loaded, just return here
  4762. if (hFTD2XXDLL == IntPtr.Zero)
  4763. return ftStatus;
  4764. // Check for our required function pointers being set up
  4765. if (pFT_GetDeviceInfo != IntPtr.Zero)
  4766. {
  4767. tFT_GetDeviceInfo FT_GetDeviceInfo = (tFT_GetDeviceInfo)Marshal.GetDelegateForFunctionPointer(pFT_GetDeviceInfo, typeof(tFT_GetDeviceInfo));
  4768. UInt32 DeviceID = 0;
  4769. byte[] sernum = new byte[16];
  4770. byte[] desc = new byte[64];
  4771. DeviceType = FT_DEVICE.FT_DEVICE_UNKNOWN;
  4772. if (ftHandle != IntPtr.Zero)
  4773. {
  4774. // Call FT_GetDeviceInfo
  4775. ftStatus = FT_GetDeviceInfo(ftHandle, ref DeviceType, ref DeviceID, sernum, desc, IntPtr.Zero);
  4776. }
  4777. }
  4778. else
  4779. {
  4780. if (pFT_GetDeviceInfo == IntPtr.Zero)
  4781. {
  4782. MessageBox.Show("Failed to load function FT_GetDeviceInfo.");
  4783. }
  4784. }
  4785. return ftStatus;
  4786. }
  4787. //**************************************************************************
  4788. // GetDeviceID
  4789. //**************************************************************************
  4790. // Intellisense comments
  4791. /// <summary>
  4792. /// Gets the Vendor ID and Product ID of the current device.
  4793. /// </summary>
  4794. /// <returns>FT_STATUS value from FT_GetDeviceInfo in FTD2XX.DLL</returns>
  4795. /// <param name="DeviceID">The device ID (Vendor ID and Product ID) of the current device.</param>
  4796. public FT_STATUS GetDeviceID(ref UInt32 DeviceID)
  4797. {
  4798. // Initialise ftStatus to something other than FT_OK
  4799. FT_STATUS ftStatus = FT_STATUS.FT_OTHER_ERROR;
  4800. // If the DLL hasn't been loaded, just return here
  4801. if (hFTD2XXDLL == IntPtr.Zero)
  4802. return ftStatus;
  4803. // Check for our required function pointers being set up
  4804. if (pFT_GetDeviceInfo != IntPtr.Zero)
  4805. {
  4806. tFT_GetDeviceInfo FT_GetDeviceInfo = (tFT_GetDeviceInfo)Marshal.GetDelegateForFunctionPointer(pFT_GetDeviceInfo, typeof(tFT_GetDeviceInfo));
  4807. FT_DEVICE DeviceType = FT_DEVICE.FT_DEVICE_UNKNOWN;
  4808. byte[] sernum = new byte[16];
  4809. byte[] desc = new byte[64];
  4810. if (ftHandle != IntPtr.Zero)
  4811. {
  4812. // Call FT_GetDeviceInfo
  4813. ftStatus = FT_GetDeviceInfo(ftHandle, ref DeviceType, ref DeviceID, sernum, desc, IntPtr.Zero);
  4814. }
  4815. }
  4816. else
  4817. {
  4818. if (pFT_GetDeviceInfo == IntPtr.Zero)
  4819. {
  4820. MessageBox.Show("Failed to load function FT_GetDeviceInfo.");
  4821. }
  4822. }
  4823. return ftStatus;
  4824. }
  4825. //**************************************************************************
  4826. // GetDescription
  4827. //**************************************************************************
  4828. // Intellisense comments
  4829. /// <summary>
  4830. /// Gets the description of the current device.
  4831. /// </summary>
  4832. /// <returns>FT_STATUS value from FT_GetDeviceInfo in FTD2XX.DLL</returns>
  4833. /// <param name="Description">The description of the current device.</param>
  4834. public FT_STATUS GetDescription(out string Description)
  4835. {
  4836. // Initialise ftStatus to something other than FT_OK
  4837. FT_STATUS ftStatus = FT_STATUS.FT_OTHER_ERROR;
  4838. Description = String.Empty;
  4839. // If the DLL hasn't been loaded, just return here
  4840. if (hFTD2XXDLL == IntPtr.Zero)
  4841. return ftStatus;
  4842. // Check for our required function pointers being set up
  4843. if (pFT_GetDeviceInfo != IntPtr.Zero)
  4844. {
  4845. tFT_GetDeviceInfo FT_GetDeviceInfo = (tFT_GetDeviceInfo)Marshal.GetDelegateForFunctionPointer(pFT_GetDeviceInfo, typeof(tFT_GetDeviceInfo));
  4846. UInt32 DeviceID = 0;
  4847. FT_DEVICE DeviceType = FT_DEVICE.FT_DEVICE_UNKNOWN;
  4848. byte[] sernum = new byte[16];
  4849. byte[] desc = new byte[64];
  4850. if (ftHandle != IntPtr.Zero)
  4851. {
  4852. // Call FT_GetDeviceInfo
  4853. ftStatus = FT_GetDeviceInfo(ftHandle, ref DeviceType, ref DeviceID, sernum, desc, IntPtr.Zero);
  4854. Description = Encoding.ASCII.GetString(desc);
  4855. Description = Description.Substring(0, Description.IndexOf("\0"));
  4856. }
  4857. }
  4858. else
  4859. {
  4860. if (pFT_GetDeviceInfo == IntPtr.Zero)
  4861. {
  4862. MessageBox.Show("Failed to load function FT_GetDeviceInfo.");
  4863. }
  4864. }
  4865. return ftStatus;
  4866. }
  4867. //**************************************************************************
  4868. // GetSerialNumber
  4869. //**************************************************************************
  4870. // Intellisense comments
  4871. /// <summary>
  4872. /// Gets the serial number of the current device.
  4873. /// </summary>
  4874. /// <returns>FT_STATUS value from FT_GetDeviceInfo in FTD2XX.DLL</returns>
  4875. /// <param name="SerialNumber">The serial number of the current device.</param>
  4876. public FT_STATUS GetSerialNumber(out string SerialNumber)
  4877. {
  4878. // Initialise ftStatus to something other than FT_OK
  4879. FT_STATUS ftStatus = FT_STATUS.FT_OTHER_ERROR;
  4880. SerialNumber = String.Empty;
  4881. // If the DLL hasn't been loaded, just return here
  4882. if (hFTD2XXDLL == IntPtr.Zero)
  4883. return ftStatus;
  4884. // Check for our required function pointers being set up
  4885. if (pFT_GetDeviceInfo != IntPtr.Zero)
  4886. {
  4887. tFT_GetDeviceInfo FT_GetDeviceInfo = (tFT_GetDeviceInfo)Marshal.GetDelegateForFunctionPointer(pFT_GetDeviceInfo, typeof(tFT_GetDeviceInfo));
  4888. UInt32 DeviceID = 0;
  4889. FT_DEVICE DeviceType = FT_DEVICE.FT_DEVICE_UNKNOWN;
  4890. byte[] sernum = new byte[16];
  4891. byte[] desc = new byte[64];
  4892. if (ftHandle != IntPtr.Zero)
  4893. {
  4894. // Call FT_GetDeviceInfo
  4895. ftStatus = FT_GetDeviceInfo(ftHandle, ref DeviceType, ref DeviceID, sernum, desc, IntPtr.Zero);
  4896. SerialNumber = Encoding.ASCII.GetString(sernum);
  4897. SerialNumber = SerialNumber.Substring(0, SerialNumber.IndexOf("\0"));
  4898. }
  4899. }
  4900. else
  4901. {
  4902. if (pFT_GetDeviceInfo == IntPtr.Zero)
  4903. {
  4904. MessageBox.Show("Failed to load function FT_GetDeviceInfo.");
  4905. }
  4906. }
  4907. return ftStatus;
  4908. }
  4909. //**************************************************************************
  4910. // GetRxBytesAvailable
  4911. //**************************************************************************
  4912. // Intellisense comments
  4913. /// <summary>
  4914. /// Gets the number of bytes available in the receive buffer.
  4915. /// </summary>
  4916. /// <returns>FT_STATUS value from FT_GetQueueStatus in FTD2XX.DLL</returns>
  4917. /// <param name="RxQueue">The number of bytes available to be read.</param>
  4918. public FT_STATUS GetRxBytesAvailable(ref UInt32 RxQueue)
  4919. {
  4920. // Initialise ftStatus to something other than FT_OK
  4921. FT_STATUS ftStatus = FT_STATUS.FT_OTHER_ERROR;
  4922. // If the DLL hasn't been loaded, just return here
  4923. if (hFTD2XXDLL == IntPtr.Zero)
  4924. return ftStatus;
  4925. // Check for our required function pointers being set up
  4926. if (pFT_GetQueueStatus != IntPtr.Zero)
  4927. {
  4928. tFT_GetQueueStatus FT_GetQueueStatus = (tFT_GetQueueStatus)Marshal.GetDelegateForFunctionPointer(pFT_GetQueueStatus, typeof(tFT_GetQueueStatus));
  4929. if (ftHandle != IntPtr.Zero)
  4930. {
  4931. // Call FT_GetQueueStatus
  4932. ftStatus = FT_GetQueueStatus(ftHandle, ref RxQueue);
  4933. }
  4934. }
  4935. else
  4936. {
  4937. if (pFT_GetQueueStatus == IntPtr.Zero)
  4938. {
  4939. MessageBox.Show("Failed to load function FT_GetQueueStatus.");
  4940. }
  4941. }
  4942. return ftStatus;
  4943. }
  4944. //**************************************************************************
  4945. // GetTxBytesWaiting
  4946. //**************************************************************************
  4947. // Intellisense comments
  4948. /// <summary>
  4949. /// Gets the number of bytes waiting in the transmit buffer.
  4950. /// </summary>
  4951. /// <returns>FT_STATUS value from FT_GetStatus in FTD2XX.DLL</returns>
  4952. /// <param name="TxQueue">The number of bytes waiting to be sent.</param>
  4953. public FT_STATUS GetTxBytesWaiting(ref UInt32 TxQueue)
  4954. {
  4955. // Initialise ftStatus to something other than FT_OK
  4956. FT_STATUS ftStatus = FT_STATUS.FT_OTHER_ERROR;
  4957. // If the DLL hasn't been loaded, just return here
  4958. if (hFTD2XXDLL == IntPtr.Zero)
  4959. return ftStatus;
  4960. // Check for our required function pointers being set up
  4961. if (pFT_GetStatus != IntPtr.Zero)
  4962. {
  4963. tFT_GetStatus FT_GetStatus = (tFT_GetStatus)Marshal.GetDelegateForFunctionPointer(pFT_GetStatus, typeof(tFT_GetStatus));
  4964. UInt32 RxQueue = 0;
  4965. UInt32 EventStatus = 0;
  4966. if (ftHandle != IntPtr.Zero)
  4967. {
  4968. // Call FT_GetStatus
  4969. ftStatus = FT_GetStatus(ftHandle, ref RxQueue, ref TxQueue, ref EventStatus);
  4970. }
  4971. }
  4972. else
  4973. {
  4974. if (pFT_GetStatus == IntPtr.Zero)
  4975. {
  4976. MessageBox.Show("Failed to load function FT_GetStatus.");
  4977. }
  4978. }
  4979. return ftStatus;
  4980. }
  4981. //**************************************************************************
  4982. // GetEventType
  4983. //**************************************************************************
  4984. // Intellisense comments
  4985. /// <summary>
  4986. /// Gets the event type after an event has fired. Can be used to distinguish which event has been triggered when waiting on multiple event types.
  4987. /// </summary>
  4988. /// <returns>FT_STATUS value from FT_GetStatus in FTD2XX.DLL</returns>
  4989. /// <param name="EventType">The type of event that has occurred.</param>
  4990. public FT_STATUS GetEventType(ref UInt32 EventType)
  4991. {
  4992. // Initialise ftStatus to something other than FT_OK
  4993. FT_STATUS ftStatus = FT_STATUS.FT_OTHER_ERROR;
  4994. // If the DLL hasn't been loaded, just return here
  4995. if (hFTD2XXDLL == IntPtr.Zero)
  4996. return ftStatus;
  4997. // Check for our required function pointers being set up
  4998. if (pFT_GetStatus != IntPtr.Zero)
  4999. {
  5000. tFT_GetStatus FT_GetStatus = (tFT_GetStatus)Marshal.GetDelegateForFunctionPointer(pFT_GetStatus, typeof(tFT_GetStatus));
  5001. UInt32 RxQueue = 0;
  5002. UInt32 TxQueue = 0;
  5003. if (ftHandle != IntPtr.Zero)
  5004. {
  5005. // Call FT_GetStatus
  5006. ftStatus = FT_GetStatus(ftHandle, ref RxQueue, ref TxQueue, ref EventType);
  5007. }
  5008. }
  5009. else
  5010. {
  5011. if (pFT_GetStatus == IntPtr.Zero)
  5012. {
  5013. MessageBox.Show("Failed to load function FT_GetStatus.");
  5014. }
  5015. }
  5016. return ftStatus;
  5017. }
  5018. //**************************************************************************
  5019. // GetModemStatus
  5020. //**************************************************************************
  5021. // Intellisense comments
  5022. /// <summary>
  5023. /// Gets the current modem status.
  5024. /// </summary>
  5025. /// <returns>FT_STATUS value from FT_GetModemStatus in FTD2XX.DLL</returns>
  5026. /// <param name="ModemStatus">A bit map representaion of the current modem status.</param>
  5027. public FT_STATUS GetModemStatus(ref byte ModemStatus)
  5028. {
  5029. // Initialise ftStatus to something other than FT_OK
  5030. FT_STATUS ftStatus = FT_STATUS.FT_OTHER_ERROR;
  5031. // If the DLL hasn't been loaded, just return here
  5032. if (hFTD2XXDLL == IntPtr.Zero)
  5033. return ftStatus;
  5034. // Check for our required function pointers being set up
  5035. if (pFT_GetModemStatus != IntPtr.Zero)
  5036. {
  5037. tFT_GetModemStatus FT_GetModemStatus = (tFT_GetModemStatus)Marshal.GetDelegateForFunctionPointer(pFT_GetModemStatus, typeof(tFT_GetModemStatus));
  5038. UInt32 ModemLineStatus = 0;
  5039. if (ftHandle != IntPtr.Zero)
  5040. {
  5041. // Call FT_GetModemStatus
  5042. ftStatus = FT_GetModemStatus(ftHandle, ref ModemLineStatus);
  5043. }
  5044. ModemStatus = Convert.ToByte(ModemLineStatus & 0x000000FF);
  5045. }
  5046. else
  5047. {
  5048. if (pFT_GetModemStatus == IntPtr.Zero)
  5049. {
  5050. MessageBox.Show("Failed to load function FT_GetModemStatus.");
  5051. }
  5052. }
  5053. return ftStatus;
  5054. }
  5055. //**************************************************************************
  5056. // GetLineStatus
  5057. //**************************************************************************
  5058. // Intellisense comments
  5059. /// <summary>
  5060. /// Gets the current line status.
  5061. /// </summary>
  5062. /// <returns>FT_STATUS value from FT_GetModemStatus in FTD2XX.DLL</returns>
  5063. /// <param name="LineStatus">A bit map representaion of the current line status.</param>
  5064. public FT_STATUS GetLineStatus(ref byte LineStatus)
  5065. {
  5066. // Initialise ftStatus to something other than FT_OK
  5067. FT_STATUS ftStatus = FT_STATUS.FT_OTHER_ERROR;
  5068. // If the DLL hasn't been loaded, just return here
  5069. if (hFTD2XXDLL == IntPtr.Zero)
  5070. return ftStatus;
  5071. // Check for our required function pointers being set up
  5072. if (pFT_GetModemStatus != IntPtr.Zero)
  5073. {
  5074. tFT_GetModemStatus FT_GetModemStatus = (tFT_GetModemStatus)Marshal.GetDelegateForFunctionPointer(pFT_GetModemStatus, typeof(tFT_GetModemStatus));
  5075. UInt32 ModemLineStatus = 0;
  5076. if (ftHandle != IntPtr.Zero)
  5077. {
  5078. // Call FT_GetModemStatus
  5079. ftStatus = FT_GetModemStatus(ftHandle, ref ModemLineStatus);
  5080. }
  5081. LineStatus = Convert.ToByte((ModemLineStatus >> 8) & 0x000000FF);
  5082. }
  5083. else
  5084. {
  5085. if (pFT_GetModemStatus == IntPtr.Zero)
  5086. {
  5087. MessageBox.Show("Failed to load function FT_GetModemStatus.");
  5088. }
  5089. }
  5090. return ftStatus;
  5091. }
  5092. //**************************************************************************
  5093. // SetBaudRate
  5094. //**************************************************************************
  5095. // Intellisense comments
  5096. /// <summary>
  5097. /// Sets the current Baud rate.
  5098. /// </summary>
  5099. /// <returns>FT_STATUS value from FT_SetBaudRate in FTD2XX.DLL</returns>
  5100. /// <param name="BaudRate">The desired Baud rate for the device.</param>
  5101. public FT_STATUS SetBaudRate(UInt32 BaudRate)
  5102. {
  5103. // Initialise ftStatus to something other than FT_OK
  5104. FT_STATUS ftStatus = FT_STATUS.FT_OTHER_ERROR;
  5105. // If the DLL hasn't been loaded, just return here
  5106. if (hFTD2XXDLL == IntPtr.Zero)
  5107. return ftStatus;
  5108. // Check for our required function pointers being set up
  5109. if (pFT_SetBaudRate != IntPtr.Zero)
  5110. {
  5111. tFT_SetBaudRate FT_SetBaudRate = (tFT_SetBaudRate)Marshal.GetDelegateForFunctionPointer(pFT_SetBaudRate, typeof(tFT_SetBaudRate));
  5112. if (ftHandle != IntPtr.Zero)
  5113. {
  5114. // Call FT_SetBaudRate
  5115. ftStatus = FT_SetBaudRate(ftHandle, BaudRate);
  5116. }
  5117. }
  5118. else
  5119. {
  5120. if (pFT_SetBaudRate == IntPtr.Zero)
  5121. {
  5122. MessageBox.Show("Failed to load function FT_SetBaudRate.");
  5123. }
  5124. }
  5125. return ftStatus;
  5126. }
  5127. //**************************************************************************
  5128. // SetDataCharacteristics
  5129. //**************************************************************************
  5130. // Intellisense comments
  5131. /// <summary>
  5132. /// Sets the data bits, stop bits and parity for the device.
  5133. /// </summary>
  5134. /// <returns>FT_STATUS value from FT_SetDataCharacteristics in FTD2XX.DLL</returns>
  5135. /// <param name="DataBits">The number of data bits for UART data. Valid values are FT_DATA_BITS.FT_DATA_7 or FT_DATA_BITS.FT_BITS_8</param>
  5136. /// <param name="StopBits">The number of stop bits for UART data. Valid values are FT_STOP_BITS.FT_STOP_BITS_1 or FT_STOP_BITS.FT_STOP_BITS_2</param>
  5137. /// <param name="Parity">The parity of the UART data. Valid values are FT_PARITY.FT_PARITY_NONE, FT_PARITY.FT_PARITY_ODD, FT_PARITY.FT_PARITY_EVEN, FT_PARITY.FT_PARITY_MARK or FT_PARITY.FT_PARITY_SPACE</param>
  5138. public FT_STATUS SetDataCharacteristics(byte DataBits, byte StopBits, byte Parity)
  5139. {
  5140. // Initialise ftStatus to something other than FT_OK
  5141. FT_STATUS ftStatus = FT_STATUS.FT_OTHER_ERROR;
  5142. // If the DLL hasn't been loaded, just return here
  5143. if (hFTD2XXDLL == IntPtr.Zero)
  5144. return ftStatus;
  5145. // Check for our required function pointers being set up
  5146. if (pFT_SetDataCharacteristics != IntPtr.Zero)
  5147. {
  5148. tFT_SetDataCharacteristics FT_SetDataCharacteristics = (tFT_SetDataCharacteristics)Marshal.GetDelegateForFunctionPointer(pFT_SetDataCharacteristics, typeof(tFT_SetDataCharacteristics));
  5149. if (ftHandle != IntPtr.Zero)
  5150. {
  5151. // Call FT_SetDataCharacteristics
  5152. ftStatus = FT_SetDataCharacteristics(ftHandle, DataBits, StopBits, Parity);
  5153. }
  5154. }
  5155. else
  5156. {
  5157. if (pFT_SetDataCharacteristics == IntPtr.Zero)
  5158. {
  5159. MessageBox.Show("Failed to load function FT_SetDataCharacteristics.");
  5160. }
  5161. }
  5162. return ftStatus;
  5163. }
  5164. //**************************************************************************
  5165. // SetFlowControl
  5166. //**************************************************************************
  5167. // Intellisense comments
  5168. /// <summary>
  5169. /// Sets the flow control type.
  5170. /// </summary>
  5171. /// <returns>FT_STATUS value from FT_SetFlowControl in FTD2XX.DLL</returns>
  5172. /// <param name="FlowControl">The type of flow control for the UART. Valid values are FT_FLOW_CONTROL.FT_FLOW_NONE, FT_FLOW_CONTROL.FT_FLOW_RTS_CTS, FT_FLOW_CONTROL.FT_FLOW_DTR_DSR or FT_FLOW_CONTROL.FT_FLOW_XON_XOFF</param>
  5173. /// <param name="Xon">The Xon character for Xon/Xoff flow control. Ignored if not using Xon/XOff flow control.</param>
  5174. /// <param name="Xoff">The Xoff character for Xon/Xoff flow control. Ignored if not using Xon/XOff flow control.</param>
  5175. public FT_STATUS SetFlowControl(UInt16 FlowControl, byte Xon, byte Xoff)
  5176. {
  5177. // Initialise ftStatus to something other than FT_OK
  5178. FT_STATUS ftStatus = FT_STATUS.FT_OTHER_ERROR;
  5179. // If the DLL hasn't been loaded, just return here
  5180. if (hFTD2XXDLL == IntPtr.Zero)
  5181. return ftStatus;
  5182. // Check for our required function pointers being set up
  5183. if (pFT_SetFlowControl != IntPtr.Zero)
  5184. {
  5185. tFT_SetFlowControl FT_SetFlowControl = (tFT_SetFlowControl)Marshal.GetDelegateForFunctionPointer(pFT_SetFlowControl, typeof(tFT_SetFlowControl));
  5186. if (ftHandle != IntPtr.Zero)
  5187. {
  5188. // Call FT_SetFlowControl
  5189. ftStatus = FT_SetFlowControl(ftHandle, FlowControl, Xon, Xoff);
  5190. }
  5191. }
  5192. else
  5193. {
  5194. if (pFT_SetFlowControl == IntPtr.Zero)
  5195. {
  5196. MessageBox.Show("Failed to load function FT_SetFlowControl.");
  5197. }
  5198. }
  5199. return ftStatus;
  5200. }
  5201. //**************************************************************************
  5202. // SetRTS
  5203. //**************************************************************************
  5204. // Intellisense comments
  5205. /// <summary>
  5206. /// Asserts or de-asserts the Request To Send (RTS) line.
  5207. /// </summary>
  5208. /// <returns>FT_STATUS value from FT_SetRts or FT_ClrRts in FTD2XX.DLL</returns>
  5209. /// <param name="Enable">If true, asserts RTS. If false, de-asserts RTS</param>
  5210. public FT_STATUS SetRTS(bool Enable)
  5211. {
  5212. // Initialise ftStatus to something other than FT_OK
  5213. FT_STATUS ftStatus = FT_STATUS.FT_OTHER_ERROR;
  5214. // If the DLL hasn't been loaded, just return here
  5215. if (hFTD2XXDLL == IntPtr.Zero)
  5216. return ftStatus;
  5217. // Check for our required function pointers being set up
  5218. if ((pFT_SetRts != IntPtr.Zero) & (pFT_ClrRts != IntPtr.Zero))
  5219. {
  5220. tFT_SetRts FT_SetRts = (tFT_SetRts)Marshal.GetDelegateForFunctionPointer(pFT_SetRts, typeof(tFT_SetRts));
  5221. tFT_ClrRts FT_ClrRts = (tFT_ClrRts)Marshal.GetDelegateForFunctionPointer(pFT_ClrRts, typeof(tFT_ClrRts));
  5222. if (ftHandle != IntPtr.Zero)
  5223. {
  5224. if (Enable)
  5225. {
  5226. // Call FT_SetRts
  5227. ftStatus = FT_SetRts(ftHandle);
  5228. }
  5229. else
  5230. {
  5231. // Call FT_ClrRts
  5232. ftStatus = FT_ClrRts(ftHandle);
  5233. }
  5234. }
  5235. }
  5236. else
  5237. {
  5238. if (pFT_SetRts == IntPtr.Zero)
  5239. {
  5240. MessageBox.Show("Failed to load function FT_SetRts.");
  5241. }
  5242. if (pFT_ClrRts == IntPtr.Zero)
  5243. {
  5244. MessageBox.Show("Failed to load function FT_ClrRts.");
  5245. }
  5246. }
  5247. return ftStatus;
  5248. }
  5249. //**************************************************************************
  5250. // SetDTR
  5251. //**************************************************************************
  5252. // Intellisense comments
  5253. /// <summary>
  5254. /// Asserts or de-asserts the Data Terminal Ready (DTR) line.
  5255. /// </summary>
  5256. /// <returns>FT_STATUS value from FT_SetDtr or FT_ClrDtr in FTD2XX.DLL</returns>
  5257. /// <param name="Enable">If true, asserts DTR. If false, de-asserts DTR.</param>
  5258. public FT_STATUS SetDTR(bool Enable)
  5259. {
  5260. // Initialise ftStatus to something other than FT_OK
  5261. FT_STATUS ftStatus = FT_STATUS.FT_OTHER_ERROR;
  5262. // If the DLL hasn't been loaded, just return here
  5263. if (hFTD2XXDLL == IntPtr.Zero)
  5264. return ftStatus;
  5265. // Check for our required function pointers being set up
  5266. if ((pFT_SetDtr != IntPtr.Zero) & (pFT_ClrDtr != IntPtr.Zero))
  5267. {
  5268. tFT_SetDtr FT_SetDtr = (tFT_SetDtr)Marshal.GetDelegateForFunctionPointer(pFT_SetDtr, typeof(tFT_SetDtr));
  5269. tFT_ClrDtr FT_ClrDtr = (tFT_ClrDtr)Marshal.GetDelegateForFunctionPointer(pFT_ClrDtr, typeof(tFT_ClrDtr));
  5270. if (ftHandle != IntPtr.Zero)
  5271. {
  5272. if (Enable)
  5273. {
  5274. // Call FT_SetDtr
  5275. ftStatus = FT_SetDtr(ftHandle);
  5276. }
  5277. else
  5278. {
  5279. // Call FT_ClrDtr
  5280. ftStatus = FT_ClrDtr(ftHandle);
  5281. }
  5282. }
  5283. }
  5284. else
  5285. {
  5286. if (pFT_SetDtr == IntPtr.Zero)
  5287. {
  5288. MessageBox.Show("Failed to load function FT_SetDtr.");
  5289. }
  5290. if (pFT_ClrDtr == IntPtr.Zero)
  5291. {
  5292. MessageBox.Show("Failed to load function FT_ClrDtr.");
  5293. }
  5294. }
  5295. return ftStatus;
  5296. }
  5297. //**************************************************************************
  5298. // SetTimeouts
  5299. //**************************************************************************
  5300. // Intellisense comments
  5301. /// <summary>
  5302. /// Sets the read and write timeout values.
  5303. /// </summary>
  5304. /// <returns>FT_STATUS value from FT_SetTimeouts in FTD2XX.DLL</returns>
  5305. /// <param name="ReadTimeout">Read timeout value in ms. A value of 0 indicates an infinite timeout.</param>
  5306. /// <param name="WriteTimeout">Write timeout value in ms. A value of 0 indicates an infinite timeout.</param>
  5307. public FT_STATUS SetTimeouts(UInt32 ReadTimeout, UInt32 WriteTimeout)
  5308. {
  5309. // Initialise ftStatus to something other than FT_OK
  5310. FT_STATUS ftStatus = FT_STATUS.FT_OTHER_ERROR;
  5311. // If the DLL hasn't been loaded, just return here
  5312. if (hFTD2XXDLL == IntPtr.Zero)
  5313. return ftStatus;
  5314. // Check for our required function pointers being set up
  5315. if (pFT_SetTimeouts != IntPtr.Zero)
  5316. {
  5317. tFT_SetTimeouts FT_SetTimeouts = (tFT_SetTimeouts)Marshal.GetDelegateForFunctionPointer(pFT_SetTimeouts, typeof(tFT_SetTimeouts));
  5318. if (ftHandle != IntPtr.Zero)
  5319. {
  5320. // Call FT_SetTimeouts
  5321. ftStatus = FT_SetTimeouts(ftHandle, ReadTimeout, WriteTimeout);
  5322. }
  5323. }
  5324. else
  5325. {
  5326. if (pFT_SetTimeouts == IntPtr.Zero)
  5327. {
  5328. MessageBox.Show("Failed to load function FT_SetTimeouts.");
  5329. }
  5330. }
  5331. return ftStatus;
  5332. }
  5333. //**************************************************************************
  5334. // SetBreak
  5335. //**************************************************************************
  5336. // Intellisense comments
  5337. /// <summary>
  5338. /// Sets or clears the break state.
  5339. /// </summary>
  5340. /// <returns>FT_STATUS value from FT_SetBreakOn or FT_SetBreakOff in FTD2XX.DLL</returns>
  5341. /// <param name="Enable">If true, sets break on. If false, sets break off.</param>
  5342. public FT_STATUS SetBreak(bool Enable)
  5343. {
  5344. // Initialise ftStatus to something other than FT_OK
  5345. FT_STATUS ftStatus = FT_STATUS.FT_OTHER_ERROR;
  5346. // If the DLL hasn't been loaded, just return here
  5347. if (hFTD2XXDLL == IntPtr.Zero)
  5348. return ftStatus;
  5349. // Check for our required function pointers being set up
  5350. if ((pFT_SetBreakOn != IntPtr.Zero) & (pFT_SetBreakOff != IntPtr.Zero))
  5351. {
  5352. tFT_SetBreakOn FT_SetBreakOn = (tFT_SetBreakOn)Marshal.GetDelegateForFunctionPointer(pFT_SetBreakOn, typeof(tFT_SetBreakOn));
  5353. tFT_SetBreakOff FT_SetBreakOff = (tFT_SetBreakOff)Marshal.GetDelegateForFunctionPointer(pFT_SetBreakOff, typeof(tFT_SetBreakOff));
  5354. if (ftHandle != IntPtr.Zero)
  5355. {
  5356. if (Enable)
  5357. {
  5358. // Call FT_SetBreakOn
  5359. ftStatus = FT_SetBreakOn(ftHandle);
  5360. }
  5361. else
  5362. {
  5363. // Call FT_SetBreakOff
  5364. ftStatus = FT_SetBreakOff(ftHandle);
  5365. }
  5366. }
  5367. }
  5368. else
  5369. {
  5370. if (pFT_SetBreakOn == IntPtr.Zero)
  5371. {
  5372. MessageBox.Show("Failed to load function FT_SetBreakOn.");
  5373. }
  5374. if (pFT_SetBreakOff == IntPtr.Zero)
  5375. {
  5376. MessageBox.Show("Failed to load function FT_SetBreakOff.");
  5377. }
  5378. }
  5379. return ftStatus;
  5380. }
  5381. //**************************************************************************
  5382. // SetResetPipeRetryCount
  5383. //**************************************************************************
  5384. // Intellisense comments
  5385. /// <summary>
  5386. /// Gets or sets the reset pipe retry count. Default value is 50.
  5387. /// </summary>
  5388. /// <returns>FT_STATUS vlaue from FT_SetResetPipeRetryCount in FTD2XX.DLL</returns>
  5389. /// <param name="ResetPipeRetryCount">The reset pipe retry count.
  5390. /// Electrically noisy environments may benefit from a larger value.</param>
  5391. public FT_STATUS SetResetPipeRetryCount(UInt32 ResetPipeRetryCount)
  5392. {
  5393. // Initialise ftStatus to something other than FT_OK
  5394. FT_STATUS ftStatus = FT_STATUS.FT_OTHER_ERROR;
  5395. // If the DLL hasn't been loaded, just return here
  5396. if (hFTD2XXDLL == IntPtr.Zero)
  5397. return ftStatus;
  5398. // Check for our required function pointers being set up
  5399. if (pFT_SetResetPipeRetryCount != IntPtr.Zero)
  5400. {
  5401. tFT_SetResetPipeRetryCount FT_SetResetPipeRetryCount = (tFT_SetResetPipeRetryCount)Marshal.GetDelegateForFunctionPointer(pFT_SetResetPipeRetryCount, typeof(tFT_SetResetPipeRetryCount));
  5402. if (ftHandle != IntPtr.Zero)
  5403. {
  5404. // Call FT_SetResetPipeRetryCount
  5405. ftStatus = FT_SetResetPipeRetryCount(ftHandle, ResetPipeRetryCount);
  5406. }
  5407. }
  5408. else
  5409. {
  5410. if (pFT_SetResetPipeRetryCount == IntPtr.Zero)
  5411. {
  5412. MessageBox.Show("Failed to load function FT_SetResetPipeRetryCount.");
  5413. }
  5414. }
  5415. return ftStatus;
  5416. }
  5417. //**************************************************************************
  5418. // GetDriverVersion
  5419. //**************************************************************************
  5420. // Intellisense comments
  5421. /// <summary>
  5422. /// Gets the current FTDIBUS.SYS driver version number.
  5423. /// </summary>
  5424. /// <returns>FT_STATUS value from FT_GetDriverVersion in FTD2XX.DLL</returns>
  5425. /// <param name="DriverVersion">The current driver version number.</param>
  5426. public FT_STATUS GetDriverVersion(ref UInt32 DriverVersion)
  5427. {
  5428. // Initialise ftStatus to something other than FT_OK
  5429. FT_STATUS ftStatus = FT_STATUS.FT_OTHER_ERROR;
  5430. // If the DLL hasn't been loaded, just return here
  5431. if (hFTD2XXDLL == IntPtr.Zero)
  5432. return ftStatus;
  5433. // Check for our required function pointers being set up
  5434. if (pFT_GetDriverVersion != IntPtr.Zero)
  5435. {
  5436. tFT_GetDriverVersion FT_GetDriverVersion = (tFT_GetDriverVersion)Marshal.GetDelegateForFunctionPointer(pFT_GetDriverVersion, typeof(tFT_GetDriverVersion));
  5437. if (ftHandle != IntPtr.Zero)
  5438. {
  5439. // Call FT_GetDriverVersion
  5440. ftStatus = FT_GetDriverVersion(ftHandle, ref DriverVersion);
  5441. }
  5442. }
  5443. else
  5444. {
  5445. if (pFT_GetDriverVersion == IntPtr.Zero)
  5446. {
  5447. MessageBox.Show("Failed to load function FT_GetDriverVersion.");
  5448. }
  5449. }
  5450. return ftStatus;
  5451. }
  5452. //**************************************************************************
  5453. // GetLibraryVersion
  5454. //**************************************************************************
  5455. // Intellisense comments
  5456. /// <summary>
  5457. /// Gets the current FTD2XX.DLL driver version number.
  5458. /// </summary>
  5459. /// <returns>FT_STATUS value from FT_GetLibraryVersion in FTD2XX.DLL</returns>
  5460. /// <param name="LibraryVersion">The current library version.</param>
  5461. public FT_STATUS GetLibraryVersion(ref UInt32 LibraryVersion)
  5462. {
  5463. // Initialise ftStatus to something other than FT_OK
  5464. FT_STATUS ftStatus = FT_STATUS.FT_OTHER_ERROR;
  5465. // If the DLL hasn't been loaded, just return here
  5466. if (hFTD2XXDLL == IntPtr.Zero)
  5467. return ftStatus;
  5468. // Check for our required function pointers being set up
  5469. if (pFT_GetLibraryVersion != IntPtr.Zero)
  5470. {
  5471. tFT_GetLibraryVersion FT_GetLibraryVersion = (tFT_GetLibraryVersion)Marshal.GetDelegateForFunctionPointer(pFT_GetLibraryVersion, typeof(tFT_GetLibraryVersion));
  5472. // Call FT_GetLibraryVersion
  5473. ftStatus = FT_GetLibraryVersion(ref LibraryVersion);
  5474. }
  5475. else
  5476. {
  5477. if (pFT_GetLibraryVersion == IntPtr.Zero)
  5478. {
  5479. MessageBox.Show("Failed to load function FT_GetLibraryVersion.");
  5480. }
  5481. }
  5482. return ftStatus;
  5483. }
  5484. //**************************************************************************
  5485. // SetDeadmanTimeout
  5486. //**************************************************************************
  5487. // Intellisense comments
  5488. /// <summary>
  5489. /// Sets the USB deadman timeout value. Default is 5000ms.
  5490. /// </summary>
  5491. /// <returns>FT_STATUS value from FT_SetDeadmanTimeout in FTD2XX.DLL</returns>
  5492. /// <param name="DeadmanTimeout">The deadman timeout value in ms. Default is 5000ms.</param>
  5493. public FT_STATUS SetDeadmanTimeout(UInt32 DeadmanTimeout)
  5494. {
  5495. // Initialise ftStatus to something other than FT_OK
  5496. FT_STATUS ftStatus = FT_STATUS.FT_OTHER_ERROR;
  5497. // If the DLL hasn't been loaded, just return here
  5498. if (hFTD2XXDLL == IntPtr.Zero)
  5499. return ftStatus;
  5500. // Check for our required function pointers being set up
  5501. if (pFT_SetDeadmanTimeout != IntPtr.Zero)
  5502. {
  5503. tFT_SetDeadmanTimeout FT_SetDeadmanTimeout = (tFT_SetDeadmanTimeout)Marshal.GetDelegateForFunctionPointer(pFT_SetDeadmanTimeout, typeof(tFT_SetDeadmanTimeout));
  5504. if (ftHandle != IntPtr.Zero)
  5505. {
  5506. // Call FT_SetDeadmanTimeout
  5507. ftStatus = FT_SetDeadmanTimeout(ftHandle, DeadmanTimeout);
  5508. }
  5509. }
  5510. else
  5511. {
  5512. if (pFT_SetDeadmanTimeout == IntPtr.Zero)
  5513. {
  5514. MessageBox.Show("Failed to load function FT_SetDeadmanTimeout.");
  5515. }
  5516. }
  5517. return ftStatus;
  5518. }
  5519. //**************************************************************************
  5520. // SetLatency
  5521. //**************************************************************************
  5522. // Intellisense comments
  5523. /// <summary>
  5524. /// Sets the value of the latency timer. Default value is 16ms.
  5525. /// </summary>
  5526. /// <returns>FT_STATUS value from FT_SetLatencyTimer in FTD2XX.DLL</returns>
  5527. /// <param name="Latency">The latency timer value in ms.
  5528. /// Valid values are 2ms - 255ms for FT232BM, FT245BM and FT2232 devices.
  5529. /// Valid values are 0ms - 255ms for other devices.</param>
  5530. public FT_STATUS SetLatency(byte Latency)
  5531. {
  5532. // Initialise ftStatus to something other than FT_OK
  5533. FT_STATUS ftStatus = FT_STATUS.FT_OTHER_ERROR;
  5534. // If the DLL hasn't been loaded, just return here
  5535. if (hFTD2XXDLL == IntPtr.Zero)
  5536. return ftStatus;
  5537. // Check for our required function pointers being set up
  5538. if (pFT_SetLatencyTimer != IntPtr.Zero)
  5539. {
  5540. tFT_SetLatencyTimer FT_SetLatencyTimer = (tFT_SetLatencyTimer)Marshal.GetDelegateForFunctionPointer(pFT_SetLatencyTimer, typeof(tFT_SetLatencyTimer));
  5541. if (ftHandle != IntPtr.Zero)
  5542. {
  5543. FT_DEVICE DeviceType = FT_DEVICE.FT_DEVICE_UNKNOWN;
  5544. // Set Bit Mode does not apply to FT8U232AM, FT8U245AM or FT8U100AX devices
  5545. GetDeviceType(ref DeviceType);
  5546. if ((DeviceType == FT_DEVICE.FT_DEVICE_BM) || (DeviceType == FT_DEVICE.FT_DEVICE_2232))
  5547. {
  5548. // Do not allow latency of 1ms or 0ms for older devices
  5549. // since this can cause problems/lock up due to buffering mechanism
  5550. if (Latency < 2)
  5551. Latency = 2;
  5552. }
  5553. // Call FT_SetLatencyTimer
  5554. ftStatus = FT_SetLatencyTimer(ftHandle, Latency);
  5555. }
  5556. }
  5557. else
  5558. {
  5559. if (pFT_SetLatencyTimer == IntPtr.Zero)
  5560. {
  5561. MessageBox.Show("Failed to load function FT_SetLatencyTimer.");
  5562. }
  5563. }
  5564. return ftStatus;
  5565. }
  5566. //**************************************************************************
  5567. // GetLatency
  5568. //**************************************************************************
  5569. // Intellisense comments
  5570. /// <summary>
  5571. /// Gets the value of the latency timer. Default value is 16ms.
  5572. /// </summary>
  5573. /// <returns>FT_STATUS value from FT_GetLatencyTimer in FTD2XX.DLL</returns>
  5574. /// <param name="Latency">The latency timer value in ms.</param>
  5575. public FT_STATUS GetLatency(ref byte Latency)
  5576. {
  5577. // Initialise ftStatus to something other than FT_OK
  5578. FT_STATUS ftStatus = FT_STATUS.FT_OTHER_ERROR;
  5579. // If the DLL hasn't been loaded, just return here
  5580. if (hFTD2XXDLL == IntPtr.Zero)
  5581. return ftStatus;
  5582. // Check for our required function pointers being set up
  5583. if (pFT_GetLatencyTimer != IntPtr.Zero)
  5584. {
  5585. tFT_GetLatencyTimer FT_GetLatencyTimer = (tFT_GetLatencyTimer)Marshal.GetDelegateForFunctionPointer(pFT_GetLatencyTimer, typeof(tFT_GetLatencyTimer));
  5586. if (ftHandle != IntPtr.Zero)
  5587. {
  5588. // Call FT_GetLatencyTimer
  5589. ftStatus = FT_GetLatencyTimer(ftHandle, ref Latency);
  5590. }
  5591. }
  5592. else
  5593. {
  5594. if (pFT_GetLatencyTimer == IntPtr.Zero)
  5595. {
  5596. MessageBox.Show("Failed to load function FT_GetLatencyTimer.");
  5597. }
  5598. }
  5599. return ftStatus;
  5600. }
  5601. //**************************************************************************
  5602. // SetUSBTransferSizes
  5603. //**************************************************************************
  5604. // Intellisense comments
  5605. /// <summary>
  5606. /// Sets the USB IN and OUT transfer sizes.
  5607. /// </summary>
  5608. /// <returns>FT_STATUS value from FT_SetUSBParameters in FTD2XX.DLL</returns>
  5609. /// <param name="InTransferSize">The USB IN transfer size in bytes.</param>
  5610. public FT_STATUS InTransferSize(UInt32 InTransferSize)
  5611. // Only support IN transfer sizes at the moment
  5612. //public UInt32 InTransferSize(UInt32 InTransferSize, UInt32 OutTransferSize)
  5613. {
  5614. // Initialise ftStatus to something other than FT_OK
  5615. FT_STATUS ftStatus = FT_STATUS.FT_OTHER_ERROR;
  5616. // If the DLL hasn't been loaded, just return here
  5617. if (hFTD2XXDLL == IntPtr.Zero)
  5618. return ftStatus;
  5619. // Check for our required function pointers being set up
  5620. if (pFT_SetUSBParameters != IntPtr.Zero)
  5621. {
  5622. tFT_SetUSBParameters FT_SetUSBParameters = (tFT_SetUSBParameters)Marshal.GetDelegateForFunctionPointer(pFT_SetUSBParameters, typeof(tFT_SetUSBParameters));
  5623. UInt32 OutTransferSize = InTransferSize;
  5624. if (ftHandle != IntPtr.Zero)
  5625. {
  5626. // Call FT_SetUSBParameters
  5627. ftStatus = FT_SetUSBParameters(ftHandle, InTransferSize, OutTransferSize);
  5628. }
  5629. }
  5630. else
  5631. {
  5632. if (pFT_SetUSBParameters == IntPtr.Zero)
  5633. {
  5634. MessageBox.Show("Failed to load function FT_SetUSBParameters.");
  5635. }
  5636. }
  5637. return ftStatus;
  5638. }
  5639. //**************************************************************************
  5640. // SetCharacters
  5641. //**************************************************************************
  5642. // Intellisense comments
  5643. /// <summary>
  5644. /// Sets an event character, an error character and enables or disables them.
  5645. /// </summary>
  5646. /// <returns>FT_STATUS value from FT_SetChars in FTD2XX.DLL</returns>
  5647. /// <param name="EventChar">A character that will be tigger an IN to the host when this character is received.</param>
  5648. /// <param name="EventCharEnable">Determines if the EventChar is enabled or disabled.</param>
  5649. /// <param name="ErrorChar">A character that will be inserted into the data stream to indicate that an error has occurred.</param>
  5650. /// <param name="ErrorCharEnable">Determines if the ErrorChar is enabled or disabled.</param>
  5651. public FT_STATUS SetCharacters(byte EventChar, bool EventCharEnable, byte ErrorChar, bool ErrorCharEnable)
  5652. {
  5653. // Initialise ftStatus to something other than FT_OK
  5654. FT_STATUS ftStatus = FT_STATUS.FT_OTHER_ERROR;
  5655. // If the DLL hasn't been loaded, just return here
  5656. if (hFTD2XXDLL == IntPtr.Zero)
  5657. return ftStatus;
  5658. // Check for our required function pointers being set up
  5659. if (pFT_SetChars != IntPtr.Zero)
  5660. {
  5661. tFT_SetChars FT_SetChars = (tFT_SetChars)Marshal.GetDelegateForFunctionPointer(pFT_SetChars, typeof(tFT_SetChars));
  5662. if (ftHandle != IntPtr.Zero)
  5663. {
  5664. // Call FT_SetChars
  5665. ftStatus = FT_SetChars(ftHandle, EventChar, Convert.ToByte(EventCharEnable), ErrorChar, Convert.ToByte(ErrorCharEnable));
  5666. }
  5667. }
  5668. else
  5669. {
  5670. if (pFT_SetChars == IntPtr.Zero)
  5671. {
  5672. MessageBox.Show("Failed to load function FT_SetChars.");
  5673. }
  5674. }
  5675. return ftStatus;
  5676. }
  5677. //**************************************************************************
  5678. // GetEEUserAreaSize
  5679. //**************************************************************************
  5680. // Intellisense comments
  5681. /// <summary>
  5682. /// Gets the size of the EEPROM user area.
  5683. /// </summary>
  5684. /// <returns>FT_STATUS value from FT_EE_UASize in FTD2XX.DLL</returns>
  5685. /// <param name="UASize">The EEPROM user area size in bytes.</param>
  5686. public FT_STATUS EEUserAreaSize(ref UInt32 UASize)
  5687. {
  5688. // Initialise ftStatus to something other than FT_OK
  5689. FT_STATUS ftStatus = FT_STATUS.FT_OTHER_ERROR;
  5690. // If the DLL hasn't been loaded, just return here
  5691. if (hFTD2XXDLL == IntPtr.Zero)
  5692. return ftStatus;
  5693. // Check for our required function pointers being set up
  5694. if (pFT_EE_UASize != IntPtr.Zero)
  5695. {
  5696. tFT_EE_UASize FT_EE_UASize = (tFT_EE_UASize)Marshal.GetDelegateForFunctionPointer(pFT_EE_UASize, typeof(tFT_EE_UASize));
  5697. if (ftHandle != IntPtr.Zero)
  5698. {
  5699. ftStatus = FT_EE_UASize(ftHandle, ref UASize);
  5700. }
  5701. }
  5702. else
  5703. {
  5704. if (pFT_EE_UASize == IntPtr.Zero)
  5705. {
  5706. MessageBox.Show("Failed to load function FT_EE_UASize.");
  5707. }
  5708. }
  5709. return ftStatus;
  5710. }
  5711. //**************************************************************************
  5712. // GetCOMPort
  5713. //**************************************************************************
  5714. // Intellisense comments
  5715. /// <summary>
  5716. /// Gets the corresponding COM port number for the current device. If no COM port is exposed, an empty string is returned.
  5717. /// </summary>
  5718. /// <returns>FT_STATUS value from FT_GetComPortNumber in FTD2XX.DLL</returns>
  5719. /// <param name="ComPortName">The COM port name corresponding to the current device. If no COM port is installed, an empty string is passed back.</param>
  5720. public FT_STATUS GetCOMPort(out string ComPortName)
  5721. {
  5722. // Initialise ftStatus to something other than FT_OK
  5723. FT_STATUS ftStatus = FT_STATUS.FT_OTHER_ERROR;
  5724. // As ComPortName is an OUT paremeter, has to be assigned before returning
  5725. ComPortName = string.Empty;
  5726. // If the DLL hasn't been loaded, just return here
  5727. if (hFTD2XXDLL == IntPtr.Zero)
  5728. return ftStatus;
  5729. // Check for our required function pointers being set up
  5730. if (pFT_GetComPortNumber != IntPtr.Zero)
  5731. {
  5732. tFT_GetComPortNumber FT_GetComPortNumber = (tFT_GetComPortNumber)Marshal.GetDelegateForFunctionPointer(pFT_GetComPortNumber, typeof(tFT_GetComPortNumber));
  5733. Int32 ComPortNumber = -1;
  5734. if (ftHandle != IntPtr.Zero)
  5735. {
  5736. // Call FT_GetComPortNumber
  5737. ftStatus = FT_GetComPortNumber(ftHandle, ref ComPortNumber);
  5738. }
  5739. if (ComPortNumber == -1)
  5740. {
  5741. // If no COM port installed, return an empty string
  5742. ComPortName = string.Empty;
  5743. }
  5744. else
  5745. {
  5746. // If installed, return full COM string
  5747. // This can then be passed to an instance of the SerialPort class to assign the port number.
  5748. ComPortName = "COM" + ComPortNumber.ToString();
  5749. }
  5750. }
  5751. else
  5752. {
  5753. if (pFT_GetComPortNumber == IntPtr.Zero)
  5754. {
  5755. MessageBox.Show("Failed to load function FT_GetComPortNumber.");
  5756. }
  5757. }
  5758. return ftStatus;
  5759. }
  5760. #endregion
  5761. #region PROPERTY_DEFINITIONS
  5762. //**************************************************************************
  5763. // IsOpen
  5764. //**************************************************************************
  5765. // Intellisense comments
  5766. /// <summary>
  5767. /// Gets the open status of the device.
  5768. /// </summary>
  5769. public bool IsOpen
  5770. {
  5771. get
  5772. {
  5773. if (ftHandle == IntPtr.Zero)
  5774. return false;
  5775. else
  5776. return true;
  5777. }
  5778. }
  5779. //**************************************************************************
  5780. // InterfaceIdentifier
  5781. //**************************************************************************
  5782. // Intellisense comments
  5783. /// <summary>
  5784. /// Gets the interface identifier.
  5785. /// </summary>
  5786. private string InterfaceIdentifier
  5787. {
  5788. get
  5789. {
  5790. string Identifier;
  5791. Identifier = String.Empty;
  5792. if (IsOpen)
  5793. {
  5794. FT_DEVICE deviceType = FT_DEVICE.FT_DEVICE_BM;
  5795. GetDeviceType(ref deviceType);
  5796. if ((deviceType == FT_DEVICE.FT_DEVICE_2232) | (deviceType == FT_DEVICE.FT_DEVICE_2232H) | (deviceType == FT_DEVICE.FT_DEVICE_4232H))
  5797. {
  5798. string Description;
  5799. GetDescription(out Description);
  5800. Identifier = Description.Substring((Description.Length - 1));
  5801. return Identifier;
  5802. }
  5803. }
  5804. return Identifier;
  5805. }
  5806. }
  5807. #endregion
  5808. #region HELPER_METHODS
  5809. //**************************************************************************
  5810. // ErrorHandler
  5811. //**************************************************************************
  5812. /// <summary>
  5813. /// Method to check ftStatus and ftErrorCondition values for error conditions and throw exceptions accordingly.
  5814. /// </summary>
  5815. private void ErrorHandler(FT_STATUS ftStatus, FT_ERROR ftErrorCondition)
  5816. {
  5817. if (ftStatus != FT_STATUS.FT_OK)
  5818. {
  5819. // Check FT_STATUS values returned from FTD2XX DLL calls
  5820. switch (ftStatus)
  5821. {
  5822. case FT_STATUS.FT_DEVICE_NOT_FOUND:
  5823. {
  5824. throw new FT_EXCEPTION("FTDI device not found.");
  5825. }
  5826. case FT_STATUS.FT_DEVICE_NOT_OPENED:
  5827. {
  5828. throw new FT_EXCEPTION("FTDI device not opened.");
  5829. }
  5830. case FT_STATUS.FT_DEVICE_NOT_OPENED_FOR_ERASE:
  5831. {
  5832. throw new FT_EXCEPTION("FTDI device not opened for erase.");
  5833. }
  5834. case FT_STATUS.FT_DEVICE_NOT_OPENED_FOR_WRITE:
  5835. {
  5836. throw new FT_EXCEPTION("FTDI device not opened for write.");
  5837. }
  5838. case FT_STATUS.FT_EEPROM_ERASE_FAILED:
  5839. {
  5840. throw new FT_EXCEPTION("Failed to erase FTDI device EEPROM.");
  5841. }
  5842. case FT_STATUS.FT_EEPROM_NOT_PRESENT:
  5843. {
  5844. throw new FT_EXCEPTION("No EEPROM fitted to FTDI device.");
  5845. }
  5846. case FT_STATUS.FT_EEPROM_NOT_PROGRAMMED:
  5847. {
  5848. throw new FT_EXCEPTION("FTDI device EEPROM not programmed.");
  5849. }
  5850. case FT_STATUS.FT_EEPROM_READ_FAILED:
  5851. {
  5852. throw new FT_EXCEPTION("Failed to read FTDI device EEPROM.");
  5853. }
  5854. case FT_STATUS.FT_EEPROM_WRITE_FAILED:
  5855. {
  5856. throw new FT_EXCEPTION("Failed to write FTDI device EEPROM.");
  5857. }
  5858. case FT_STATUS.FT_FAILED_TO_WRITE_DEVICE:
  5859. {
  5860. throw new FT_EXCEPTION("Failed to write to FTDI device.");
  5861. }
  5862. case FT_STATUS.FT_INSUFFICIENT_RESOURCES:
  5863. {
  5864. throw new FT_EXCEPTION("Insufficient resources.");
  5865. }
  5866. case FT_STATUS.FT_INVALID_ARGS:
  5867. {
  5868. throw new FT_EXCEPTION("Invalid arguments for FTD2XX function call.");
  5869. }
  5870. case FT_STATUS.FT_INVALID_BAUD_RATE:
  5871. {
  5872. throw new FT_EXCEPTION("Invalid Baud rate for FTDI device.");
  5873. }
  5874. case FT_STATUS.FT_INVALID_HANDLE:
  5875. {
  5876. throw new FT_EXCEPTION("Invalid handle for FTDI device.");
  5877. }
  5878. case FT_STATUS.FT_INVALID_PARAMETER:
  5879. {
  5880. throw new FT_EXCEPTION("Invalid parameter for FTD2XX function call.");
  5881. }
  5882. case FT_STATUS.FT_IO_ERROR:
  5883. {
  5884. throw new FT_EXCEPTION("FTDI device IO error.");
  5885. }
  5886. case FT_STATUS.FT_OTHER_ERROR:
  5887. {
  5888. throw new FT_EXCEPTION("An unexpected error has occurred when trying to communicate with the FTDI device.");
  5889. }
  5890. default:
  5891. break;
  5892. }
  5893. }
  5894. if (ftErrorCondition != FT_ERROR.FT_NO_ERROR)
  5895. {
  5896. // Check for other error conditions not handled by FTD2XX DLL
  5897. switch (ftErrorCondition)
  5898. {
  5899. case FT_ERROR.FT_INCORRECT_DEVICE:
  5900. {
  5901. throw new FT_EXCEPTION("The current device type does not match the EEPROM structure.");
  5902. }
  5903. case FT_ERROR.FT_INVALID_BITMODE:
  5904. {
  5905. throw new FT_EXCEPTION("The requested bit mode is not valid for the current device.");
  5906. }
  5907. case FT_ERROR.FT_BUFFER_SIZE:
  5908. {
  5909. throw new FT_EXCEPTION("The supplied buffer is not big enough.");
  5910. }
  5911. default:
  5912. break;
  5913. }
  5914. }
  5915. return;
  5916. }
  5917. #endregion
  5918. }
  5919. }