
Security News
TC39 Advances 11 Proposals for Math Precision, Binary APIs, and More
TC39 advances 11 JavaScript proposals, with two moving to Stage 4, bringing better math, binary APIs, and more features one step closer to the ECMAScript spec.
A Python library for reading QR codes from both HID (Human Interface Device) scanners and Serial devices. This library supports multiple connection types to accommodate different QR code scanner configurations.
Install using pip:
pip install qrcode-scanner
Or using Poetry:
poetry add qrcode-scanner
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")
from qrcode_scanner import SerialScanner, serial_ports
from qrcode_scanner.exceptions import DeviceConnectionError, DeviceNotConnectedError
# List available serial ports
print("Available serial ports:")
ports = serial_ports()
for port in ports:
print(f" - {port}")
# Configure the serial connection
port = "/dev/ttyACM0" # Adjust for your system (Windows: "COM3", etc.)
baudrate = 9600
try:
# Create and connect to the serial scanner
scanner = SerialScanner(
port=port,
baudrate=baudrate,
timeout=1 # timeout in seconds
)
scanner.connect()
print(f"Connected to serial device on {port}")
while True:
print("Waiting for data...")
try:
# Read data from the scanner (blocking call)
scanned_text = scanner.read()
if scanned_text:
print("=== SCAN COMPLETE ===")
print("Scanned text:", scanned_text)
else:
print("No data received (timeout)")
except DeviceNotConnectedError:
print("Device not connected")
break
except DeviceConnectionError as e:
print(f"Device connection error: {e}")
break
except KeyboardInterrupt:
print("Program terminated by user")
except DeviceConnectionError as e:
print(f"Failed to connect to serial device: {e}")
finally:
scanner.close()
print("Serial device closed")
The SerialScanner
class supports various configuration options:
scanner = SerialScanner(
port="/dev/ttyACM0", # Serial port
baudrate=9600, # Baud rate (default: 9600)
parity=serial.PARITY_NONE, # Parity (default: NONE)
stopbits=serial.STOPBITS_ONE, # Stop bits (default: 1)
bytesize=serial.EIGHTBITS, # Byte size (default: 8)
timeout=1 # Read timeout in seconds (default: 1)
)
Common baud rates for QR scanners: 9600, 19200, 38400, 115200
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 (HID only)To find your HID device's vendor and product IDs:
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("---")
To discover available serial ports:
from qrcode_scanner import serial_ports
# List all available serial ports
ports = serial_ports()
for port in ports:
print(f"Available port: {port}")
Use HID Scanner when:
Use Serial Scanner when:
MIT License
Contributions are welcome! Please feel free to submit a Pull Request.
FAQs
Read QR codes from HID devices
We found that qrcode-scanner demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
TC39 advances 11 JavaScript proposals, with two moving to Stage 4, bringing better math, binary APIs, and more features one step closer to the ECMAScript spec.
Research
/Security News
A flawed sandbox in @nestjs/devtools-integration lets attackers run code on your machine via CSRF, leading to full Remote Code Execution (RCE).
Product
Customize license detection with Socket’s new license overlays: gain control, reduce noise, and handle edge cases with precision.