0

I have two USB to Serial-Converters plugged into my PC. Both of them use FTDI chips so their VendorID and ProductID is the same. I need to read and write to only one of them but when I use usb.core.find(VendorID, ProductID) it always finds the wrong USB device. So I'm trying to figure out how to use the bus and the address to find the right one. The code I'm currently using to figure out which one I connected to is:

import usb.core

dev = usb.core.find(idVendor=0x0403, idProduct=0x6001)
if dev is None:
    raise ValueError('Gerät nicht angeschlossen!')

if dev.address == 2 and dev.bus == 2:  # usb->serial converter motor
    print("Motor")
elif dev.address == 1 and dev.bus == 2:  # usb->serial converter laser
    print("Laser")
else:
    print("Weder Laser noch Motor")
print(dev)

I need it to find the usb to serial converter of the motor but it always finds the laser. Is there a way to use dev.bus and dev.address in the usb.core.find() function? I'm new to python & pyusb so I need to print everything for my own understanding. In PyUSB's tutorial it says it's possible using the bus and the address but I still couldn't figure it out after reading it.

Jamiu S.
  • 5,257
  • 5
  • 12
  • 34
mugnuff
  • 37
  • 3
  • do they have different serial numbers? – A. Herlas Oct 10 '22 at 10:50
  • @A.Herlas yes they do. The serial number of the motor's is GAFR0000 and the serial number of the laser's is AQ027KF4. – mugnuff Oct 10 '22 at 11:08
  • Can you use these in order to identify them and not by address and bus? Will you communicate with these Motor and Laser over the serial, by sending commands or receiving some information? – A. Herlas Oct 10 '22 at 11:27
  • I will communicate with both of them by sending commands and receiving information. I need to tell the motor (step motor) to drive any distance and measure the travelled distance by using the laser, then I'll compare the distance after. It didn't say I could use the serial number on pyusb's page and I didn't find anyone with the same problem either so im kind of stuck with the bus and address idea since it was mentioned by pyusb. – mugnuff Oct 10 '22 at 11:36
  • here: https://stackoverflow.com/questions/24956198/get-serial-number-of-usb-device-with-python-3 `print( usb.util.get_string( dev, dev.iSerialNumber ) )` – A. Herlas Oct 10 '22 at 11:51
  • Otherwise you can implement on the motor and laser side some commands to which they will respond with some string to your python code, in order to identify them. – A. Herlas Oct 10 '22 at 11:54
  • The first one doesn't work since I need different Product IDs but they are the same so it gave me the same output twice (It gave me the lasers serial number twice. I'll try to to implement a command or do something similar to tell them apart. – mugnuff Oct 10 '22 at 12:04
  • Use find_all option `devs = usb.core.find(find_all=True, idVendor=0x0403, idProduct=0x6001)`, then iterate through the devices, this will give all devices from the same vendor and product. – A. Herlas Oct 10 '22 at 12:12
  • Thank you! I'll only need the one of them for now so I'll try to skip one iteration with itertools – mugnuff Oct 10 '22 at 12:58
  • you can pass in `serial_number` as an argument to the `find` function. `usb.core.find(serial_number="GAFR0000")` – Teejay Bruno Oct 10 '22 at 17:50
  • If i use `import usb.core dev = usb.core.find(idVendor=0x0403, idProduct=0x6001, serial_number="GAFR0000") if dev is None: raise ValueError('Not connected') print(dev)` I get an Errormessage telling me the device is not connected although it is. `Traceback (most recent call last): File "D:\PycharmProjects\motorseriell\main.py", line 5, in raise ValueError('Not connected') ValueError: Not connected` If I just use the VendorID & ProductID I can `print(dev)` but I get this `Error Accessing String` for the serial number. I think it might be a driver related issue. – mugnuff Oct 11 '22 at 08:31

0 Answers0