Discussion:
[pyusb-users] question regarding HID with PYUSB on raspberry pi
Chhavi Goenka
2016-01-21 20:17:34 UTC
Permalink
Hi,
I am trying to interface raspberry pi with a custom HID device via USB. I
am using PYUSB for this. But once I run my program i get a timeout error.

Here is my program:

import os
import sys
import time


import usb.core
import usb.util

packet_len = 64

# packet request function
def pack_request(*arguments):
packet = [0x0] * packet_len
i = 0
for arg in arguments:
packet[i] = arg
i+=1
return ''.join([chr(c) for c in packet])

def show_result(bytes):
sys.stdout.write("Result:")
sys.stdout.write(''.join(['%d ' % abyte for abyte in bytes]))


def main():
dev = usb.core.find(idVendor = 0x04d8, idProduct = 0xf2a6)

if dev is None:
raise ValueError('Device not found')

#dev.set_configuration()

if dev.is_kernel_driver_active(0):
reattach = True
dev.detach_kernel_driver(0)

cfg = dev.get_active_configuration()
intf = cfg[(0,0)]

ep = usb.util.find_descriptor(
intf,
# match the first OUT endpoint
custom_match = \
lambda e: \
usb.util.endpoint_direction(e.bEndpointAddress) == \
usb.util.ENDPOINT_OUT)

assert ep is not None

raw = pack_request(0x00, 0x81)

dev.write(1, raw, 100)

bytes = dev.read(0x81,packet_len, 10000)
show_result(bytes)

#print(bytes)

if __name__ == '__main__':
main()


and Here is the error:

***@raspberrypi:~/Documents $ sudo python multispec_usbcode_jan21.py
Traceback (most recent call last):
File "multispec_usbcode_jan21.py", line 66, in <module>
main()
File "multispec_usbcode_jan21.py", line 58, in main
dev.write(1, raw,100)
File "/usr/local/lib/python2.7/dist-packages/usb/core.py", line 878, in
write
self.__get_timeout(timeout)
File "/usr/local/lib/python2.7/dist-packages/usb/backend/libusb1.py",
line 778, in intr_write
timeout)
File "/usr/local/lib/python2.7/dist-packages/usb/backend/libusb1.py",
line 856, in __write
_check(retval)
File "/usr/local/lib/python2.7/dist-packages/usb/backend/libusb1.py",
line 552, in _check
raise USBError(_strerror(ret), ret, _libusb_errno[ret])
usb.core.USBError: [Errno 110] Operation timed out

Please let me know how to get around this?

Thanks,
Chhavi
Jared Contrascere
2016-01-21 21:42:57 UTC
Permalink
Hi, Chhavi!

I'll defer to other opinions on the matter, but I think pyusb might be too
low-level for your purposes, Chhavi. I've been able to interface with a HID
scale on a Raspberry Pi using the python 'hidapi' package. See
https://github.com/libretees/silverscale for an example project.

--contracode
Post by Chhavi Goenka
Hi,
I am trying to interface raspberry pi with a custom HID device via USB. I
am using PYUSB for this. But once I run my program i get a timeout error.
import os
import sys
import time
import usb.core
import usb.util
packet_len = 64
# packet request function
packet = [0x0] * packet_len
i = 0
packet[i] = arg
i+=1
return ''.join([chr(c) for c in packet])
sys.stdout.write("Result:")
sys.stdout.write(''.join(['%d ' % abyte for abyte in bytes]))
dev = usb.core.find(idVendor = 0x04d8, idProduct = 0xf2a6)
raise ValueError('Device not found')
#dev.set_configuration()
reattach = True
dev.detach_kernel_driver(0)
cfg = dev.get_active_configuration()
intf = cfg[(0,0)]
ep = usb.util.find_descriptor(
intf,
# match the first OUT endpoint
custom_match = \
lambda e: \
usb.util.endpoint_direction(e.bEndpointAddress) == \
usb.util.ENDPOINT_OUT)
assert ep is not None
raw = pack_request(0x00, 0x81)
dev.write(1, raw, 100)
bytes = dev.read(0x81,packet_len, 10000)
show_result(bytes)
#print(bytes)
main()
File "multispec_usbcode_jan21.py", line 66, in <module>
main()
File "multispec_usbcode_jan21.py", line 58, in main
dev.write(1, raw,100)
File "/usr/local/lib/python2.7/dist-packages/usb/core.py", line 878, in
write
self.__get_timeout(timeout)
File "/usr/local/lib/python2.7/dist-packages/usb/backend/libusb1.py",
line 778, in intr_write
timeout)
File "/usr/local/lib/python2.7/dist-packages/usb/backend/libusb1.py",
line 856, in __write
_check(retval)
File "/usr/local/lib/python2.7/dist-packages/usb/backend/libusb1.py",
line 552, in _check
raise USBError(_strerror(ret), ret, _libusb_errno[ret])
usb.core.USBError: [Errno 110] Operation timed out
Please let me know how to get around this?
Thanks,
Chhavi
------------------------------------------------------------------------------
Site24x7 APM Insight: Get Deep Visibility into Application Performance
APM + Mobile APM + RUM: Monitor 3 App instances at just $35/Month
Monitor end-to-end web transactions and take corrective actions now
Troubleshoot faster and improve end-user experience. Signup Now!
http://pubads.g.doubleclick.net/gampad/clk?id=267308311&iu=/4140
_______________________________________________
pyusb-users mailing list
https://lists.sourceforge.net/lists/listinfo/pyusb-users
Chhavi Goenka
2016-01-22 17:19:23 UTC
Permalink
Thank you Jared. I will look at your project and try HIDAPI for my project.

On Thu, Jan 21, 2016 at 4:42 PM, Jared Contrascere <
Post by Jared Contrascere
Hi, Chhavi!
I'll defer to other opinions on the matter, but I think pyusb might be too
low-level for your purposes, Chhavi. I've been able to interface with a HID
scale on a Raspberry Pi using the python 'hidapi' package. See
https://github.com/libretees/silverscale for an example project.
--contracode
Post by Chhavi Goenka
Hi,
I am trying to interface raspberry pi with a custom HID device via USB. I
am using PYUSB for this. But once I run my program i get a timeout error.
import os
import sys
import time
import usb.core
import usb.util
packet_len = 64
# packet request function
packet = [0x0] * packet_len
i = 0
packet[i] = arg
i+=1
return ''.join([chr(c) for c in packet])
sys.stdout.write("Result:")
sys.stdout.write(''.join(['%d ' % abyte for abyte in bytes]))
dev = usb.core.find(idVendor = 0x04d8, idProduct = 0xf2a6)
raise ValueError('Device not found')
#dev.set_configuration()
reattach = True
dev.detach_kernel_driver(0)
cfg = dev.get_active_configuration()
intf = cfg[(0,0)]
ep = usb.util.find_descriptor(
intf,
# match the first OUT endpoint
custom_match = \
lambda e: \
usb.util.endpoint_direction(e.bEndpointAddress) == \
usb.util.ENDPOINT_OUT)
assert ep is not None
raw = pack_request(0x00, 0x81)
dev.write(1, raw, 100)
bytes = dev.read(0x81,packet_len, 10000)
show_result(bytes)
#print(bytes)
main()
File "multispec_usbcode_jan21.py", line 66, in <module>
main()
File "multispec_usbcode_jan21.py", line 58, in main
dev.write(1, raw,100)
File "/usr/local/lib/python2.7/dist-packages/usb/core.py", line 878,
in write
self.__get_timeout(timeout)
File "/usr/local/lib/python2.7/dist-packages/usb/backend/libusb1.py",
line 778, in intr_write
timeout)
File "/usr/local/lib/python2.7/dist-packages/usb/backend/libusb1.py",
line 856, in __write
_check(retval)
File "/usr/local/lib/python2.7/dist-packages/usb/backend/libusb1.py",
line 552, in _check
raise USBError(_strerror(ret), ret, _libusb_errno[ret])
usb.core.USBError: [Errno 110] Operation timed out
Please let me know how to get around this?
Thanks,
Chhavi
------------------------------------------------------------------------------
Site24x7 APM Insight: Get Deep Visibility into Application Performance
APM + Mobile APM + RUM: Monitor 3 App instances at just $35/Month
Monitor end-to-end web transactions and take corrective actions now
Troubleshoot faster and improve end-user experience. Signup Now!
http://pubads.g.doubleclick.net/gampad/clk?id=267308311&iu=/4140
_______________________________________________
pyusb-users mailing list
https://lists.sourceforge.net/lists/listinfo/pyusb-users
------------------------------------------------------------------------------
Site24x7 APM Insight: Get Deep Visibility into Application Performance
APM + Mobile APM + RUM: Monitor 3 App instances at just $35/Month
Monitor end-to-end web transactions and take corrective actions now
Troubleshoot faster and improve end-user experience. Signup Now!
http://pubads.g.doubleclick.net/gampad/clk?id=267308311&iu=/4140
_______________________________________________
pyusb-users mailing list
https://lists.sourceforge.net/lists/listinfo/pyusb-users
Loading...