Created
July 6, 2021 12:22
-
-
Save alacret/a5d80ff2a6a6c76e546724fdafa41ae3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import serial | |
import io | |
import time | |
import os | |
# if __name__ == '__main__': | |
# try: | |
# # configure the serial connections (the parameters differs on the device you are connecting to) | |
# with serial.Serial(port='/dev/ttys000', baudrate=9600, timeout=1, | |
# xonxoff=False, rtscts=False, dsrdtr=True) as s: | |
# while True: | |
# for line in s: | |
# print(line) | |
# | |
# except Exception as e: | |
# print('Program exit !', e) | |
# pass | |
# finally: | |
# pass | |
import usb.core | |
import usb.util | |
import sys | |
# device = usb.core.find(idVendor=0x2808, idProduct=0x93a9) # dongle | |
device = usb.core.find(idVendor=0x2009, idProduct=0x7638) # cable | |
# device = usb.core.find(idVendor=0x046d, idProduct=0xc31c) | |
# if device is None: | |
# raise ValueError('Device not found') | |
# else: | |
# # print(device) | |
# device.set_configuration() | |
for cfg in device: | |
for intf in cfg: | |
print(device.is_kernel_driver_active(intf.bInterfaceNumber)) | |
if device.is_kernel_driver_active(intf.bInterfaceNumber): | |
try: | |
device.detach_kernel_driver(intf.bInterfaceNumber) | |
print("Detach kernel") | |
except usb.core.USBError as e: | |
sys.exit( | |
"Could not detatch kernel driver from interface({0}): {1}".format(intf.bInterfaceNumber, str(e))) | |
else: | |
print("Kernel Driver is not active") | |
endpoint = device[0][(0, 0)][0] | |
print("ENDPOINT", endpoint) | |
try: | |
device.write(endpoint, "test") | |
result = (device.read(0x81, 100000, 1000)) | |
print(result) | |
except Exception as e: | |
print("ERROR:", e) | |
while True: | |
try: | |
data = device.read(endpoint.bEndpointAddress, endpoint.wMaxPacketSize) | |
print("READING:", data) | |
except usb.core.USBError as e: | |
# print("ERROR:", e) | |
data = None | |
if e.args == ('Operation timed out',): | |
continue |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@Silverfran