RAWHID and PyUSB

Status
Not open for further replies.

DrM

Well-known member
Here is a simple Python program using only PyUSB to talk to a Teensy running RAWHID

Code:
#!/usr/bin/python

import usb.core
import usb.util

dev = usb.core.find(idVendor=0x16C0, idProduct=0x0486)
    
try:
    dev.reset()
except Exception as e:
    print( 'reset', e)

if dev.is_kernel_driver_active(0):
    print( 'detaching kernel driver')
    dev.detach_kernel_driver(0)

endpoint_in = dev[0][(0,0)][0]
endpoint_out = dev[0][(0,0)][1]

# Send a command to the Teensy
endpoint_out.write( "version".encode() + bytes([0]) )

# Read the response, it is an array, of byte    
buffer = dev.read(endpoint_in.bEndpointAddress, 64, 1000)    
buffer = bytearray(buffer)

# Decode and print the zero terminated string response
n = buffer.index(0)
print( bytearray(buffer[:n]).decode() )
 
Status
Not open for further replies.
Back
Top