Accessing USB input in container in python

Could you also share the snippet you use to read data from the device?
It may help analyzing and reproducing the problem?

Hi @roman-mazur,

I have a created a repo which replicates my issue: https://github.com/Junett/VLP.

Thanks,

Thanks for sharing!

If I read your Python code directly, it attempts to read from standard input. However, there is nothing that would connect your python process standard input to the actual device.
You mentioned that your scanner device is available as /dev/hidraw0 from the container.
In this case, assuming this device driver is implemented in a way that allows reading data from that file, I would attempt reading from that file directly in your Python code with something like

with open('/dev/hidraw0') as f:
  line = f.readLine()

If you prefer keeping your python code in a form when it accesses std input instead of a specific file, you will need to make a pipe between that device stream and your process, wrapping your container entry point with a script similar to

cat /dev/hidraw0 | python3 main.py

(you would need to add such script to your Dockerfile and set it as CMD on the image)

Again, this might work if the device driver allows reading from the device file like cat /dev/hidraw0.

Hi @roman-mazur,

I’ve just tried:

with open(’/dev/hidraw0’) as f: line = f.readLine()

But I still had the same result. I will continue to look into ways to read direct from the device as this would probably be the best approach.

Thanks for your help, it is appreciated!

Hi @junett ,

Whilst it’s not python, we’ve a NodeJS example that works that might be useful, which uses a USB HID library to read from a USB HID device given a vendor and product ID. That’s here: https://github.com/balena-io-playground/node-keyboard-input

Hopefully this will help!

Best regards,

Heds

I am having much the same problem as the above user. I’m using code that works fine in standalone, my usb device detects as a keyboard and works without issue, but a call to n = input() results in nothing being read when the USB device is hit. I dont usually do readline, but raw_input or input now on python 3, so didnt ry with open, but I could. I was trying to figure out how to do the cat /dev/hidraw0 (which my device also shows up as) piping it to python3 and my program name, but that one failed as well saying cat not found…so whatever I did in the docker image was clearly the wrong thing. If it would help I could provide access and the UUID as well.

I have put in 1 day of access to uuid: f751d7c0616a7644a57686d47b652aec

Any help greatly appreciated!

Hi @MydKnight,

I tried multiple different ways of trying to read the input but still couldn’t get any to work. I never tried this method:

I wasn’t sure exactly where to put it, I wasn’t sure if it was meant to be in the Dockerfile or a Docker container was required.

If you do find a solution, I would appreciate if you could post it on this thread as I am still stuck!!

Thanks!

yeah, I intellectually understand what that thing says its doing, but im not entirely certain how to implement the above. Hopefully whoever asked you for remote access to your application container can get into mine and see something we’ve missed.

Had some local confusion on my device so its reimaged. Support access granted for UUID feceb8ba298dd234563030eaa9bda4a9

So, this sucks, but it works at least at a base level:

dev = os.open("/dev/usb/hiddev0", os.O_RDONLY) 

Then you read it with:

n = os.read(dev, 10)

This allows you to read the raw input values for the usb port. There HAS to be a better option though.

Hi @mydknight ,
a collegue suggested this solution ( https://github.com/shaunmulligan/resin-keyboard-example ) to read keyboard entries in a container. I have given it a try as he suggested it is a bit old and might not work any more but it works for me. I can see keyboard input in the journal.

After some digging around, I installed evdev, which seems to let me connect to a usb port and capture its input. Theres some hoop jumping to go from that to useable keyboard output, but id say I got 90 percent there from someones code using a barcode scanner. Seems to be working just fine now. Message me if you need more.