1 // FTDI class, header file
3 #define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
15 FTDI() : handle(NULL), status(FT_OK) {}
17 FT_STATUS list_devices(VOID * arg1, VOID * arg2, DWORD flags) {
18 status = FT_ListDevices(arg1, arg2, flags);
21 FT_STATUS open(int index) {
22 status = FT_Open(index, &handle);
25 FT_STATUS open_ex(); //OpenEx(PVOID, DWORD);
27 status = FT_Close(handle);
31 bool get_device_info(FT_DEVICE * type, DWORD * id, char * SerialNumber, char * Description) {
32 status = FT_GetDeviceInfo(handle, type, id, SerialNumber, Description, NULL);
33 return (status == FT_OK);
35 inline int read(void * buffer, unsigned length) {
37 status = FT_Read(handle, buffer, length, &done_w);
42 inline int write(const void * buffer, unsigned length) {
44 status = FT_Write(handle, (void *)buffer, length, &done_w);
49 #if 0 // obsolete versions
50 inline FT_STATUS read_old(void * buffer, unsigned len, unsigned & done) {
52 status = FT_Read(handle, buffer, len, &done_w);
56 inline FT_STATUS write_old(const void * buffer, unsigned len, unsigned & done) {
58 status = FT_Write(handle, (void *)buffer, len, &done_w);
63 FT_STATUS reset_device() {
64 status = FT_ResetDevice(handle);
67 FT_STATUS reset_port() {
68 status = FT_ResetPort(handle);
71 FT_STATUS cycle_port() {
72 status = FT_CyclePort(handle);
75 FT_STATUS purge(ULONG mode) {
76 status = FT_Purge(handle, mode);
79 UCHAR get_bit_mode() {
81 status = FT_GetBitMode(handle, &byte);
84 bool set_bit_mode(UCHAR mask, UCHAR mode) {
85 status = FT_SetBitMode(handle, mask, mode);
86 return (status == FT_OK);
88 // Rx, Tx timeout duration in ms
89 FT_STATUS set_timeouts(ULONG rx, ULONG tx) {
90 status = FT_SetTimeouts(handle, rx, tx);
93 DWORD get_queue_status() { // get number of bytes in rx queue
95 status = FT_GetQueueStatus(handle, &rx_bytes);
98 FT_STATUS get_status(DWORD * RxBytes, DWORD * TxBytes, DWORD * Event) {
99 status = FT_GetStatus(handle, RxBytes, TxBytes, Event);
102 bool set_baud_rate(unsigned long baud_rate) {
103 status = FT_SetBaudRate(handle, baud_rate);
104 return (status == FT_OK);
106 FT_STATUS set_divisor(USHORT divisor) {
107 status = FT_SetDivisor(handle, divisor);
110 FT_STATUS set_latency_timer(UCHAR latency) {
111 status = FT_SetLatencyTimer(handle, latency);
114 UCHAR get_latency_timer() {
116 status = FT_GetLatencyTimer(handle, &latency);
121 DWORD get_library_version() {
123 status = FT_GetLibraryVersion(&version);
128 DWORD get_driver_version() {
130 status = FT_GetDriverVersion(handle, &version);
135 FT_STATUS set_usb_parameters(ULONG InTransferSize, ULONG OutTransferSize) {
136 status = FT_SetUSBParameters(handle, InTransferSize, OutTransferSize);
139 FT_STATUS set_event_notification(DWORD EventMask, PVOID hEvent) {
140 status = FT_SetEventNotification(handle, EventMask, hEvent);
143 FT_STATUS set_reset_pipe_retry_count(DWORD count) {
144 status = FT_SetResetPipeRetryCount(handle, count);
147 FT_STATUS set_flow_control(USHORT usFlowControl, UCHAR uXon, UCHAR uXoff) {
148 status = FT_SetFlowControl(handle, usFlowControl, uXon, uXoff);
151 bool set_flow_control_off() {
152 status = FT_SetFlowControl(handle, FT_FLOW_NONE, 0, 0);
153 return (status == FT_OK);
155 bool set_flow_control_RTS_CTS() {
156 status = FT_SetFlowControl(handle, FT_FLOW_RTS_CTS, 0, 0);
157 return (status == FT_OK);
159 FT_STATUS set_data_characteristics(UCHAR uWordLength, UCHAR uStopBits, UCHAR uParity) {
160 status = FT_SetDataCharacteristics(handle, uWordLength, uStopBits, uParity);
163 bool set_serial(int data_bits, char parity, int stop_bits) {
166 case 7: e_data = FT_BITS_7; break;
167 case 8: e_data = FT_BITS_8; break;
168 default: return false;
172 case 1: e_stop = FT_STOP_BITS_1; break;
173 case 2: e_stop = FT_STOP_BITS_2; break;
174 default: return false;
178 case 'N': e_parity = FT_PARITY_NONE; break;
179 case 'O': e_parity = FT_PARITY_ODD; break;
180 case 'E': e_parity = FT_PARITY_EVEN; break;
181 case 'M': e_parity = FT_PARITY_MARK; break;
182 case 'S': e_parity = FT_PARITY_SPACE; break;
183 default: return false;
185 status = FT_SetDataCharacteristics(handle, e_data, e_stop, e_parity);
186 return (status == FT_OK);
188 unsigned get_modem_status() {
190 status = FT_GetModemStatus(handle, &ModemStatus);
193 bool set_dtr(bool on) {
195 status = FT_SetDtr(handle);
197 status = FT_ClrDtr(handle);
198 return (status == FT_OK);
200 bool set_rts(bool on) {
202 status = FT_SetRts(handle);
204 status = FT_ClrRts(handle);
205 return (status == FT_OK);
207 // raw EEPROM interface
208 int eeprom_read(unsigned addr) {
210 status = FT_ReadEE(handle, addr, &value);
216 bool eeprom_write(unsigned addr, unsigned short value) {
217 status = FT_WriteEE(handle, addr, value);
218 return (status == FT_OK);
220 // old EEPROM interface
221 bool eeprom_read_parse(PFT_PROGRAM_DATA pData) {
222 status = FT_EE_Read(handle, pData);
223 return (status == FT_OK);
225 bool eeprom_write(const PFT_PROGRAM_DATA pData) {
226 status = FT_EE_Program(handle, pData);
227 return (status == FT_OK);
229 // new EEPROM interface
230 bool eeprom_read(void * data, unsigned data_size, char * Manufacturer, char *ManufacturerId, char *Description, char *SerialNumber) {
231 status = FT_EEPROM_Read(handle, data, data_size, Manufacturer, ManufacturerId, Description, SerialNumber);
232 return (status == FT_OK);
234 bool eeprom_write(void * data, unsigned data_size, char * Manufacturer, char *ManufacturerId, char *Description, char *SerialNumber) {
235 status = FT_EEPROM_Program(handle, data, data_size, Manufacturer, ManufacturerId, Description, SerialNumber);
236 return (status == FT_OK);
239 int eeprom_ua_size() {
241 status = FT_EE_UASize(handle, &value);
247 int eeprom_ua_read(unsigned char * data, unsigned length) {
249 status = FT_EE_UARead(handle, data, length, &len);
255 bool eeprom_ua_write(const unsigned char * data, unsigned length) {
256 status = FT_EE_UAWrite(handle, (PUCHAR)data, length);
257 return (status == FT_OK);
262 #if 0 // these functions hang FTDI version > 2.8 on Vista/W7
263 FT_STATUS FTDI::purge_rx()
265 for(unsigned i = 0; i < 100; ++i)
266 if (FT_StopInTask(handle) == FT_OK)
269 status = FT_Purge(handle, FT_PURGE_RX);
271 for(unsigned i = 0; i < 100; ++i)
272 if (FT_RestartInTask(handle) == FT_OK)
279 status = FT_StopInTask(handle);
282 FT_STATUS restart_rx()
284 status = FT_RestartInTask(handle);
291 operator bool() const { return (status == FT_OK); }
292 // returns a short description of the status
293 const char * status_msg() const
300 case FT_INVALID_HANDLE:
301 mesg = "INVALID_HANDLE"; break;
302 case FT_DEVICE_NOT_FOUND:
303 mesg = "DEVICE_NOT_FOUND"; break;
304 case FT_DEVICE_NOT_OPENED:
305 mesg = "DEVICE_NOT_OPENED"; break;
307 mesg = "IO_ERROR"; break;
308 case FT_INSUFFICIENT_RESOURCES:
309 mesg = "INSUFFICIENT_RESOURCES"; break;
310 case FT_INVALID_PARAMETER:
311 mesg = "INVALID_PARAMETER"; break;
312 case FT_INVALID_BAUD_RATE:
313 mesg = "INVALID_BAUD_RATE"; break;
314 case FT_DEVICE_NOT_OPENED_FOR_ERASE:
315 mesg = "DEVICE_NOT_OPENED_FOR_ERASE"; break;
316 case FT_DEVICE_NOT_OPENED_FOR_WRITE:
317 mesg = "DEVICE_NOT_OPENED_FOR_WRITE"; break;
318 case FT_FAILED_TO_WRITE_DEVICE:
319 mesg = "FAILED_TO_WRITE_DEVICE"; break;
320 case FT_EEPROM_READ_FAILED:
321 mesg = "EEPROM_READ_FAILED"; break;
322 case FT_EEPROM_WRITE_FAILED:
323 mesg = "EEPROM_ERASE_FAILED"; break;
324 case FT_EEPROM_ERASE_FAILED:
325 mesg = "EEPROM_ERASE_FAILED"; break;
326 case FT_EEPROM_NOT_PRESENT:
327 mesg = "EEPROM_NOT_PRESENT"; break;
328 case FT_EEPROM_NOT_PROGRAMMED:
329 mesg = "EEPROM_NOT_PROGRAMMED"; break;
330 case FT_INVALID_ARGS:
331 mesg = "INVALID_ARGS"; break;
332 case FT_NOT_SUPPORTED:
333 mesg = "NOT_SUPPORTED"; break;
335 mesg = "OTHER_ERROR"; break;
341 static const char * device_type(FT_DEVICE type) {
343 case FT_DEVICE_232H: return "FT232H";
344 case FT_DEVICE_4232H: return "FT4232H";
345 case FT_DEVICE_2232H: return "FT2232H";
346 case FT_DEVICE_232R: return "FT232R";
347 case FT_DEVICE_2232C: return "FT2232C/L/D";
348 case FT_DEVICE_BM: return "FT232BM";
349 case FT_DEVICE_AM: return "FT8U232AM";
350 case FT_DEVICE_X_SERIES: return "FT X";
351 default: return NULL;
354 bool is_open() const {
355 return (handle != NULL);
362 FT_INVALID_HANDLE = 1
363 FT_DEVICE_NOT_FOUND = 2
364 FT_DEVICE_NOT_OPENED = 3
366 FT_INSUFFICIENT_RESOURCES = 5
367 FT_INVALID_PARAMETER = 6
368 Flags (see FT_OpenEx)
369 FT_OPEN_BY_SERIAL_NUMBER = 1
370 FT_OPEN_BY_DESCRIPTION = 2
371 Flags (see FT_ListDevices)
372 FT_LIST_NUMBER_ONLY = 0x80000000
373 FT_LIST_BY_INDEX = 0x40000000
374 FT_LIST_ALL = 0x20000000
375 Word Length (see FT_SetDataCharacteristics)
378 Stop Bits (see FT_SetDataCharacteristics)
381 Parity (see FT_SetDataCharacteristics)
387 Flow Control (see FT_SetFlowControl)
388 FT_FLOW_NONE = 0x0000
389 FT_FLOW_RTS_CTS = 0x0100
390 FT_FLOW_DTR_DSR = 0x0200
391 FT_FLOW_XON_XOFF = 0x0400
392 Purge RX and TX buffers (see FT_Purge)
397 Get information concerning the devices currently connected. This function can return
398 such information as the number of devices connected, and device strings such as serial
399 number and product description.
400 Syntax FT_STATUS FT_ListDevices ( PVOID pvArg1,PVOID pvArg2, DWORD dwFlags )
402 pvArg1 PVOID: Meaning depends on dwFlags
403 pvArg2 PVOID: Meaning depends on dwFlags
404 dwFlags DWORD: Determines format of returned information
405 Return Value FT_STATUS: FT_OK if successful, otherwise the return value is an FT error code.
407 This function can be used in a number of ways to return different types of information.
408 In its simplest form, it can be used to return the number of devices currently connected.
409 If FT_LIST_NUMBER_ONLY bit is set in dwFlags, the parameter pvArg1 is interpreted
410 as a pointer to a DWORD location to store the number of devices currently connected.
411 It can be used to return device string information. If FT_OPEN_BY_SERIAL_NUMBER
412 bit is set in dwFlags, the serial number string will be returned from this function. If
413 FT_OPEN_BY_DESCRIPTION bit is set in dwFlags, the product description string will
414 be returned from this function. If neither of these bits is set, the serial number string will
415 be returned by default.
416 It can be used to return device string information for a single device. If
417 FT_LIST_BY_INDEX bit is set in dwFlags, the parameter pvArg1 is interpreted as the
418 index of the device, and the parameter pvArg2 is interpreted as a pointer to a buffer to
419 contain the appropriate string. Indexes are zero-based, and the error code
420 FT_DEVICE_NOT_FOUND is returned for an invalid index.
421 It can be used to return device string information for all connected devices. If
422 FT_LIST_ALL bit is set in dwFlags, the parameter pvArg1 is interpreted as a pointer to an
423 array of pointers to buffers to contain the appropriate strings, and the parameter pvArg2 is
424 interpreted as a pointer to a DWORD location to store the number of devices currently
425 connected. Note that, for pvArg1, the last entry in the array of pointers to buffers should
426 be a NULL pointer so the array will contain one more location than the number of
429 Sample code shows how to get the number of devices currently connected.
432 ftStatus = FT_ListDevices(&numDevs,NULL,FT_LIST_NUMBER_ONLY);
433 if (ftStatus == FT_OK) {
434 // FT_ListDevices OK, number of devices connected is in numDevs
437 // FT_ListDevices failed
439 This sample shows how to get the serial number of the first device found. Note that
440 indexes are zero-based. If more than one device is connected, incrementing devIndex
441 will get the serial number of each connected device in turn.
446 FT_ListDevices((PVOID)devIndex,Buffer,FT_LIST_BY_INDEX|FT_OPEN_BY_SERIAL_N
448 if (FT_SUCCESS(ftStatus)) {
449 // FT_ListDevices OK, serial number is in Buffer
452 // FT_ListDevices failed
454 This sample shows how to get the product descriptions of all the devices currently
457 char *BufPtrs[3]; // pointer to array of 3 pointers
458 char Buffer1[64]; // buffer for product description of first
460 char Buffer2[64]; // buffer for product description of second
463 // initialize the array of pointers
464 BufPtrs[0] = Buffer1;
465 BufPtrs[1] = Buffer2;
466 BufPtrs[2] = NULL; // last entry should be NULL
468 FT_ListDevices(BufPtrs,&numDevs,FT_LIST_ALL|FT_OPEN_BY_DESCRIPTION);
469 if (FT_SUCCESS(ftStatus)) {
470 // FT_ListDevices OK, product descriptions are in Buffer1 and Buffer2,
472 // numDevs contains the number of devices connected
475 // FT_ListDevices failed
478 Description Open the device and return a handle which will be used for subsequent accesses.
479 Syntax FT_STATUS FT_Open ( PVOID pvDevice, FT_HANDLE *ftHandle )
481 pvDevice ULONG: Must be 0 if only one device is attached. For multiple devices 1, 2 etc.
482 ftHandle FT_HANDLE *: Pointer to a variable of type FT_HANDLE where the handle will be
483 stored. This handle must be used to access the device.
484 Return Value FT_STATUS: FT_OK if successful, otherwise the return value is an FT error code.
486 Although this function can be used to open multiple devices by setting pvDevice to 0, 1, 2
487 etc. there is no ability to open a specific device. To open named devices, use the function
490 This sample shows how to open a device.
493 ftStatus = FT_Open(0,&ftHandle);
494 if (ftStatus == FT_OK) {
495 // FT_Open OK, use ftHandle to access device
502 Open the named device and return a handle which will be used for subsequent accesses.
503 The device name can be its serial number or device description.
504 Syntax FT_STATUS FT_OpenEx ( PVOID pvArg1, DWORD dwFlags, FT_HANDLE
507 pvArg1 PVOID: Meaning depends on dwFlags, but it will normally be interpreted as a pointer to
508 a null terminated string.
509 dwFlags DWORD: FT_OPEN_BY_SERIAL_NUMBER or FT_OPEN_BY_DESCRIPTION.
510 ftHandle FT_HANDLE *: Pointer to a variable of type FT_HANDLE where the handle will be
511 stored. This handle must be used to access the device.
512 Return Value FT_STATUS: FT_OK if successful, otherwise the return value is an FT error code.
514 This function should be used to open multiple devices. Multiple devices can be opened at
515 the same time if they can be distinguished by serial number or device description.
517 These samples show how to open two devices simultaneously.
518 Suppose one device has serial number "FT000001", and the other has serial number
523 ftStatus = FT_OpenEx("FT000001",FT_OPEN_BY_SERIAL_NUMBER,&ftHandle1);
524 if (ftStatus == FT_OK) {
525 // FT_OpenEx OK, device with serial number "FT000001" is open
530 ftStatus = FT_OpenEx("FT999999",FT_OPEN_BY_SERIAL_NUMBER,&ftHandle2);
531 if (ftStatus == FT_OK) {
532 // FT_OpenEx OK, device with serial number "FT999999" is open
537 Suppose one device has description "USB HS SERIAL CONVERTER", and the other
538 has description "USB PUMP CONTROLLER".
542 ftStatus = FT_OpenEx("USB HS SERIAL
543 CONVERTER",FT_OPEN_BY_DESCRIPTION,&ftHandle1);
544 if (ftStatus == FT_OK) {
545 // FT_OpenEx OK, device with description "USB HS SERIAL CONVERTER"
551 ftStatus = FT_OpenEx("USB PUMP
552 CONTROLLER",FT_OPEN_BY_DESCRIPTION,&ftHandle2);
553 if (ftStatus == FT_OK) {
554 // FT_OpenEx OK, device with description "USB PUMP CONTROLLER" is
561 Description This function sends a reset command to the device.
562 Syntax FT_STATUS FT_ResetDevice ( FT_HANDLE ftHandle )
564 ftHandle FT_HANDLE: handle of the device to reset.
565 Return Value FT_STATUS: FT_OK if successful, otherwise the return value is an FT error code.
567 Description This function sets the baud rate for the device.
568 Syntax FT_STATUS FT_SetBaudRate ( FT_HANDLE ftHandle, DWORD dwBaudRate )
570 ftHandle FT_HANDLE: handle of the device.
571 dwBaudRate DWORD: Baud rate.
572 Return Value FT_STATUS: FT_OK if successful, otherwise the return value is an FT error code.
575 This function sets the baud rate for the device. It is used to set non-standard baud rates.
576 Syntax FT_STATUS FT_SetDivisor ( FT_HANDLE ftHandle, USHORT usDivisor )
578 ftHandle FT_HANDLE: handle of the device.
579 usDivisor USHORT: Divisor.
580 Return Value FT_STATUS: FT_OK if successful, otherwise the return value is an FT error code.
582 The application note "Setting Baud rates for the FT8U232AM", which is available on our
584 www.ftdi.co.uk , describes how to calculate the divisor for a non standard baud rate.
586 Suppose we want to set a baud rate of 5787 baud. A simple calculation suggests that a
587 divisor of 4206 should work.
588 FT_HANDLE ftHandle; // handle of device obtained from FT_Open or
591 ftStatus = FT_SetDivisor(ftHandle,0x4206);
592 if (ftStatus == FT_OK) {
593 // FT_SetDivisor OK, baud rate has been set to 5787 baud
596 // FT_SetDivisor failed
599 Description This function purges the receive and transmit buffers in the device.
600 Syntax FT_STATUS FT_Purge ( FT_HANDLE ftHandle, DWORD dwMask )
602 ftHandle FT_HANDLE: handle of the device.
603 dwMask DWORD: Any combination of FT_PURGE_RX and FT_PURGE_TX.
604 Return Value FT_STATUS: FT_OK if successful, otherwise the return value is an FT error code.
606 Description This function sets the read and write timeouts for the device.
607 Syntax FT_STATUS FT_SetTimeouts ( FT_HANDLE ftHandle, DWORD dwReadTimeout, DWORD dwWriteTimeout )
609 ftHandle FT_HANDLE: handle of the device.
610 dwReadTimeout DWORD: Read timeout in milliseconds.
611 dwWriteTimeout DWORD: Write timeout in milliseconds.
612 Return Value FT_STATUS: FT_OK if successful, otherwise the return value is an FT error code.
614 Description Gets the number of characters in the receive queue.
615 Syntax FT_STATUS FT_GetQueueStatus ( FT_HANDLE ftHandle, LPDWORD lpdwAmountInRxQueue )
617 ftHandle FT_HANDLE: handle of the device to read.
618 lpdwAmountInRxQueue LPDWORD: Pointer to a variable of type DWORD which receives the
619 number of characters in the receive queue.
620 Return Value FT_STATUS: FT_OK if successful, otherwise the return value is an FT error code.