def reset(self, bootloader=False): handle = ftdi.ftdi_context() if ftdi.ftdi_init(handle) < 0: print 'Initialization error.', print sys.exit(-1) serialnum = self.getFTDIIdFromDevPort(self.port) if subprocess.call(['rmmod', 'ftdi_sio']) != 0: print 'Close all processes that may access serial ports. You may also need to run the program as root.', print sys.exit(-1) error = ftdi.ftdi_usb_open_desc(handle, FT232R_VENDOR, FT232R_PRODUCT, None, serialnum) if error < 0: print ('Unable to open port. Error:' + str(error)), print subprocess.call(['modprobe', 'ftdi_sio']) sys.exit(-1) item0, item1 = self.guessResetDirection(handle) resetValue = item0 nBootValue = item1 if (resetValue, nBootValue) == (None, None): ftdi.ftdi_usb_close(handle) subprocess.call(['modprobe', 'ftdi_sio']) return 1 else: ftdi.ftdi_set_bitmode(handle, resetValue, ftdi.BITMODE_CBUS) time.sleep(0.5) if bootloader: ftdi.ftdi_set_bitmode(handle, nBootValue, ftdi.BITMODE_CBUS) time.sleep(0.10000000000000001) ftdi.ftdi_set_bitmode(handle, 0, ftdi.BITMODE_CBUS) ftdi.ftdi_usb_close(handle) subprocess.call(['modprobe', 'ftdi_sio']) time.sleep(1) return None
def close(self): # close channel A if bool(self.ft_ctx.usb_dev): print "Closing FTDI Interface" ftdi.ftdi_set_bitmode(self.ft_ctx, 0xfb, ftdi.BITMODE_RESET) ftdi.ftdi_usb_close(self.ft_ctx) ftdi.ftdi_deinit(self.ft_ctx)
def __del__(self): ''' Docstring Parameters ---------- Returns ------- ''' if self.port is not None: ftdi.ftdi_disable_bitbang(self.port) ftdi.ftdi_usb_close(self.port) ftdi.ftdi_deinit(self.port)
def __init__(self): # open channel A self.ft_ctx = ftdi.ftdi_context() if ftdi.ftdi_init(self.ft_ctx) < 0: raise RuntimeError("ftdi_init failed") ftdi.ftdi_set_interface(self.ft_ctx, ftdi.INTERFACE_A) if ftdi.ftdi_usb_open(self.ft_ctx, 0x0403, 0x6010) != 0: raise RuntimeError(ftdi.ftdi_get_error_string(self.ft_ctx)) try: # open and purge channel B ft_ctx_b = ftdi.ftdi_context() if ftdi.ftdi_init(ft_ctx_b) < 0: raise RuntimeError("ftdi_init failed") ftdi.ftdi_set_interface(ft_ctx_b, ftdi.INTERFACE_B) if ftdi.ftdi_usb_open(ft_ctx_b, 0x0403, 0x6010) != 0: raise RuntimeError(ftdi.ftdi_get_error_string(ft_ctx_b)) # purge buffers if ftdi.ftdi_usb_purge_buffers(ft_ctx_b) != 0: raise RuntimeError(ftdi.ftdi_get_error_string(ft_ctx_b)) # close channel B ftdi.ftdi_usb_close(ft_ctx_b) ftdi.ftdi_deinit(ft_ctx_b) except RuntimeError: self.close() raise # setup FIFO mode try: self.ft_ctx.usb_read_timeout = 5000000 self.ft_ctx.usb_write_timeout = 5000000 ftdi.ftdi_read_data_set_chunksize(self.ft_ctx, self.READ_CHUNKSIZE) ftdi.ftdi_write_data_set_chunksize(self.ft_ctx, self.WRITE_CHUNKSIZE) if ftdi.ftdi_usb_reset(self.ft_ctx) != 0: raise RuntimeError("Reset failed") time.sleep(100e-3) if ftdi.ftdi_set_bitmode(self.ft_ctx, 0, ftdi.BITMODE_SYNCFF) != 0: raise RuntimeError("Setting FIFO mode failed") if ftdi.ftdi_set_latency_timer(self.ft_ctx, 1) != 0: raise RuntimeError("Setting latency failed") except RuntimeError: self.close() raise
def __init__(self): # open channel A self.ft_ctx = ftdi.ftdi_context() if ftdi.ftdi_init(self.ft_ctx) < 0: raise RuntimeError("ftdi_init failed") ftdi.ftdi_set_interface(self.ft_ctx, ftdi.INTERFACE_A) if ftdi.ftdi_usb_open(self.ft_ctx, 0x0403, 0x6010) != 0: raise RuntimeError(ftdi.ftdi_get_error_string(self.ft_ctx)) try: # open and purge channel B ft_ctx_b = ftdi.ftdi_context() if ftdi.ftdi_init(ft_ctx_b) < 0: raise RuntimeError("ftdi_init failed") ftdi.ftdi_set_interface(ft_ctx_b, ftdi.INTERFACE_B) if ftdi.ftdi_usb_open(ft_ctx_b, 0x0403, 0x6010) != 0: raise RuntimeError(ftdi.ftdi_get_error_string(ft_ctx_b)) # purge buffers if ftdi.ftdi_usb_purge_buffers(ft_ctx_b) != 0: raise RuntimeError(ftdi.ftdi_get_error_string(ft_ctx_b)) # close channel B ftdi.ftdi_usb_close(ft_ctx_b) ftdi.ftdi_deinit(ft_ctx_b) except RuntimeError: self.close() raise # setup FIFO mode try: self.ft_ctx.usb_read_timeout = 5000000; self.ft_ctx.usb_write_timeout = 5000000; ftdi.ftdi_read_data_set_chunksize(self.ft_ctx, self.READ_CHUNKSIZE) ftdi.ftdi_write_data_set_chunksize(self.ft_ctx, self.WRITE_CHUNKSIZE) if ftdi.ftdi_usb_reset(self.ft_ctx) != 0: raise RuntimeError("Reset failed") time.sleep(100e-3) if ftdi.ftdi_set_bitmode(self.ft_ctx, 0, ftdi.BITMODE_SYNCFF) != 0: raise RuntimeError("Setting FIFO mode failed") if ftdi.ftdi_set_latency_timer(self.ft_ctx, 1) != 0: raise RuntimeError("Setting latency failed") except RuntimeError: self.close() raise
def shutdown(self): """ Shutdown ftdi com. """ self._log.info("Try to close ftdi port") err = ftdi.ftdi_usb_close(self.__ftdic) if err < 0: self._log.error("Can't close ftdi com. (%d, %s)" % (err, ftdi.ftdi_get_error_string(self.__ftdic))) raise FtdiError("Can't close ftdi com. (%d, %s)" % (err, ftdi.ftdi_get_error_string(self.__ftdic))) ftdi.ftdi_deinit(self.__ftdic)
def fermerPort(ftdiContext): # Exemple de desactivation des compteurs ftdi.ftdi_set_bitmode(ftdiContext, NO_PORT, ftdi.BITMODE_CBUS); ret = ftdi.ftdi_usb_close(ftdiContext) if ret < 0: raise IOError("Impossible de fermer le port : " + repr(ret) + "(" + ftdi.ftdi_get_error_string(ftdiContext) + ")") ftdi.ftdi_deinit(ftdiContext); del ftdiContext
def close_ftdi(): write_ftdi(255) # alle pins auf 0 ret = ftdi.ftdi_usb_close(ftdic) # ftdi-usb deinitialisieren if ret < 0: print "Konnte Gerät nicht schließen:" + ret + "(" + \ ftdi.ftdi_get_error_string(ftdic) + ")" exit(-1) else: print "Gerät" ftdi.ftdi_disable_bitbang(ftdic); ftdi.ftdi_deinit(ftdic); del ftdic
#!/usr/bin/python import ftdi import time F = ftdi.ftdi_context() ftdi.ftdi_init(F) ftdi.ftdi_usb_open(F, 0x0403, 0x6011) ftdi.ftdi_set_interface(F, ftdi.INTERFACE_B) ftdi.ftdi_set_bitmode(F, 0x08, ftdi.BITMODE_BITBANG) ftdi.ftdi_write_data(F, '\x00', 1) ftdi.ftdi_usb_close(F)
opening = False if toggle_door: LOG.info("Opening Door") opening = True # activating the relay ftdi.ftdi_write_data(ftdic, chr(ord(result) | CTS), 1) time.sleep(3) if toggle_door and opening: # trying to work with the existing result variable has strange # behavior - rereading the pins new_result = " " ftdi.ftdi_read_pins(ftdic, new_result) LOG.info("... %s vs %s & %s = %s", \ hex(ord(result)), hex(ord(new_result)), hex(~CTS), hex(ord(new_result) & (~CTS)) ) # resetting the relay ftdi.ftdi_write_data(ftdic, chr(ord(new_result) & ((~CTS) & (~RXT))), 1) toggle_door = False opening = False ftdi.ftdi_usb_close(ftdic)
def close_ftdi(): ftdi.ftdi_usb_close(F)
opening=False if toggle_door: LOG.info("Opening Door") opening = True # activating the relay ftdi.ftdi_write_data(ftdic, chr(ord(result) | CTS), 1) time.sleep(3) if toggle_door and opening: # trying to work with the existing result variable has strange # behavior - rereading the pins new_result = " " ftdi.ftdi_read_pins(ftdic, new_result) LOG.info("... %s vs %s & %s = %s", \ hex(ord(result)), hex(ord(new_result)), hex(~CTS), hex(ord(new_result) & (~CTS)) ) # resetting the relay ftdi.ftdi_write_data(ftdic, chr(ord(new_result) & ((~CTS) & (~RXT))), 1) toggle_door = False opening = False ftdi.ftdi_usb_close(ftdic)