Search code examples
pythonubuntuhidhidapi

Writing data to a USB scanner with Python


I'm leveraging the Python HID API to attempt to write data to a scanner. The specific command I am trying to write is "A1 04 00"(where A1 Is the ID 04 is the command and 00 is the data)and currently I am writing to the scanner following the specific command format, which is below.

Image here

In code this looks like data=[0x05,0x57,0xA1,0x04,0x00,0xFE,0xFF] and I'm using device.write(data) however I get no response from the scanner. I don't have a lot of experience in interfacing HID devices with Python so I'm sure I'm doing something incorrectly, thanks for the help!

Edit: Code added per request

import hid
data=[0x05,0x57,0xA1,0x04,0x00,0xFE,0xFF]
VENDOR_ID = 0x24ea
PRODUCT_ID = 0x0197

device = hid.device()
device.open(VENDOR_ID,PRODUCT_ID)
device.write(data)

Checksum algorithm per scanner docs

0x10000 – [ Length] – [ Source] – [ExID] – [ExCMD] – [D1 + D2 +D3 +…..]

Solution

  • The scanner data is delivered to you over a HID keyboard interface, but that's output-only. The control sequences go over a USB virtual serial port. You will need to use something like pyserial to access that. It can be a bit tricky to figure out which serial port is the scanner; do ls /dev/tty* before and after you plug in to find it.