Skip to main content

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 found
  • DeviceConnectionError: When there are issues connecting to the device
  • DeviceNotConnectedError: When trying to read from a disconnected device
  • DeviceReadError: When reading from the device fails
  • UnknownCharacterError: 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

qrcode_scanner-0.0.2.tar.gz (3.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

qrcode_scanner-0.0.2-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

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

Hashes for qrcode_scanner-0.0.2.tar.gz
Algorithm Hash digest
SHA256 8a1164edf8ff64308380c3c5cf42c8baaa60823a1d50dda7d1cb8b585d89952b
MD5 e7b8d73a50107f12fd5a1d85459c9f6e
BLAKE2b-256 be92d2f0d7c18b4214a752580c9e046af96398f87095e96fe8d3522577945d08

See more details on using hashes here.

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

Hashes for qrcode_scanner-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 dff924f4c1eb58b83707fe72f96435ae200b7e757c07c687c0a98c9a8fef92c5
MD5 d61a802929fbb46ce447f642bebffd79
BLAKE2b-256 3d4334446ae87eb59be5d024bef516bff66fb4c2dd3509c7a8c9f0cad44e4ebd

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page