Sending and receiving bytes to a HID device on Raspberry Pi in Python?

Viewed 571

I'm on a Raspberry Pi 4 and have a HID device connected via USB which comes up as /dev/hidraw0. (In case you're curious: it's an HP Prime calculator.)

I need to send two bytes to the device and expect the device to answer with a bytestream of unknown size. It isn't a constant size, its length varies.

First problems first: I don't seem to be able to send exactly two bytes to the device. This is my code:

import os, sys
fd = os.open("/dev/hidraw0", os.O_RDWR|os.O_NONBLOCK)
bytesWritten = os.write(fd, bytes([252, 8]))
print('bytes written: ' + str(bytesWritten))

This prints out:

bytes written: 8

Why were 8 bytes written when I only specified two bytes? What's going on here?

Second question: in a first test I wanted to read a constant number of bytes back from the device by means of:

answer = os.read(fd, 20)

This results in the error

OSError: [Errno 11] Resource temporarily unavailable

Any basic advice for this? Or does it depend on how the HID protocol is implemented on the device side?

Thanks so much!

0 Answers
Related