Example #1
0
    def list_devices(filter_ids=True):
        """
        List all connected devices.

        Return list of tuples ``(conn, description)``.
        If ``filter_ids==True``, only leave devices with Tholabs-like IDs (8-digit numbers).
        Otherwise, show all devices (some of them might not be Thorlabs-related).
        """
        def _is_thorlabs_id(id):
            return re.match(rb"^\d{8}$",id[0]) is not None
        ids=ft232.list_devices()
        if filter_ids:
            ids=[id for id in ids if _is_thorlabs_id(id)]
        return ids
    def __init__(self, terminal, opts):
        # Lazy-init FT232.
        # On windows systems, if D2XX drivers are not installed
        try:
            import ft232
        except:
            print("ERROR: Failed to load pyft232")
            print("ERROR: Is D2XX/libftdi installed?")
            sys.exit(1)
        # HACK: On some linux systems (debian buster) we can't free libftdi context
        # HACK: or things will segfault
        # HACK: This hack causes intentional memory leak, but prevents segfault
        def dummy(x):
            pass

        try:
            ft232.libftdi.ftdi.ftdi_free = dummy
        except:
            pass

        serial = opts["mt12505_serial"]
        if opts["mt12505_serial"] == None:
            devices = ft232.list_devices()
            devices = self.__to_mt12505_devices(devices)
            if len(devices) == 0:
                raise Exception("ERROR: No MT12505 devices found")

            if len(devices) == 1:
                serial = devices[0][0]
            else:
                print(
                    "WARNING: Multiple MT12505 devices found, please specify serial"
                )
                for d in devices:
                    print(f"Serial: {d[0]} Description: {d[1]}")
                sys.exit(1)

        self.sp = ft232.Ft232(serial, baudrate=115200)
        self.sp.cbus_setup(mask=0xf, init=0xf)
        return super().__init__(terminal, opts)