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.3.tar.gz (3.8 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.3-py3-none-any.whl (4.4 kB view details)

Uploaded Python 3

File details

Details for the file qrcode_scanner-0.0.3.tar.gz.

File metadata

  • Download URL: qrcode_scanner-0.0.3.tar.gz
  • Upload date:
  • Size: 3.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.10.12 Linux/6.5.0-45-generic

File hashes

Hashes for qrcode_scanner-0.0.3.tar.gz
Algorithm Hash digest
SHA256 5f127b380ced884a3cf17a345ebbe42b6010c462fc854efa3bf96f6c8067dcdb
MD5 b0c8c3b0f0d5f0ff7544750d01b96f9e
BLAKE2b-256 3f636d9d8f00911b7d7759d7357cc1d5765a78547759143454907be7adc7e497

See more details on using hashes here.

File details

Details for the file qrcode_scanner-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: qrcode_scanner-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 4.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.10.12 Linux/6.5.0-45-generic

File hashes

Hashes for qrcode_scanner-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 a1e73de0b3cf1bbf0f5d249c7d3f74254c65c14f844e94f74899c2aad432a0b0
MD5 1bbe0a5b000dd29b61b7ae183fc00309
BLAKE2b-256 9a62494916954e22b60772d9f927060cd242b91dc7ae2a35fc4caa212f1e84e6

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