-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCH340-interactive.py
More file actions
executable file
·58 lines (40 loc) · 1.31 KB
/
CH340-interactive.py
File metadata and controls
executable file
·58 lines (40 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env python3
# QinHeng Electronics CH-340 / HL-340 USB relay manual toggle script
# By Dan MacDonald, 2021
# You must install the python USB modules to run this:
# sudo apt install python-usb
# or
# pacman -S python-pyusb
# To run with pyusb debugging:
#
# PYUSB_DEBUG=debug python relay.py
import os, sys, subprocess, usb.core, usb.util
# This script must be run as root!
if not os.geteuid()==0:
sys.exit('This script must be run as root or as a sudo user.')
if len(sys.argv) != 2:
sys.exit('You must provide an option, -on or -off')
dev = usb.core.find(idVendor=0x1a86, idProduct=0x7523)
if dev is None:
raise ValueError("Device not found")
if dev.is_kernel_driver_active(0):
dev.detach_kernel_driver(0)
cfg = dev.get_active_configuration()
intf = cfg[(0,0)]
ep = usb.util.find_descriptor(
intf,
# match the first OUT endpoint
custom_match = \
lambda e: \
usb.util.endpoint_direction(e.bEndpointAddress) == \
usb.util.ENDPOINT_OUT)
assert ep is not None
close_relay_cmd = [0xA0, 0x01, 0x01, 0xA2]
open_relay_cmd = [0xA0, 0x01, 0x00, 0xA1]
if __name__ == '__main__':
if sys.argv[1] == '-on':
ep.write(close_relay_cmd)
elif sys.argv[1] == '-off':
ep.write(open_relay_cmd)
else:
print('Unknown option', sys.argv[1])