def w32CreateFile(name, access=GENERIC_READ | GENERIC_WRITE, flags=OPEN_BY_SERIAL_NUMBER): return FTD2XX( _ft.FT_W32_CreateFile(_ft.STRING(name), _ft.DWORD(access), _ft.DWORD(0), None, _ft.DWORD(OPEN_EXISTING), _ft.DWORD(flags), _ft.HANDLE(0)))
def getStatus(self): """Return a 3-tuple of rx queue bytes, tx queue bytes and event status""" rxQAmount = _ft.DWORD() txQAmount = _ft.DWORD() evtStatus = _ft.DWORD() call_ft(_ft.FT_GetStatus, self.handle, c.byref(rxQAmount), c.byref(txQAmount), c.byref(evtStatus)) return (rxQAmount.value, txQAmount.value, evtStatus.value)
def getDeviceInfo(self): """Returns a dictionary describing the device. """ deviceType = _ft.DWORD() deviceId = _ft.DWORD() desc = c.c_buffer(MAX_DESCRIPTION_SIZE) serial = c.c_buffer(MAX_DESCRIPTION_SIZE) call_ft(_ft.FT_GetDeviceInfo, self.handle, c.byref(deviceType), c.byref(deviceId), serial, desc, None) return { 'type': deviceType.value, 'id': deviceId.value, 'description': desc.value, 'serial': serial.value }
def read(self, 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""" b_read = _ft.DWORD() b = c.c_buffer(nchars) call_ft(_ft.FT_Read, self.handle, b, nchars, c.byref(b_read)) return b.raw[:b_read.value] if raw else b.value[:b_read.value]
def openEx(id_str, flags=OPEN_BY_SERIAL_NUMBER): """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""" h = _ft.FT_HANDLE() call_ft(_ft.FT_OpenEx, id_str, _ft.DWORD(flags), c.byref(h)) return FTD2XX(h)
def eeUARead(self, b_to_read): """Read b_to_read bytes from the EEPROM user area""" b_read = _ft.DWORD() buf = c.c_buffer(b_to_read) call_ft(_ft.FT_EE_UARead, self.handle, c.cast(buf, _ft.PUCHAR), b_to_read, c.byref(b_read)) return buf.value[:b_read.value]
def eeProgram(self, progdata=None, *args, **kwds): """Program the EEPROM with custom data. If SerialNumber is null, a new serial number is generated from ManufacturerId""" if progdata is None: progdata = _ft.ft_program_data(**kwds) ## if self.devInfo['type'] == 4: ## version = 1 ## elif self.devInfo['type'] == 5: ## version = 2 ## else: ## version = 0 progdata.Signature1 = _ft.DWORD(0) progdata.Signature2 = _ft.DWORD(0xffffffff) progdata.Version = _ft.DWORD(2) call_ft(_ft.FT_EE_Program, self.handle, progdata) return None
def 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""" n = _ft.DWORD() call_ft(_ft.FT_ListDevices, c.byref(n), None, _ft.DWORD(LIST_NUMBER_ONLY)) devcount = n.value if devcount: # since ctypes has no pointer arithmetic. bd = [c.c_buffer(MAX_DESCRIPTION_SIZE) for i in range(devcount)] +\ [None] # array of pointers to those strings, initially all NULL ba = (c.c_char_p * (devcount + 1))() for i in range(devcount): ba[i] = c.cast(bd[i], c.c_char_p) call_ft(_ft.FT_ListDevices, ba, c.byref(n), _ft.DWORD(LIST_ALL | flags)) return [res for res in ba[:devcount]] else: return None
def 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.""" f = _ft.DWORD() t = _ft.DWORD() i = _ft.DWORD() l = _ft.DWORD() h = _ft.FT_HANDLE() n = c.c_buffer(MAX_DESCRIPTION_SIZE) d = c.c_buffer(MAX_DESCRIPTION_SIZE) # createDeviceInfoList is slow, only run if update is True if update: createDeviceInfoList() call_ft(_ft.FT_GetDeviceInfoDetail, _ft.DWORD(devnum), c.byref(f), c.byref(t), c.byref(i), c.byref(l), n, d, c.byref(h)) return { 'index': devnum, 'flags': f.value, 'type': t.value, 'id': i.value, 'location': l.value, 'serial': n.value, 'description': d.value, 'handle': h }
def setWaitMask(self, mask): call_ft(_ft.FT_SetWaitMask, self.handle, _ft.DWORD(mask)) return None
def getModemStatus(self): m = _ft.DWORD() call_ft(_ft.FT_GetModemStatus, self.handle, c.byref(m)) return m.value
def eeUASize(self): """Get the EEPROM user area size""" uasize = _ft.DWORD() call_ft(_ft.FT_EE_UASize, self.handle, c.byref(uasize)) return uasize.value
def setRestPipeRetryCount(self, count): call_ft(_ft.FT_SetResetPipeRetryCount, self.handle, _ft.DWORD(count)) return None
def getDriverVersion(self): drvver = _ft.DWORD() call_ft(_ft.FT_GetDriverVersion, self.handle, c.byref(drvver)) return drvver.value
def setBaudRate(self, baud): """Set the baud rate""" call_ft(_ft.FT_SetBaudRate, self.handle, _ft.DWORD(baud)) return None
def getVIDPID(): """Linux only. Get the VID and PID of the device""" vid = _ft.DWORD() pid = _ft.DWORD() call_ft(_ft.FT_GetVIDPID, c.byref(vid), c.byref(pid)) return (vid.value, pid.value)
def purge(self, mask=0): if not mask: mask = PURGE_RX | PURGE_TX call_ft(_ft.FT_Purge, self.handle, _ft.DWORD(mask)) return None
def createDeviceInfoList(): """Create the internal device info list and return number of entries""" m = _ft.DWORD() call_ft(_ft.FT_CreateDeviceInfoList, c.byref(m)) return m.value
def setVIDPID(vid, pid): """Linux only. Set the VID and PID of the device""" call_ft(_ft.FT_SetVIDPID, _ft.DWORD(vid), _ft.DWORD(pid)) return None
def setEventNotification(self, evtmask, evthandle): call_ft(_ft.FT_SetEventNotification, self.handle, _ft.DWORD(evtmask), _ft.HANDLE(evthandle)) return None
def getQueueStatus(self): """Get number of bytes in receive queue.""" rxQAmount = _ft.DWORD() call_ft(_ft.FT_GetQueueStatus, self.handle, c.byref(rxQAmount)) return rxQAmount.value
def setDeadmanTimeout(self, timeout): call_ft(_ft.FT_SetDeadmanTimeout, self.handle, _ft.DWORD(timeout)) return None
def setTimeouts(self, read, write): call_ft(_ft.FT_SetTimeouts, self.handle, _ft.DWORD(read), _ft.DWORD(write)) return None
def getLibraryVersion(): """Return a long representing library version""" m = _ft.DWORD() call_ft(_ft.FT_GetLibraryVersion, c.byref(m)) return m.value
def waitOnMask(self): mask = _ft.DWORD() call_ft(_ft.FT_WaitOnMask, self.handle, c.byref(mask)) return mask.value
def getEventStatus(self): evtStatus = _ft.DWORD() call_ft(_ft.FT_GetEventStatus, self.handle, c.byref(evtStatus)) return evtStatus.value
def write(self, data): """Send the data to the device. Data must be a string representing the bytes to be sent""" w = _ft.DWORD() call_ft(_ft.FT_Write, self.handle, data, len(data), c.byref(w)) return w.value