7 def __init__(self, vendor_id, product_id):
8 self.vendor_id = vendor_id
9 self.product_id = product_id
19 def find_device(self):
20 self.dev = usb.core.find(idVendor=self.vendor_id, idProduct=self.product_id)
22 raise ValueError(f"Device {self.vendor_id}:{self.product_id} not found\nAre you sure it is connected?")
24 for config in self.dev:
26 if usb.util.find_descriptor(config, custom_match=lambda d: (d.bInterfaceClass == 0x02 or d.bInterfaceClass == 0xFF)):
27 print("Found serial device with 0x02 | 0xFF")
28 if self.dev.is_kernel_driver_active(intf.bInterfaceNumber):
29 print("Found kernel driver, detaching ... ")
30 self.dev.detach_kernel_driver(intf.bInterfaceNumber)
32 if (ep.bmAttributes == 0x02):
33 if ep.bEndpointAddress == self.ep_in:
36 elif ep.bEndpointAddress == self.ep_out:
41 raise ValueError("Device does not have a serial interface")
43 def establish_connection(self):
44 for i in range(self.max_tries):
46 self.tx_ep.write(bytes([0x00]), self.timeout_ms)
47 ret = self.rx_ep.read(1, self.timeout_ms)
51 except usb.core.USBError as e:
52 print(f"Timeout: retry #{i}", e)
55 def confirm_connection(self):
56 for i in range(self.max_tries):
58 self.tx_ep.write(bytes([0x55]), self.timeout_ms)
59 ret = self.rx_ep.read(1, self.timeout_ms)
63 except usb.core.USBError as e:
64 print(f"Timeout: retry #{i}", e)
68 communicator = RAConnect(vendor_id=0x1a86, product_id=0x7523)
70 if not communicator.establish_connection():
71 print("Cannot connect")
74 if not communicator.confirm_connection():
75 print("Failed to confirm boot code")