ftd2xx.ftd2xx

Module for accessing functions from FTD2XX in an easier to use _pythonic_ way. For full documentation please refer to the FTDI Programming Guide. This module is based on Pablo Bleyers d2xx module, except this uses ctypes instead of an extension approach.

Module Contents

Classes

FTD2XX

Class for communicating with an FTDI device

Functions

call_ft(function, *args)

Call an FTDI function and check the status. Raise exception on error

listDevices([flags])

Return a list of serial numbers(default), descriptions or

getLibraryVersion()

Return a long representing library version

createDeviceInfoList()

Create the internal device info list and return number of entries

getDeviceInfoDetail([devnum, update])

Get an entry from the internal device info list. Set update to

open([dev, update])

Open a handle to a usb device by index and return an FTD2XX instance for

openEx(id_str[, flags, update])

Open a handle to a usb device by serial number(default), description or

Attributes

ftd2xx.ftd2xx.ft_program_data
exception ftd2xx.ftd2xx.DeviceError(message)

Bases: Exception

Exception class for status messages

Initialize self. See help(type(self)) for accurate signature.

Parameters:

message (int | Any)

__str__()

Return str(self).

__reduce__()

Helper for pickle.

ftd2xx.ftd2xx.call_ft(function, *args)

Call an FTDI function and check the status. Raise exception on error

Parameters:

function (Callable)

ftd2xx.ftd2xx.listDevices(flags=0)

Return a list of serial numbers(default), descriptions or locations (Windows only) of the connected FTDI devices depending on value of flags

Parameters:

flags (int)

Return type:

list[bytes] | None

ftd2xx.ftd2xx.getLibraryVersion()

Return a long representing library version

Return type:

int

ftd2xx.ftd2xx.createDeviceInfoList()

Create the internal device info list and return number of entries

Return type:

int

ftd2xx.ftd2xx.getDeviceInfoDetail(devnum=0, update=True)

Get an entry from the internal device info list. Set update to False to avoid a slow call to createDeviceInfoList.

Parameters:
  • devnum (int)

  • update (bool)

Return type:

DeviceInfoDetail

ftd2xx.ftd2xx.open(dev=0, update=True)

Open a handle to a usb device by index and return an FTD2XX instance for it. Set update to False to avoid a slow call to createDeviceInfoList.

Parameters:
  • dev (int) – The device number to open.

  • update (bool) – Set to False to disable automatic call to createDeviceInfoList.

Raises:

DeviceError – If the device cannot be opened.

Returns:

An instance of the FTD2XX class if successful. Use it as a context manager.

Return type:

FTD2XX

Example

with open(0, False) as dev:

dev.write(b”Hello World”)

ftd2xx.ftd2xx.openEx(id_str, flags=defines.OPEN_BY_SERIAL_NUMBER, update=True)

Open a handle to a usb device by serial number(default), description or location(Windows only) depending on value of flags and return an FTD2XX instance for it. Set update to False to avoid a slow call to createDeviceInfoList.

Parameters:
  • id_str (bytes) – The ID string from listDevices.

  • flags (int) – Flag (consult D2XX Guide). Defaults to OPEN_BY_SERIAL_NUMBER.

  • update (bool) – Set to False to disable automatic call to createDeviceInfoList.

Raises:

DeviceError – If the device cannot be opened.

Returns:

An instance of the FTD2XX class if successful. Use it as a context manager.

Return type:

FTD2XX

Example

with openEx(b”MyDevice”, update=False) as dev:

dev.write(b”Hello World”)

class ftd2xx.ftd2xx.FTD2XX(handle, update=True)

Bases: ContextManager[FTD2XX]

Class for communicating with an FTDI device

Use open or openEx to create an instance of this class.

Create an instance of the FTD2XX class with the given device handle and populate the device info in the instance dictionary.

Parameters:
  • update (bool) – Set False to disable automatic (slow) call to createDeviceInfoList

  • handle (ftd2xx._ftd2xx.FT_HANDLE)

handle: ftd2xx._ftd2xx.FT_HANDLE
status: int
close()

Close the device handle

Return type:

None

read(nchars, raw=True)

Read up to nchars bytes of data from the device. Can return fewer if timedout. Use getQueueStatus to find how many bytes are available

Parameters:
  • nchars (int)

  • raw (bool)

Return type:

bytes

write(data)

Send the data to the device. Data must be a string representing the bytes to be sent

Parameters:

data (bytes)

abstract ioctl()

Not implemented

setBaudRate(baud)

Set the baud rate

Parameters:

baud (int)

Return type:

None

setDivisor(div)

Set the clock divider. The clock will be set to 6e6/(div + 1).

Parameters:

div (int)

setDataCharacteristics(wordlen, stopbits, parity)

Set the data characteristics for UART

Parameters:
  • wordlen (int)

  • stopbits (int)

  • parity (int)

setFlowControl(flowcontrol, xon=-1, xoff=-1)

Set the flow control for UART

Parameters:
  • flowcontrol (int)

  • xon (int)

  • xoff (int)

resetDevice()

Reset the device

setDtr()

Set the DTR (Data Terminal Ready) signal of the FTDI device.

clrDtr()

Clear the DTR signal of the FTDI device.

setRts()

Set the RTS (Request To Send) signal of the FTDI device.

clrRts()

Clear the RTS signal of the FTDI device.

getModemStatus()

Get the modem status of the FTDI device.

Return type:

ftd2xx.defines.ModemStatus

setChars(evch, evch_en, erch, erch_en)

Set the event and error characters for UART

Parameters:
  • evch (int)

  • evch_en (int)

  • erch (int)

  • erch_en (int)

purge(mask=0)

Purge the receive and/or transmit buffers

Parameters:

mask (int)

setTimeouts(read, write)

Set the read and write timeouts in milliseconds

Parameters:
  • read (int)

  • write (int)

setDeadmanTimeout(timeout)

Set the deadman timeout in milliseconds

Parameters:

timeout (int)

getQueueStatus()

Get number of bytes in receive queue.

Return type:

int

setEventNotification(evtmask, evthandle)
Parameters:

evtmask (int)

getStatus()

Return a 3-tuple of rx queue bytes, tx queue bytes and event status

setBreakOn()
setBreakOff()
setWaitMask(mask)
Parameters:

mask (int)

waitOnMask()
getEventStatus()
setLatencyTimer(latency)
Parameters:

latency (int)

getLatencyTimer()
Return type:

int

setBitMode(mask, enable)
Parameters:
  • mask (int)

  • enable (int)

getBitMode()
Return type:

int

setUSBParameters(in_tx_size, out_tx_size=0)

Set the USB request transfer sizes

Parameters:
  • in_tx_size (int)

  • out_tx_size (int)

getDeviceInfo()

Returns a dictionary describing the device.

Return type:

DeviceInfo

stopInTask()
restartInTask()
setRestPipeRetryCount(count)
resetPort()
cyclePort()
getDriverVersion()
Return type:

int

getComPortNumber()

Return a long representing the COM port number

Return type:

int

eeProgram(progdata=None, **kwds)

Program the EEPROM with custom data. If SerialNumber is null, a new serial number is generated from ManufacturerId

Parameters:

progdata (ftd2xx._ftd2xx.ft_program_data | None)

Return type:

None

eeRead()

Get the program information from the EEPROM

Return type:

ftd2xx._ftd2xx.ft_program_data

eeUASize()

Get the EEPROM user area size

Return type:

int

eeUAWrite(data)

Write data to the EEPROM user area. data must be a bytes object with appropriate byte values

Parameters:

data (bytes)

Return type:

None

eeUARead(b_to_read)

Read b_to_read bytes from the EEPROM user area

Parameters:

b_to_read (int)

Return type:

bytes

__exit__(__exc_type, __exc_value, __traceback)

Close the device when exiting the context manager

Parameters:
  • __exc_type (type[BaseException] | None)

  • __exc_value (BaseException | None)

  • __traceback (types.TracebackType | None)

Return type:

bool | None