You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
PyCH9329-HID is a high-level Python package for CH9329 HID chip, designed to provide seamless, OS-independent peripheral control via serial communication.
π― Target Use Cases
Use Case
Description
AI Vision Agents
Perfect for models like UI-TARS or Claude Computer Use that require high-fidelity interaction with a screen based on visual coordinates
Test Automation
Reliable UI testing on macOS/Windows/Linux without requiring OS-level accessibility permissions
Embedded Robotics
Controlling computers via Raspberry Pi or ESP32 using a physical CH9329 USB dongle
π¦ Installation
pip install pych9329-hid
Requirements
Python >= 3
pyserial
Import Options
You can use either of the full package name or shorter alias:
# Option 1: Full package namefrompych9329_hidimportHIDController# Option 2: Short alias (recommended)frompych9329importHIDController
Both options work identically - choose whichever you prefer!
frompych9329importCH9329, SerialTransportwithSerialTransport(port='/dev/ttyUSB0', baudrate=115200) astransport:
ch9329=CH9329(transport)
# Get current configurationconfig=ch9329.get_config()
# CH9329Config:# Work Mode: Keyboard+Mouse (Hardware)# Serial Mode: Protocol mode (Hardware)# Address: 0x00# Baud Rate: 9600# Packet Interval: 3ms# VID: 0x861A# PID: 0x29E1# Keyboard Submit Interval: 0ms# Keyboard Release Delay: 1ms# Auto Enter Flag: 0# Enter Characters: 0d00000000000000# Filter Strings: 0000000000000000# Custom Descriptor Enable: {'vendor': True, 'product': False, 'sn': False}# Keyboard Fast Submission: 0# Modify configurationconfig.baudrate=9600config.vid=0x1234config.pid=0x5678# Enable vendor and product string descriptorsconfig.custom_descriptor_enable= {'vendor': True, 'product': True, 'sn': False}
# Apply configurationifch9329.set_config(config):
print("Configuration updated successfully!")
# Set USB descriptorsch9329.set_usb_descriptor(0x00, "My Company") # Vendorch9329.set_usb_descriptor(0x01, "CH9329 Keyboard") # Productch9329.set_usb_descriptor(0x02, "SN-12345") # Serial Number# Read back USB descriptorsvendor=ch9329.get_usb_descriptor(0x00)
product=ch9329.get_usb_descriptor(0x01)
serial=ch9329.get_usb_descriptor(0x02)
print(f"Vendor: {vendor}, Product: {product}, SN: {serial}")
# Restore factory defaultsifch9329.set_config_to_default():
print("Factory settings restored!")
# Reset chipch9329.chip_reset()
π API Reference
CH9329 (Low-Level Protocol)
Device Information
Method
Description
Returns
get_info()
Get device information
dict with keys: version, usb_connected, num_lock_on, caps_lock_on, scroll_lock_on
Configuration Management
Method
Description
Returns
get_config()
Get device configuration
CH9329Config object or None
set_config(config)
Set device configuration
bool - True if successful
set_config_to_default()
Restore factory default settings
bool - True if successful
chip_reset()
Perform software reset
bool - True if successful
USB String Descriptors
Method
Description
Returns
get_usb_descriptor(type)
Get USB string descriptor
str or None
set_usb_descriptor(type, string)
Set USB string descriptor
bool - True if successful
USB Descriptor Types:
0x00: Vendor string descriptor
0x01: Product string descriptor
0x02: Serial number string descriptor
Keyboard Operations
Method
Description
Returns
send_keyboard(modifier, keycodes)
Send keyboard report
bool - True if successful
Mouse Operations
Method
Description
Returns
send_mouse_rel(dx, dy, buttons, wheel)
Send relative mouse movement
bool - True if successful
send_mouse_abs(x, y, buttons, wheel)
Send absolute mouse position
bool - True if successful
β οΈ Error Handling
CH9329 Layer
The CH9329 protocol layer uses a hybrid error handling strategy:
Soft Errors (timeout, ACK error): Return False with a warning
Device doesn't respond within timeout
Device returns error status in ACK
Hard Errors (transport failure): Raise SerialTransportError
Serial port disconnected
Serial port read/write failure
Invalid serial port configuration
HIDController Layer
HIDController does not check return values from CH9329 - failures propagate naturally to the caller. This allows users to decide how to handle errors based on their use case.
SerialTransport Layer
The transport layer provides custom exceptions:
SerialTransportError: Base exception for transport errors
SerialTransportClosedError: Raised when operation attempted on closed transport