Read QR codes from HID devices
Project description
QR Code Scanner
A Python library for reading QR codes from HID (Human Interface Device) scanners. This library treats QR code scanners as keyboard input devices and decodes their scanned data.
Features
- Easy-to-use HID device interface
- Support for multiple keyboard layouts
- Automatic character decoding
- Robust error handling
- Non-blocking read operations
Installation
Install using pip:
pip install qrcode-scanner
Or using Poetry:
poetry add qrcode-scanner
Requirements
- Python 3.10+
- hidapi
Quick Start
from qrcode_scanner import HIDScanner
from qrcode_scanner.exceptions import DeviceConnectionError, DeviceNotFoundError, DeviceReadError
# Replace with your device's vendor ID and product ID
VENDOR_ID = 0x1D82
PRODUCT_ID = 0x5CA0
try:
# Initialize and connect to the scanner
scanner = HIDScanner(vendor_id=VENDOR_ID, product_id=PRODUCT_ID)
scanner.connect()
print("Connected to device")
print("Manufacturer:", scanner.device.get_manufacturer_string())
print("Product:", scanner.device.get_product_string())
# Start reading QR codes
while True:
try:
print("Listening for scans...")
scanned_text = scanner.read() # Blocking read operation
if scanned_text:
print("=== SCAN COMPLETE ===")
print("Scanned text:", scanned_text)
except DeviceNotFoundError:
print("Device not found or not connected")
break
except (DeviceReadError, DeviceConnectionError) as e:
print(f"Device error: {e}")
break
except KeyboardInterrupt:
print("Program terminated by user")
finally:
scanner.close()
print("HID device closed")
Error Handling
The library includes several exception classes to handle different error scenarios:
DeviceNotFoundError: When the specified device cannot be foundDeviceConnectionError: When there are issues connecting to the deviceDeviceNotConnectedError: When trying to read from a disconnected deviceDeviceReadError: When reading from the device failsUnknownCharacterError: When encountering unknown character codes
Finding Device IDs
To find your device's vendor and product IDs, you can use the devices() function:
from qrcode_scanner import devices
# List all connected HID devices
for device in devices():
print(f"Vendor ID: 0x{device['vendor_id']:04X}")
print(f"Product ID: 0x{device['product_id']:04X}")
print(f"Product Name: {device.get('product_string', 'N/A')}")
print("---")
License
MIT License
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file qrcode_scanner-0.0.2.tar.gz.
File metadata
- Download URL: qrcode_scanner-0.0.2.tar.gz
- Upload date:
- Size: 3.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.10.12 Linux/5.15.0-139-generic
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8a1164edf8ff64308380c3c5cf42c8baaa60823a1d50dda7d1cb8b585d89952b
|
|
| MD5 |
e7b8d73a50107f12fd5a1d85459c9f6e
|
|
| BLAKE2b-256 |
be92d2f0d7c18b4214a752580c9e046af96398f87095e96fe8d3522577945d08
|
File details
Details for the file qrcode_scanner-0.0.2-py3-none-any.whl.
File metadata
- Download URL: qrcode_scanner-0.0.2-py3-none-any.whl
- Upload date:
- Size: 4.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.10.12 Linux/5.15.0-139-generic
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dff924f4c1eb58b83707fe72f96435ae200b7e757c07c687c0a98c9a8fef92c5
|
|
| MD5 |
d61a802929fbb46ce447f642bebffd79
|
|
| BLAKE2b-256 |
3d4334446ae87eb59be5d024bef516bff66fb4c2dd3509c7a8c9f0cad44e4ebd
|