UniNE-CHYN / thorpy

Python library implementing Thorlabs APT communication protocol
43 stars 22 forks source link

Error in discover_stages #22

Open aveekchandra opened 3 years ago

aveekchandra commented 3 years ago

I have been trying to interface Thorlabs APT controller (TDC001) using thorpy_master. I have plugged and unplugged the device but I still get the same error message (see below) in the discovery_stages inside thorpy.comm. I have printed the serial ports (<print(serial_ports)>) in discovery_stages and you can see the controller has been detected as 'Brushed Motor Controller'. However, when I do print port_candidates it gives a null array. While trying several times, once or twice it returned the correct port_candidates at the print-out but returned null soon after. Please advise.

Output messages with error: [('/dev/ttyS4', 'n/a', {}), ('/dev/ttyUSB0', 'Brushed Motor Controller', {'VID:PID': '0403:FAF0', 'SER': '27003927', 'LOCATION': '1-1'})] ---> <print(serial_ports)> [] ----> print(port_candidates) Traceback (most recent call last): File "test.py", line 6, in stages = list(discover_stages()) File "/home/qitlab/anaconda3/lib/python3.7/site-packages/thorpy/comm/discovery.py", line 28, in discover_stages assert len(port_candidates) == 1 AssertionError

isildur7 commented 1 year ago

replace the discover.py file with this code:

    from .port import Port
    from serial.tools.list_ports import comports
    import platform

    serial_ports = [
        (x[0], x[1], dict(y.split("=", 1) for y in x[2].split(" ") if "=" in y))
        for x in comports()
    ]

    if platform.system() == "Linux":
        port_candidates = [
            x
            for x in serial_ports
            if x[1] == "Brushed Motor Controller - Brushed Motor Controller"
        ]
        print(port_candidates)
    else:
        raise NotImplementedError(
            "Implement for platform.system()=={0}".format(platform.system())
        )

    assert len(port_candidates) == 1

    port = port_candidates[0]

    p = Port.create(port[0], port[2].get("SER", None))
    for stage in p.get_stages().values():
        yield stage

if __name__ == "__main__":
    print(list(discover_stages()))

# iManufacturer           1 Thorlabs
#    iProduct                2 APT DC Motor Controller
#    iSerial                 3 83856536

This works for me.