-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathread.py
executable file
·40 lines (33 loc) · 867 Bytes
/
read.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/usr/bin/env python
#
# Keep reading a DYMO scale until too many errors
# Mainly taken from http://steventsnyder.com/reading-a-dymo-usb-scale-using-python/ with a few alterations
#
import usb.core
import usb.util
from time import sleep
VENDOR_ID = 0x0922
PRODUCT_ID = 0x8003
# find the USB device
device = usb.core.find(idVendor=VENDOR_ID, idProduct=PRODUCT_ID)
if not device:
print 'ERROR: No device found.'
exit(1)
# use the first/default configuration
device.set_configuration()
# first endpoint
endpoint = device[0][(0,0)][0]
# read a data packet
attempts = 10
while attempts > 0:
try:
data = device.read(endpoint.bEndpointAddress,
endpoint.wMaxPacketSize)
print data
sleep (0.5)
except usb.core.USBError as e:
data = None
if e.args == ('Operation timed out',):
attempts -= 1
continue
print 'Done.'