Skip to content

Instantly share code, notes, and snippets.

@PaulFurtado
Last active June 22, 2024 22:44
Show Gist options
  • Save PaulFurtado/fce98aef890469f34d51 to your computer and use it in GitHub Desktop.
Save PaulFurtado/fce98aef890469f34d51 to your computer and use it in GitHub Desktop.
Reset USB device from python
"""
Example code for resetting the USB port that a Teensy microcontroller is
attached to. There are a lot of situations where a Teensy or Arduino can
end up in a bad state and need resetting, this code is useful for
"""
import os
import fcntl
import subprocess
# Equivalent of the _IO('U', 20) constant in the linux kernel.
USBDEVFS_RESET = ord('U') << (4*2) | 20
def get_teensy():
"""
Gets the devfs path to a Teensy microcontroller by scraping the output
of the lsusb command
The lsusb command outputs a list of USB devices attached to a computer
in the format:
Bus 002 Device 009: ID 16c0:0483 Van Ooijen Technische Informatica Teensyduino Serial
The devfs path to these devices is:
/dev/bus/usb/<busnum>/<devnum>
So for the above device, it would be:
/dev/bus/usb/002/009
This function generates that path.
"""
proc = subprocess.Popen(['lsusb'], stdout=subprocess.PIPE)
out = proc.communicate()[0]
lines = out.split('\n')
for line in lines:
if 'Teensyduino' in line:
parts = line.split()
bus = parts[1]
dev = parts[3][:3]
return '/dev/bus/usb/%s/%s' % (bus, dev)
def send_reset(dev_path):
"""
Sends the USBDEVFS_RESET IOCTL to a USB device.
dev_path - The devfs path to the USB device (under /dev/bus/usb/)
See get_teensy for example of how to obtain this.
"""
fd = os.open(dev_path, os.O_WRONLY)
try:
fcntl.ioctl(fd, USBDEVFS_RESET, 0)
finally:
os.close(fd)
def reset_teensy():
"""
Finds a teensy and reset it.
"""
send_reset(get_teensy())
@rishta
Copy link

rishta commented Dec 25, 2016

#!/usr/bin/python
from usb.core import find as finddev
dev = finddev(idVendor=0x1234, idProduct=0x5678)
dev.reset()

@RichardBronosky
Copy link

@rishta That's a great hint. Thanks.

Note to others: You have to pip install pyusb. If you pip install usb you get the only the wheel and can't import it.

@CorniiDog
Copy link

What if the device isnt getting detected while connected? Any way to reset all USB ports and rescan?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment