-
Notifications
You must be signed in to change notification settings - Fork 0
/
FTDI.cs
5879 lines (5307 loc) · 272 KB
/
FTDI.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;
using System.IO;
namespace Graph_practice_2_Rolling_data
{
public class FTDI
{
// constructor
/// <summary>
/// Constructor for the FTDI class.
/// </summary>
public FTDI()
{
// If FTD2XX.DLL is NOT loaded already, load it
if (hFTD2XXDLL == IntPtr.Zero)
{
// Load our FTD2XX.DLL library
hFTD2XXDLL = LoadLibrary(@"FTD2XX.DLL");
if (hFTD2XXDLL == IntPtr.Zero)
{
// Failed to load our FTD2XX.DLL library from System32 or the application directory
// Try the same directory that this FTD2XX_NET DLL is in
MessageBox.Show("Attempting to load FTD2XX.DLL from:\n" + Path.GetDirectoryName(GetType().Assembly.Location));
hFTD2XXDLL = LoadLibrary(@Path.GetDirectoryName(GetType().Assembly.Location) + "\\FTD2XX.DLL");
}
}
// If we have succesfully loaded the library, get the function pointers set up
if (hFTD2XXDLL != IntPtr.Zero)
{
// Set up our function pointers for use through our exported methods
pFT_CreateDeviceInfoList = GetProcAddress(hFTD2XXDLL, "FT_CreateDeviceInfoList");
pFT_GetDeviceInfoDetail = GetProcAddress(hFTD2XXDLL, "FT_GetDeviceInfoDetail");
pFT_Open = GetProcAddress(hFTD2XXDLL, "FT_Open");
pFT_OpenEx = GetProcAddress(hFTD2XXDLL, "FT_OpenEx");
pFT_Close = GetProcAddress(hFTD2XXDLL, "FT_Close");
pFT_Read = GetProcAddress(hFTD2XXDLL, "FT_Read");
pFT_Write = GetProcAddress(hFTD2XXDLL, "FT_Write");
pFT_GetQueueStatus = GetProcAddress(hFTD2XXDLL, "FT_GetQueueStatus");
pFT_GetModemStatus = GetProcAddress(hFTD2XXDLL, "FT_GetModemStatus");
pFT_GetStatus = GetProcAddress(hFTD2XXDLL, "FT_GetStatus");
pFT_SetBaudRate = GetProcAddress(hFTD2XXDLL, "FT_SetBaudRate");
pFT_SetDataCharacteristics = GetProcAddress(hFTD2XXDLL, "FT_SetDataCharacteristics");
pFT_SetFlowControl = GetProcAddress(hFTD2XXDLL, "FT_SetFlowControl");
pFT_SetDtr = GetProcAddress(hFTD2XXDLL, "FT_SetDtr");
pFT_ClrDtr = GetProcAddress(hFTD2XXDLL, "FT_ClrDtr");
pFT_SetRts = GetProcAddress(hFTD2XXDLL, "FT_SetRts");
pFT_ClrRts = GetProcAddress(hFTD2XXDLL, "FT_ClrRts");
pFT_ResetDevice = GetProcAddress(hFTD2XXDLL, "FT_ResetDevice");
pFT_ResetPort = GetProcAddress(hFTD2XXDLL, "FT_ResetPort");
pFT_CyclePort = GetProcAddress(hFTD2XXDLL, "FT_CyclePort");
pFT_Rescan = GetProcAddress(hFTD2XXDLL, "FT_Rescan");
pFT_Reload = GetProcAddress(hFTD2XXDLL, "FT_Reload");
pFT_Purge = GetProcAddress(hFTD2XXDLL, "FT_Purge");
pFT_SetTimeouts = GetProcAddress(hFTD2XXDLL, "FT_SetTimeouts");
pFT_SetBreakOn = GetProcAddress(hFTD2XXDLL, "FT_SetBreakOn");
pFT_SetBreakOff = GetProcAddress(hFTD2XXDLL, "FT_SetBreakOff");
pFT_GetDeviceInfo = GetProcAddress(hFTD2XXDLL, "FT_GetDeviceInfo");
pFT_SetResetPipeRetryCount = GetProcAddress(hFTD2XXDLL, "FT_SetResetPipeRetryCount");
pFT_StopInTask = GetProcAddress(hFTD2XXDLL, "FT_StopInTask");
pFT_RestartInTask = GetProcAddress(hFTD2XXDLL, "FT_RestartInTask");
pFT_GetDriverVersion = GetProcAddress(hFTD2XXDLL, "FT_GetDriverVersion");
pFT_GetLibraryVersion = GetProcAddress(hFTD2XXDLL, "FT_GetLibraryVersion");
pFT_SetDeadmanTimeout = GetProcAddress(hFTD2XXDLL, "FT_SetDeadmanTimeout");
pFT_SetChars = GetProcAddress(hFTD2XXDLL, "FT_SetChars");
pFT_SetEventNotification = GetProcAddress(hFTD2XXDLL, "FT_SetEventNotification");
pFT_GetComPortNumber = GetProcAddress(hFTD2XXDLL, "FT_GetComPortNumber");
pFT_SetLatencyTimer = GetProcAddress(hFTD2XXDLL, "FT_SetLatencyTimer");
pFT_GetLatencyTimer = GetProcAddress(hFTD2XXDLL, "FT_GetLatencyTimer");
pFT_SetBitMode = GetProcAddress(hFTD2XXDLL, "FT_SetBitMode");
pFT_GetBitMode = GetProcAddress(hFTD2XXDLL, "FT_GetBitMode");
pFT_SetUSBParameters = GetProcAddress(hFTD2XXDLL, "FT_SetUSBParameters");
pFT_ReadEE = GetProcAddress(hFTD2XXDLL, "FT_ReadEE");
pFT_WriteEE = GetProcAddress(hFTD2XXDLL, "FT_WriteEE");
pFT_EraseEE = GetProcAddress(hFTD2XXDLL, "FT_EraseEE");
pFT_EE_UASize = GetProcAddress(hFTD2XXDLL, "FT_EE_UASize");
pFT_EE_UARead = GetProcAddress(hFTD2XXDLL, "FT_EE_UARead");
pFT_EE_UAWrite = GetProcAddress(hFTD2XXDLL, "FT_EE_UAWrite");
pFT_EE_Read = GetProcAddress(hFTD2XXDLL, "FT_EE_Read");
pFT_EE_Program = GetProcAddress(hFTD2XXDLL, "FT_EE_Program");
}
else
{
// Failed to load our DLL - alert the user
MessageBox.Show("Failed to load FTD2XX.DLL. Are the FTDI drivers installed?");
}
}
/// <summary>
/// Destructor for the FTDI class.
/// </summary>
~FTDI()
{
// FreeLibrary here - we should only do this if we are completely finished
FreeLibrary(hFTD2XXDLL);
hFTD2XXDLL = IntPtr.Zero;
}
/// <summary>
/// Built-in Windows API functions to allow us to dynamically load our own DLL.
/// Will allow us to use old versions of the DLL that do not have all of these functions available.
/// </summary>
[DllImport("kernel32.dll")]
private static extern IntPtr LoadLibrary(string dllToLoad);
[DllImport("kernel32.dll")]
private static extern IntPtr GetProcAddress(IntPtr hModule, string procedureName);
[DllImport("kernel32.dll")]
private static extern bool FreeLibrary(IntPtr hModule);
// Definitions for FTD2XX functions
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
private delegate FT_STATUS tFT_CreateDeviceInfoList(ref UInt32 numdevs);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
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);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
private delegate FT_STATUS tFT_Open(UInt32 index, ref IntPtr ftHandle);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
private delegate FT_STATUS tFT_OpenEx(string devstring, UInt32 dwFlags, ref IntPtr ftHandle);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
private delegate FT_STATUS tFT_OpenExLoc(UInt32 devloc, UInt32 dwFlags, ref IntPtr ftHandle);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
private delegate FT_STATUS tFT_Close(IntPtr ftHandle);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
private delegate FT_STATUS tFT_Read(IntPtr ftHandle, byte[] lpBuffer, UInt32 dwBytesToRead, ref UInt32 lpdwBytesReturned);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
private delegate FT_STATUS tFT_Write(IntPtr ftHandle, byte[] lpBuffer, UInt32 dwBytesToWrite, ref UInt32 lpdwBytesWritten);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
private delegate FT_STATUS tFT_GetQueueStatus(IntPtr ftHandle, ref UInt32 lpdwAmountInRxQueue);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
private delegate FT_STATUS tFT_GetModemStatus(IntPtr ftHandle, ref UInt32 lpdwModemStatus);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
private delegate FT_STATUS tFT_GetStatus(IntPtr ftHandle, ref UInt32 lpdwAmountInRxQueue, ref UInt32 lpdwAmountInTxQueue, ref UInt32 lpdwEventStatus);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
private delegate FT_STATUS tFT_SetBaudRate(IntPtr ftHandle, UInt32 dwBaudRate);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
private delegate FT_STATUS tFT_SetDataCharacteristics(IntPtr ftHandle, byte uWordLength, byte uStopBits, byte uParity);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
private delegate FT_STATUS tFT_SetFlowControl(IntPtr ftHandle, UInt16 usFlowControl, byte uXon, byte uXoff);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
private delegate FT_STATUS tFT_SetDtr(IntPtr ftHandle);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
private delegate FT_STATUS tFT_ClrDtr(IntPtr ftHandle);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
private delegate FT_STATUS tFT_SetRts(IntPtr ftHandle);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
private delegate FT_STATUS tFT_ClrRts(IntPtr ftHandle);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
private delegate FT_STATUS tFT_ResetDevice(IntPtr ftHandle);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
private delegate FT_STATUS tFT_ResetPort(IntPtr ftHandle);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
private delegate FT_STATUS tFT_CyclePort(IntPtr ftHandle);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
private delegate FT_STATUS tFT_Rescan();
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
private delegate FT_STATUS tFT_Reload(UInt16 wVID, UInt16 wPID);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
private delegate FT_STATUS tFT_Purge(IntPtr ftHandle, UInt32 dwMask);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
private delegate FT_STATUS tFT_SetTimeouts(IntPtr ftHandle, UInt32 dwReadTimeout, UInt32 dwWriteTimeout);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
private delegate FT_STATUS tFT_SetBreakOn(IntPtr ftHandle);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
private delegate FT_STATUS tFT_SetBreakOff(IntPtr ftHandle);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
private delegate FT_STATUS tFT_GetDeviceInfo(IntPtr ftHandle, ref FT_DEVICE pftType, ref UInt32 lpdwID, byte[] pcSerialNumber, byte[] pcDescription, IntPtr pvDummy);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
private delegate FT_STATUS tFT_SetResetPipeRetryCount(IntPtr ftHandle, UInt32 dwCount);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
private delegate FT_STATUS tFT_StopInTask(IntPtr ftHandle);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
private delegate FT_STATUS tFT_RestartInTask(IntPtr ftHandle);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
private delegate FT_STATUS tFT_GetDriverVersion(IntPtr ftHandle, ref UInt32 lpdwDriverVersion);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
private delegate FT_STATUS tFT_GetLibraryVersion(ref UInt32 lpdwLibraryVersion);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
private delegate FT_STATUS tFT_SetDeadmanTimeout(IntPtr ftHandle, UInt32 dwDeadmanTimeout);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
private delegate FT_STATUS tFT_SetChars(IntPtr ftHandle, byte uEventCh, byte uEventChEn, byte uErrorCh, byte uErrorChEn);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
private delegate FT_STATUS tFT_SetEventNotification(IntPtr ftHandle, UInt32 dwEventMask, SafeHandle hEvent);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
private delegate FT_STATUS tFT_GetComPortNumber(IntPtr ftHandle, ref Int32 dwComPortNumber);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
private delegate FT_STATUS tFT_SetLatencyTimer(IntPtr ftHandle, byte ucLatency);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
private delegate FT_STATUS tFT_GetLatencyTimer(IntPtr ftHandle, ref byte ucLatency);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
private delegate FT_STATUS tFT_SetBitMode(IntPtr ftHandle, byte ucMask, byte ucMode);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
private delegate FT_STATUS tFT_GetBitMode(IntPtr ftHandle, ref byte ucMode);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
private delegate FT_STATUS tFT_SetUSBParameters(IntPtr ftHandle, UInt32 dwInTransferSize, UInt32 dwOutTransferSize);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
private delegate FT_STATUS tFT_ReadEE(IntPtr ftHandle, UInt32 dwWordOffset, ref UInt16 lpwValue);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
private delegate FT_STATUS tFT_WriteEE(IntPtr ftHandle, UInt32 dwWordOffset, UInt16 wValue);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
private delegate FT_STATUS tFT_EraseEE(IntPtr ftHandle);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
private delegate FT_STATUS tFT_EE_UASize(IntPtr ftHandle, ref UInt32 dwSize);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
private delegate FT_STATUS tFT_EE_UARead(IntPtr ftHandle, byte[] pucData, Int32 dwDataLen, ref UInt32 lpdwDataRead);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
private delegate FT_STATUS tFT_EE_UAWrite(IntPtr ftHandle, byte[] pucData, Int32 dwDataLen);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
private delegate FT_STATUS tFT_EE_Read(IntPtr ftHandle, FT_PROGRAM_DATA pData);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
private delegate FT_STATUS tFT_EE_Program(IntPtr ftHandle, FT_PROGRAM_DATA pData);
//**************************************************************************
//
// CONSTANTS
//
//**************************************************************************
// Constants for FT_STATUS
/// <summary>
/// Status values for FTDI devices.
/// </summary>
public enum FT_STATUS
{
/// <summary>
/// Status OK
/// </summary>
FT_OK = 0,
/// <summary>
/// The device handle is invalid
/// </summary>
FT_INVALID_HANDLE,
/// <summary>
/// Device not found
/// </summary>
FT_DEVICE_NOT_FOUND,
/// <summary>
/// Device is not open
/// </summary>
FT_DEVICE_NOT_OPENED,
/// <summary>
/// IO error
/// </summary>
FT_IO_ERROR,
/// <summary>
/// Insufficient resources
/// </summary>
FT_INSUFFICIENT_RESOURCES,
/// <summary>
/// A parameter was invalid
/// </summary>
FT_INVALID_PARAMETER,
/// <summary>
/// The requested baud rate is invalid
/// </summary>
FT_INVALID_BAUD_RATE,
/// <summary>
/// Device not opened for erase
/// </summary>
FT_DEVICE_NOT_OPENED_FOR_ERASE,
/// <summary>
/// Device not poened for write
/// </summary>
FT_DEVICE_NOT_OPENED_FOR_WRITE,
/// <summary>
/// Failed to write to device
/// </summary>
FT_FAILED_TO_WRITE_DEVICE,
/// <summary>
/// Failed to read the device EEPROM
/// </summary>
FT_EEPROM_READ_FAILED,
/// <summary>
/// Failed to write the device EEPROM
/// </summary>
FT_EEPROM_WRITE_FAILED,
/// <summary>
/// Failed to erase the device EEPROM
/// </summary>
FT_EEPROM_ERASE_FAILED,
/// <summary>
/// An EEPROM is not fitted to the device
/// </summary>
FT_EEPROM_NOT_PRESENT,
/// <summary>
/// Device EEPROM is blank
/// </summary>
FT_EEPROM_NOT_PROGRAMMED,
/// <summary>
/// Invalid arguments
/// </summary>
FT_INVALID_ARGS,
/// <summary>
/// An other error has occurred
/// </summary>
FT_OTHER_ERROR
};
// Constants for other error states internal to this class library
/// <summary>
/// Error states not supported by FTD2XX DLL.
/// </summary>
private enum FT_ERROR
{
FT_NO_ERROR = 0,
FT_INCORRECT_DEVICE,
FT_INVALID_BITMODE,
FT_BUFFER_SIZE
};
// Flags for FT_OpenEx
private const UInt32 FT_OPEN_BY_SERIAL_NUMBER = 0x00000001;
private const UInt32 FT_OPEN_BY_DESCRIPTION = 0x00000002;
private const UInt32 FT_OPEN_BY_LOCATION = 0x00000004;
// Word Lengths
/// <summary>
/// Permitted data bits for FTDI devices
/// </summary>
public class FT_DATA_BITS
{
/// <summary>
/// 8 data bits
/// </summary>
public const byte FT_BITS_8 = 0x08;
/// <summary>
/// 7 data bits
/// </summary>
public const byte FT_BITS_7 = 0x07;
}
// Stop Bits
/// <summary>
/// Permitted stop bits for FTDI devices
/// </summary>
public class FT_STOP_BITS
{
/// <summary>
/// 1 stop bit
/// </summary>
public const byte FT_STOP_BITS_1 = 0x00;
/// <summary>
/// 2 stop bits
/// </summary>
public const byte FT_STOP_BITS_2 = 0x02;
}
// Parity
/// <summary>
/// Permitted parity values for FTDI devices
/// </summary>
public class FT_PARITY
{
/// <summary>
/// No parity
/// </summary>
public const byte FT_PARITY_NONE = 0x00;
/// <summary>
/// Odd parity
/// </summary>
public const byte FT_PARITY_ODD = 0x01;
/// <summary>
/// Even parity
/// </summary>
public const byte FT_PARITY_EVEN = 0x02;
/// <summary>
/// Mark parity
/// </summary>
public const byte FT_PARITY_MARK = 0x03;
/// <summary>
/// Space parity
/// </summary>
public const byte FT_PARITY_SPACE = 0x04;
}
// Flow Control
/// <summary>
/// Permitted flow control values for FTDI devices
/// </summary>
public class FT_FLOW_CONTROL
{
/// <summary>
/// No flow control
/// </summary>
public const UInt16 FT_FLOW_NONE = 0x0000;
/// <summary>
/// RTS/CTS flow control
/// </summary>
public const UInt16 FT_FLOW_RTS_CTS = 0x0100;
/// <summary>
/// DTR/DSR flow control
/// </summary>
public const UInt16 FT_FLOW_DTR_DSR = 0x0200;
/// <summary>
/// Xon/Xoff flow control
/// </summary>
public const UInt16 FT_FLOW_XON_XOFF = 0x0400;
}
// Purge Rx and Tx buffers
/// <summary>
/// Purge buffer constant definitions
/// </summary>
public class FT_PURGE
{
/// <summary>
/// Purge Rx buffer
/// </summary>
public const byte FT_PURGE_RX = 0x01;
/// <summary>
/// Purge Tx buffer
/// </summary>
public const byte FT_PURGE_TX = 0x02;
}
// Modem Status bits
/// <summary>
/// Modem status bit definitions
/// </summary>
public class FT_MODEM_STATUS
{
/// <summary>
/// Clear To Send (CTS) modem status
/// </summary>
public const byte FT_CTS = 0x10;
/// <summary>
/// Data Set Ready (DSR) modem status
/// </summary>
public const byte FT_DSR = 0x20;
/// <summary>
/// Ring Indicator (RI) modem status
/// </summary>
public const byte FT_RI = 0x40;
/// <summary>
/// Data Carrier Detect (DCD) modem status
/// </summary>
public const byte FT_DCD = 0x80;
}
// Line Status bits
/// <summary>
/// Line status bit definitions
/// </summary>
public class FT_LINE_STATUS
{
/// <summary>
/// Overrun Error (OE) line status
/// </summary>
public const byte FT_OE = 0x02;
/// <summary>
/// Parity Error (PE) line status
/// </summary>
public const byte FT_PE = 0x04;
/// <summary>
/// Framing Error (FE) line status
/// </summary>
public const byte FT_FE = 0x08;
/// <summary>
/// Break Interrupt (BI) line status
/// </summary>
public const byte FT_BI = 0x10;
}
// Events
/// <summary>
/// FTDI device event types that can be monitored
/// </summary>
public class FT_EVENTS
{
/// <summary>
/// Event on receive character
/// </summary>
public const UInt32 FT_EVENT_RXCHAR = 0x00000001;
/// <summary>
/// Event on modem status change
/// </summary>
public const UInt32 FT_EVENT_MODEM_STATUS = 0x00000002;
/// <summary>
/// Event on line status change
/// </summary>
public const UInt32 FT_EVENT_LINE_STATUS = 0x00000004;
}
// Bit modes
/// <summary>
/// Permitted bit mode values for FTDI devices. For use with SetBitMode
/// </summary>
public class FT_BIT_MODES
{
/// <summary>
/// Reset bit mode
/// </summary>
public const byte FT_BIT_MODE_RESET = 0x00;
/// <summary>
/// Asynchronous bit-bang mode
/// </summary>
public const byte FT_BIT_MODE_ASYNC_BITBANG = 0x01;
/// <summary>
/// MPSSE bit mode - only available on FT2232, FT2232H, FT4232H and FT232H
/// </summary>
public const byte FT_BIT_MODE_MPSSE = 0x02;
/// <summary>
/// Synchronous bit-bang mode
/// </summary>
public const byte FT_BIT_MODE_SYNC_BITBANG = 0x04;
/// <summary>
/// MCU host bus emulation mode - only available on FT2232, FT2232H, FT4232H and FT232H
/// </summary>
public const byte FT_BIT_MODE_MCU_HOST = 0x08;
/// <summary>
/// Fast opto-isolated serial mode - only available on FT2232, FT2232H, FT4232H and FT232H
/// </summary>
public const byte FT_BIT_MODE_FAST_SERIAL = 0x10;
/// <summary>
/// CBUS bit-bang mode - only available on FT232R and FT232H
/// </summary>
public const byte FT_BIT_MODE_CBUS_BITBANG = 0x20;
/// <summary>
/// Single channel synchronous 245 FIFO mode - only available on FT2232H channel A and FT232H
/// </summary>
public const byte FT_BIT_MODE_SYNC_FIFO = 0x40;
}
// FT232R CBUS Options
/// <summary>
/// Available functions for the FT232R CBUS pins. Controlled by FT232R EEPROM settings
/// </summary>
public class FT_CBUS_OPTIONS
{
/// <summary>
/// FT232R CBUS EEPROM options - Tx Data Enable
/// </summary>
public const byte FT_CBUS_TXDEN = 0x00;
/// <summary>
/// FT232R CBUS EEPROM options - Power On
/// </summary>
public const byte FT_CBUS_PWRON = 0x01;
/// <summary>
/// FT232R CBUS EEPROM options - Rx LED
/// </summary>
public const byte FT_CBUS_RXLED = 0x02;
/// <summary>
/// FT232R CBUS EEPROM options - Tx LED
/// </summary>
public const byte FT_CBUS_TXLED = 0x03;
/// <summary>
/// FT232R CBUS EEPROM options - Tx and Rx LED
/// </summary>
public const byte FT_CBUS_TXRXLED = 0x04;
/// <summary>
/// FT232R CBUS EEPROM options - Sleep
/// </summary>
public const byte FT_CBUS_SLEEP = 0x05;
/// <summary>
/// FT232R CBUS EEPROM options - 48MHz clock
/// </summary>
public const byte FT_CBUS_CLK48 = 0x06;
/// <summary>
/// FT232R CBUS EEPROM options - 24MHz clock
/// </summary>
public const byte FT_CBUS_CLK24 = 0x07;
/// <summary>
/// FT232R CBUS EEPROM options - 12MHz clock
/// </summary>
public const byte FT_CBUS_CLK12 = 0x08;
/// <summary>
/// FT232R CBUS EEPROM options - 6MHz clock
/// </summary>
public const byte FT_CBUS_CLK6 = 0x09;
/// <summary>
/// FT232R CBUS EEPROM options - IO mode
/// </summary>
public const byte FT_CBUS_IOMODE = 0x0A;
/// <summary>
/// FT232R CBUS EEPROM options - Bit-bang write strobe
/// </summary>
public const byte FT_CBUS_BITBANG_WR = 0x0B;
/// <summary>
/// FT232R CBUS EEPROM options - Bit-bang read strobe
/// </summary>
public const byte FT_CBUS_BITBANG_RD = 0x0C;
}
// FT232H CBUS Options
/// <summary>
/// Available functions for the FT232H CBUS pins. Controlled by FT232H EEPROM settings
/// </summary>
public class FT_232H_CBUS_OPTIONS
{
/// <summary>
/// FT232H CBUS EEPROM options - Tristate
/// </summary>
public const byte FT_CBUS_TRISTATE = 0x00;
/// <summary>
/// FT232H CBUS EEPROM options - Tx LED
/// </summary>
public const byte FT_CBUS_TXLED = 0x01;
/// <summary>
/// FT232H CBUS EEPROM options - Rx LED
/// </summary>
public const byte FT_CBUS_RXLED = 0x02;
/// <summary>
/// FT232H CBUS EEPROM options - Tx and Rx LED
/// </summary>
public const byte FT_CBUS_TXRXLED = 0x03;
/// <summary>
/// FT232H CBUS EEPROM options - Power Enable#
/// </summary>
public const byte FT_CBUS_PWREN = 0x04;
/// <summary>
/// FT232H CBUS EEPROM options - Sleep
/// </summary>
public const byte FT_CBUS_SLEEP = 0x05;
/// <summary>
/// FT232H CBUS EEPROM options - Drive pin to logic 0
/// </summary>
public const byte FT_CBUS_DRIVE_0 = 0x06;
/// <summary>
/// FT232H CBUS EEPROM options - Drive pin to logic 1
/// </summary>
public const byte FT_CBUS_DRIVE_1 = 0x07;
/// <summary>
/// FT232H CBUS EEPROM options - IO Mode
/// </summary>
public const byte FT_CBUS_IOMODE = 0x08;
/// <summary>
/// FT232H CBUS EEPROM options - Tx Data Enable
/// </summary>
public const byte FT_CBUS_TXDEN = 0x09;
/// <summary>
/// FT232H CBUS EEPROM options - 30MHz clock
/// </summary>
public const byte FT_CBUS_CLK30 = 0x0A;
/// <summary>
/// FT232H CBUS EEPROM options - 15MHz clock
/// </summary>
public const byte FT_CBUS_CLK15 = 0x0B;/// <summary>
/// FT232H CBUS EEPROM options - 7.5MHz clock
/// </summary>
public const byte FT_CBUS_CLK7_5 = 0x0C;
}
// Flag values for FT_GetDeviceInfoDetail and FT_GetDeviceInfo
/// <summary>
/// Flags that provide information on the FTDI device state
/// </summary>
public class FT_FLAGS
{
/// <summary>
/// Indicates that the device is open
/// </summary>
public const UInt32 FT_FLAGS_OPENED = 0x00000001;
/// <summary>
/// Indicates that the device is enumerated as a hi-speed USB device
/// </summary>
public const UInt32 FT_FLAGS_HISPEED = 0x00000002;
}
// Valid drive current values for FT2232H, FT4232H and FT232H devices
/// <summary>
/// Valid values for drive current options on FT2232H, FT4232H and FT232H devices.
/// </summary>
public class FT_DRIVE_CURRENT
{
/// <summary>
/// 4mA drive current
/// </summary>
public const byte FT_DRIVE_CURRENT_4MA = 4;
/// <summary>
/// 8mA drive current
/// </summary>
public const byte FT_DRIVE_CURRENT_8MA = 8;
/// <summary>
/// 12mA drive current
/// </summary>
public const byte FT_DRIVE_CURRENT_12MA = 12;
/// <summary>
/// 16mA drive current
/// </summary>
public const byte FT_DRIVE_CURRENT_16MA = 16;
}
// Device type identifiers for FT_GetDeviceInfoDetail and FT_GetDeviceInfo
/// <summary>
/// List of FTDI device types
/// </summary>
public enum FT_DEVICE
{
/// <summary>
/// FT232B or FT245B device
/// </summary>
FT_DEVICE_BM = 0,
/// <summary>
/// FT8U232AM or FT8U245AM device
/// </summary>
FT_DEVICE_AM,
/// <summary>
/// FT8U100AX device
/// </summary>
FT_DEVICE_100AX,
/// <summary>
/// Unknown device
/// </summary>
FT_DEVICE_UNKNOWN,
/// <summary>
/// FT2232 device
/// </summary>
FT_DEVICE_2232,
/// <summary>
/// FT232R or FT245R device
/// </summary>
FT_DEVICE_232R,
/// <summary>
/// FT2232H device
/// </summary>
FT_DEVICE_2232H,
/// <summary>
/// FT4232H device
/// </summary>
FT_DEVICE_4232H,
/// <summary>
/// FT232H device
/// </summary>
FT_DEVICE_232H
};
// Default values
private const UInt32 FT_DEFAULT_BAUD_RATE = 9600;
private const UInt32 FT_DEFAULT_DEADMAN_TIMEOUT = 5000;
private const Int32 FT_COM_PORT_NOT_ASSIGNED = -1;
private const UInt32 FT_DEFAULT_IN_TRANSFER_SIZE = 0x1000;
private const UInt32 FT_DEFAULT_OUT_TRANSFER_SIZE = 0x1000;
private const byte FT_DEFAULT_LATENCY = 16;
private const UInt32 FT_DEFAULT_DEVICE_ID = 0x04036001;
//**************************************************************************
//
// VARIABLES
//
//**************************************************************************
// Create private variables for the device within the class
private IntPtr ftHandle = IntPtr.Zero;
//**************************************************************************
//
// TYPE DEFINITIONS
//
//**************************************************************************
/// <summary>
/// Type that holds device information for GetDeviceInformation method.
/// Used with FT_GetDeviceInfo and FT_GetDeviceInfoDetail in FTD2XX.DLL
/// </summary>
public class FT_DEVICE_INFO_NODE
{
/// <summary>
/// Indicates device state. Can be any combination of the following: FT_FLAGS_OPENED, FT_FLAGS_HISPEED
/// </summary>
public UInt32 Flags;
/// <summary>
/// Indicates the device type. Can be one of the following: FT_DEVICE_232H, FT_DEVICE_4232H, FT_DEVICE_2232H,
/// FT_DEVICE_232R, FT_DEVICE_2232C, FT_DEVICE_BM, FT_DEVICE_AM, FT_DEVICE_100AX or FT_DEVICE_UNKNOWN
/// </summary>
public FT_DEVICE Type;
/// <summary>
/// The Vendor ID and Product ID of the device
/// </summary>
public UInt32 ID;
/// <summary>
/// The physical location identifier of the device
/// </summary>
public UInt32 LocId;
/// <summary>
/// The device serial number
/// </summary>
public string SerialNumber;
/// <summary>
/// The device description
/// </summary>
public string Description;
/// <summary>
/// The device handle. This value is not used externally and is provided for information only.
/// If the device is not open, this value is 0.
/// </summary>
public IntPtr ftHandle;
}
// Internal structure for reading and writing EEPROM contents
// NOTE: NEED Pack=1 for byte alignment! Without this, data is garbage
[StructLayout(LayoutKind.Sequential, Pack = 1)]
private class FT_PROGRAM_DATA
{
public UInt32 Signature1;
public UInt32 Signature2;
public UInt32 Version;
public UInt16 VendorID;
public UInt16 ProductID;
public IntPtr Manufacturer;
public IntPtr ManufacturerID;
public IntPtr Description;
public IntPtr SerialNumber;
public UInt16 MaxPower;
public UInt16 PnP;
public UInt16 SelfPowered;
public UInt16 RemoteWakeup;
// FT232B extensions
public byte Rev4;
public byte IsoIn;
public byte IsoOut;
public byte PullDownEnable;
public byte SerNumEnable;
public byte USBVersionEnable;
public UInt16 USBVersion;
// FT2232D extensions
public byte Rev5;
public byte IsoInA;
public byte IsoInB;
public byte IsoOutA;
public byte IsoOutB;
public byte PullDownEnable5;
public byte SerNumEnable5;
public byte USBVersionEnable5;
public UInt16 USBVersion5;
public byte AIsHighCurrent;
public byte BIsHighCurrent;
public byte IFAIsFifo;
public byte IFAIsFifoTar;
public byte IFAIsFastSer;
public byte AIsVCP;
public byte IFBIsFifo;
public byte IFBIsFifoTar;
public byte IFBIsFastSer;
public byte BIsVCP;
// FT232R extensions
public byte UseExtOsc;
public byte HighDriveIOs;
public byte EndpointSize;
public byte PullDownEnableR;
public byte SerNumEnableR;
public byte InvertTXD; // non-zero if invert TXD
public byte InvertRXD; // non-zero if invert RXD
public byte InvertRTS; // non-zero if invert RTS
public byte InvertCTS; // non-zero if invert CTS
public byte InvertDTR; // non-zero if invert DTR
public byte InvertDSR; // non-zero if invert DSR
public byte InvertDCD; // non-zero if invert DCD
public byte InvertRI; // non-zero if invert RI
public byte Cbus0; // Cbus Mux control - Ignored for FT245R
public byte Cbus1; // Cbus Mux control - Ignored for FT245R
public byte Cbus2; // Cbus Mux control - Ignored for FT245R
public byte Cbus3; // Cbus Mux control - Ignored for FT245R
public byte Cbus4; // Cbus Mux control - Ignored for FT245R
public byte RIsD2XX; // Default to loading VCP
// FT2232H extensions
public byte PullDownEnable7;
public byte SerNumEnable7;
public byte ALSlowSlew; // non-zero if AL pins have slow slew
public byte ALSchmittInput; // non-zero if AL pins are Schmitt input
public byte ALDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA
public byte AHSlowSlew; // non-zero if AH pins have slow slew
public byte AHSchmittInput; // non-zero if AH pins are Schmitt input
public byte AHDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA
public byte BLSlowSlew; // non-zero if BL pins have slow slew
public byte BLSchmittInput; // non-zero if BL pins are Schmitt input
public byte BLDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA
public byte BHSlowSlew; // non-zero if BH pins have slow slew
public byte BHSchmittInput; // non-zero if BH pins are Schmitt input
public byte BHDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA
public byte IFAIsFifo7; // non-zero if interface is 245 FIFO
public byte IFAIsFifoTar7; // non-zero if interface is 245 FIFO CPU target
public byte IFAIsFastSer7; // non-zero if interface is Fast serial
public byte AIsVCP7; // non-zero if interface is to use VCP drivers
public byte IFBIsFifo7; // non-zero if interface is 245 FIFO
public byte IFBIsFifoTar7; // non-zero if interface is 245 FIFO CPU target
public byte IFBIsFastSer7; // non-zero if interface is Fast serial
public byte BIsVCP7; // non-zero if interface is to use VCP drivers
public byte PowerSaveEnable; // non-zero if using BCBUS7 to save power for self-powered designs
// FT4232H extensions
public byte PullDownEnable8;
public byte SerNumEnable8;
public byte ASlowSlew; // non-zero if AL pins have slow slew
public byte ASchmittInput; // non-zero if AL pins are Schmitt input
public byte ADriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA
public byte BSlowSlew; // non-zero if AH pins have slow slew
public byte BSchmittInput; // non-zero if AH pins are Schmitt input
public byte BDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA
public byte CSlowSlew; // non-zero if BL pins have slow slew
public byte CSchmittInput; // non-zero if BL pins are Schmitt input
public byte CDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA
public byte DSlowSlew; // non-zero if BH pins have slow slew
public byte DSchmittInput; // non-zero if BH pins are Schmitt input
public byte DDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA
public byte ARIIsTXDEN;
public byte BRIIsTXDEN;
public byte CRIIsTXDEN;
public byte DRIIsTXDEN;
public byte AIsVCP8; // non-zero if interface is to use VCP drivers
public byte BIsVCP8; // non-zero if interface is to use VCP drivers
public byte CIsVCP8; // non-zero if interface is to use VCP drivers
public byte DIsVCP8; // non-zero if interface is to use VCP drivers
// FT232H extensions
public byte PullDownEnableH; // non-zero if pull down enabled
public byte SerNumEnableH; // non-zero if serial number to be used
public byte ACSlowSlewH; // non-zero if AC pins have slow slew
public byte ACSchmittInputH; // non-zero if AC pins are Schmitt input
public byte ACDriveCurrentH; // valid values are 4mA, 8mA, 12mA, 16mA
public byte ADSlowSlewH; // non-zero if AD pins have slow slew
public byte ADSchmittInputH; // non-zero if AD pins are Schmitt input
public byte ADDriveCurrentH; // valid values are 4mA, 8mA, 12mA, 16mA
public byte Cbus0H; // Cbus Mux control
public byte Cbus1H; // Cbus Mux control
public byte Cbus2H; // Cbus Mux control
public byte Cbus3H; // Cbus Mux control
public byte Cbus4H; // Cbus Mux control
public byte Cbus5H; // Cbus Mux control
public byte Cbus6H; // Cbus Mux control
public byte Cbus7H; // Cbus Mux control
public byte Cbus8H; // Cbus Mux control
public byte Cbus9H; // Cbus Mux control
public byte IsFifoH; // non-zero if interface is 245 FIFO
public byte IsFifoTarH; // non-zero if interface is 245 FIFO CPU target
public byte IsFastSerH; // non-zero if interface is Fast serial
public byte IsFT1248H; // non-zero if interface is FT1248
public byte FT1248CpolH; // FT1248 clock polarity
public byte FT1248LsbH; // FT1248 data is LSB (1) or MSB (0)
public byte FT1248FlowControlH; // FT1248 flow control enable
public byte IsVCPH; // non-zero if interface is to use VCP drivers
public byte PowerSaveEnableH; // non-zero if using ACBUS7 to save power for self-powered designs
}
// Base class for EEPROM structures - these elements are common to all devices
/// <summary>
/// Common EEPROM elements for all devices. Inherited to specific device type EEPROMs.
/// </summary>
public class FT_EEPROM_DATA
{
//private const UInt32 Signature1 = 0x00000000;
//private const UInt32 Signature2 = 0xFFFFFFFF;
//private const UInt32 Version = 0x00000002;
/// <summary>
/// Vendor ID as supplied by the USB Implementers Forum
/// </summary>
public UInt16 VendorID = 0x0403;
/// <summary>
/// Product ID
/// </summary>
public UInt16 ProductID = 0x6001;
/// <summary>
/// Manufacturer name string
/// </summary>
public string Manufacturer = "FTDI";
/// <summary>
/// Manufacturer name abbreviation to be used as a prefix for automatically generated serial numbers
/// </summary>
public string ManufacturerID = "FT";
/// <summary>
/// Device description string