Пример #1
0
 def purgeBuffers(self):
     """ Purge ftdi buffers
     """
     err = ftdi.ftdi_usb_purge_buffers(self.__ftdic)
     if err < 0:
         self._log.error("Can't purge buffers (%d, %s)" % (err, ftdi.ftdi_get_error_string(self.__ftdic)))
         raise FtdiError("Can't purge buffers (%d, %s)" % (err, ftdi.ftdi_get_error_string(self.__ftdic)))
Пример #2
0
 def selectPort(self, port):
     """ Select the giver port
     """
     err = ftdi.ftdi_set_bitmode(self.__ftdic, port, ftdi.BITMODE_CBUS)
     if err < 0:
         self._log.error("Can't set bitmode (%d, %s)" % (err, ftdi.ftdi_get_error_string(self.__ftdic)))
         raise FtdiError("Can't set bitmode (%d, %s)" % (err, ftdi.ftdi_get_error_string(self.__ftdic)))
     time.sleep(0.1)
Пример #3
0
    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)
Пример #4
0
 def readOne(self):
     """ read 1 char from usb
     """
     buf = ' '
     err = ftdi.ftdi_read_data(self.__ftdic, buf, 1)
     if err < 0:
         self._log.error("Can't read data (%d, %s)" % (err, ftdi.ftdi_get_error_string(self.__ftdic)))
         self.shutdown()
         raise FtdiError("Can't read data (%d, %s)" % (err, ftdi.ftdi_get_error_string(self.__ftdic)))
     if err:
         c = unichr(ord(buf) % 0x80)  # Clear bit 7
         return c
     else:
         return None
Пример #5
0
    def init(self):
        """ Init ftdi com.
        """

        # Create ftdi context
        self._log.info("Try to Create ftdi context")
        self.__ftdic = ftdi.ftdi_context()
        if self.__ftdic is None:
            self._log.error("Can't create ftdi context")
            raise FtdiError("Can't create ftdi context")

        # Init ftdi context
        err = ftdi.ftdi_init(self.__ftdic)
        if err < 0:
            self._log.error("Can't init ftdi context (%d, %s)" % (err, ftdi.ftdi_get_error_string(self.__ftdic)))
            raise FtdiError("Can't init ftdi context (%d, %s)" % (err, ftdi.ftdi_get_error_string(self.__ftdic)))

        # Open port
        self._log.info("Try to open ftdi port")
        err = ftdi.ftdi_usb_open(self.__ftdic, USB_VENDOR, USB_PRODUCT)
        if err < 0:
            self._log.error("Can't open usb (%d, %s)" % (err, ftdi.ftdi_get_error_string(self.__ftdic)))
            raise FtdiError("Can't open usb (%d, %s)" % (err, ftdi.ftdi_get_error_string(self.__ftdic)))

        err = ftdi.ftdi_set_baudrate(self.__ftdic, BAUD_RATE)
        if err < 0:
            self._log.error("Can't set baudrate (%d, %s)" % (err, ftdi.ftdi_get_error_string(self.__ftdic)))
            raise FtdiError("Can't set baudrate (%d, %s)" % (err, ftdi.ftdi_get_error_string(self.__ftdic)))

        # Because of the usb interface, must use 8 bits transmission data, instead of 7 bits
        err = ftdi.ftdi_set_line_property(self.__ftdic, ftdi.BITS_8, ftdi.EVEN, ftdi.STOP_BIT_1)
        if err < 0:
            self._log.error("Can't set line property (%d, %s)" % (err, ftdi.ftdi_get_error_string(self.__ftdic)))
            raise FtdiError("Can't set line property (%d, %s)" % (err, ftdi.ftdi_get_error_string(self.__ftdic)))
Пример #6
0
def open_ftdi():
	if (ftdi.ftdi_init(ftdic) < 0) :
		print "ftdi_init fehlgeschlagen"
		exit(-1)
	
	else:
		print "ftdi_init geklappt"


	#Öffne FTDI-Chip FT232R - evt. andere Nummern eintragen! 
	#Tut auch mit FT245BL
	ret = ftdi.ftdi_usb_open(ftdic, 0x0403, 0x6001)
	if (ret) < 0:
		print "Konnte Gerät nicht öffnen:" + ret + "(" + \
			ftdi.ftdi_get_error_string(ftdic) + ")"
		exit(-1)
	else:
		print "Gerät erfolgreich geöffnet"

	#Bitbag mit DTR, RX und TX beginnen
	#2. Argument macht die Pins D0-D7 ansprechbar


	#ftdi.ftdi_enable_bitbang(ftdic, D[0]|D[1]|D[2]|D[3]|D[4]|D[5]|D[6]|D[7] )
	ftdi.ftdi_enable_bitbang(ftdic,0x1 | 0x2| 0x4| 0x8| 0x10| 0x20| 0x40| 0x80)

	#Am Anfang ausschalten
	ftdi.ftdi_write_data(ftdic, chr(255), 1)
Пример #7
0
    def __init__(self, serial=None, index=None):
        self.ftdic = ftdi.ftdi_context()
        self.ftdi = ftdi
        self.sensors = dict()
        self.hw_sw = dict()
        self.hw_sw["DC"] = nvouten
        self.hw_sw["SW0"] = sws[0]
        self.hw_sw["SW1"] = sws[1]
        self.hw_sw["ALERT"] = alert
        self.hw_sw["GPIO0"] = gpios[0]
        self.hw_sw["GPIO1"] = gpios[1]
        self.hw_sw["GPIO2"] = gpios[2]
        self.hw_sw["GPIO3"] = gpios[3]
        self.switches = dict()

        try:
            ret = ftdi.ftdi_init(self.ftdic)
            if ret < 0:
                raise Exception
            ret = ftdi.ftdi_usb_open_desc_index(self.ftdic, 0x0403, 0x06010, "PMC-17 v1.0", serial, index if index else 0)
            if ret < 0:
                raise Exception("Could not open device", ftdi.ftdi_get_error_string(self.ftdic))
            ret = ftdi.ftdi_set_interface(self.ftdic, ftdi.INTERFACE_A)
            if ret < 0:
                raise Exception
            self.hw = i2c_ftdi.i2c_ftdi(self.ftdic, scl, sda_out, sda_in, 400000, initial_output)
            self.i2c = i2c.i2c(self.hw)
        except Exception as e:
            ftdi.ftdi_deinit(self.ftdic)
            raise
Пример #8
0
    def __init__(self, serial=None, index=None):
        self.ftdic = ftdi.ftdi_context()
        self.ftdi = ftdi
        self.sensors = dict()
        self.hw_sw = dict()
        self.hw_sw["DC"] = nvouten
        self.hw_sw["SW0"] = sws[0]
        self.hw_sw["SW1"] = sws[1]
        self.hw_sw["ALERT"] = alert
        self.hw_sw["GPIO0"] = gpios[0]
        self.hw_sw["GPIO1"] = gpios[1]
        self.hw_sw["GPIO2"] = gpios[2]
        self.hw_sw["GPIO3"] = gpios[3]
        self.switches = dict()

        try:
            ret = ftdi.ftdi_init(self.ftdic)
            if ret < 0:
                raise Exception
            ret = ftdi.ftdi_usb_open_desc_index(self.ftdic, 0x0403, 0x06010,
                                                "PMC-17 v1.0", serial,
                                                index if index else 0)
            if ret < 0:
                raise Exception("Could not open device",
                                ftdi.ftdi_get_error_string(self.ftdic))
            ret = ftdi.ftdi_set_interface(self.ftdic, ftdi.INTERFACE_A)
            if ret < 0:
                raise Exception
            self.hw = i2c_ftdi.i2c_ftdi(self.ftdic, scl, sda_out, sda_in,
                                        400000, initial_output)
            self.i2c = i2c.i2c(self.hw)
        except Exception as e:
            ftdi.ftdi_deinit(self.ftdic)
            raise
Пример #9
0
    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
Пример #10
0
    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
Пример #11
0
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
Пример #12
0
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
Пример #13
0
 def __init__(self, ftdi_):
     """
     """
     logging.info("TELEINFO------Initialisation de la teleinfo")
     logging.info("TELEINFO------FTDI TYPE : " + str(ftdi_type))
     if ftdi_type == 0:
         globals.ftdi_context = ""
         super(Teleinfo, self).__init__()
         self.__ftdi = ftdi_
     else:
         globals.ftdi_context = ftdi.new()
         ret = ftdi.usb_open(globals.ftdi_context, 0x0403, 0x6001)
         if ret < 0:
             logging.error("Can't open usb (%d, %s)" % (err, ftdi.ftdi_get_error_string(self.__ftdic)))
         ftdi.set_baudrate(globals.ftdi_context, int(globals.vitesse))
     if globals.mode == "historique":
         globals.frame_length = 500
Пример #14
0
    def __init__(self):
        '''
        Docstring

        Parameters
        ----------

        Returns
        -------
        '''
        super(Button, self).__init__()
        self.port = None
        port = ftdi.ftdi_new()
        usb_open = ftdi.ftdi_usb_open_string(port, "s:0x403:0x6001:2eb80091")
        assert usb_open == 0, ftdi.ftdi_get_error_string(port)

        ftdi.ftdi_set_bitmode(port, 0xFF, ftdi.BITMODE_BITBANG)
        self.port = port
        self.queue = queue.Queue()
        self.daemon = True
        self.start()
Пример #15
0
    def __init__(self):
        '''
        Docstring

        Parameters
        ----------

        Returns
        -------
        '''
        super(Button, self).__init__()
        self.port = None
        port = ftdi.ftdi_new()
        usb_open = ftdi.ftdi_usb_open_string(port, "s:0x403:0x6001:2eb80091")
        assert usb_open == 0, ftdi.ftdi_get_error_string(port)
        
        ftdi.ftdi_set_bitmode(port, 0xFF, ftdi.BITMODE_BITBANG)
        self.port = port
        self.queue = Queue.Queue()
        self.daemon = True
        self.start()
Пример #16
0
def ouvrirPort(port):
	# Création du context
	ftdiContext = ftdi.ftdi_context()

	# Initialisation du context
	if ftdi.ftdi_init(ftdiContext) < 0:
		raise IOError("Echec de l'initialisation")

	# Ouverture du port
	ret = ftdi.ftdi_usb_open(ftdiContext, 0x0403, 0x6001)
	if (ret) < 0:
		raise IOError("Impossible d'ouvrir le port " + hex(port) + " : " + repr(ret) + "(" + ftdi.ftdi_get_error_string(ftdiContext) + ")")


	# Fixer le debit à 1200 bit/s => 150 car/s (8 bits/car)
	ret = ftdi.ftdi_set_baudrate(ftdiContext, 1200)
	if (ret) < 0:
		raise IOError("Impossible de fixer le baudrate pour le port " + hex(port) + " : " + repr(ret) + "(" + ftdi.ftdi_get_error_string(ftdiContext) + ")")

	# Pour une obscure raison pour le moment il faut mettre ftdi.BITS_8
	# et non ftdi.BITS_7 comme indiqué dans la spec pour que cela fonctionne ???
	ret = ftdi.ftdi_set_line_property(ftdiContext, ftdi.BITS_8, ftdi.EVEN, ftdi.STOP_BIT_1)

	# Activation du compteur 1 et lecture de qqes trames
	ret = ftdi.ftdi_set_bitmode(ftdiContext, port, ftdi.BITMODE_CBUS);

	# Fixer le timeout de lecture
	ftdiContext.usb_read_timeout = 50000; 

	return ftdiContext