I am running Python 3 with Anaconda in a Windows 10 machine.
I am trying to connect to a usb barcode scanner in order to read barcodes and store them in variables in other python routine. I have found an example using PyUSB library and tried it. After a few modifications I was able to run it without raising errors. The program connects to the usb with the following code:
import usb.core
import usb.util
def main():
# Find usb device
dev = usb.core.find(idVendor=0x05E0, idProduct=0x1200)
# Raise error if device not found
if dev is None:
raise ValueError('Device not found')
else:
# Set configuration of device
dev.set_configuration()
endpoint = dev[0][(0,0)][0]
data = None
while True:
try:
dev.read(endpoint.bEndpointAddress, endpoint.wMaxPacketSize, timeout=1)
print(data)
except usb.core.USBError as e:
data = None
if e.args == ('Operation timed out',):
print("ERROR")
continue
if __name__ == '__main__':
main()
As I say the program runs correctly and finds the usb. Nonetheless, data (barcode number) is never printed because the program seems to get stuck at following line:
dev.read(endpoint.bEndpointAddress, endpoint.wMaxPacketSize, timeout=1)
Can you help me?
Thank you very much in advance!