-
Notifications
You must be signed in to change notification settings - Fork 0
/
USBSocket.py
40 lines (31 loc) · 1.08 KB
/
USBSocket.py
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
import time
from datetime import datetime
import HID
import BTCommands
import Constants
def TransmitData(handle, buffer):
success = False
if (HID.GetNumHidDevices() > 0):
try:
success = HID.TransmitData(handle, buffer)
except Exception as err:
print(f"USBSocket: Transmission error {err=}, {type(err)=}")
success = False
return success
def ReceiveData(handle, bufferSize, timeout):
success = False
try:
bytesRead = 0
timer = time.monotonic()
while bytesRead == 0:
success, result = HID.ReceiveData(handle, bufferSize, timeout)
bytesRead = len(result)
timeElapsed = (time.monotonic() - timer) * 1000
if (not success) or (timeElapsed > timeout):
# print("USBSocket: recive timeout")
success = False
break
time.sleep(Constants.TIMER_READ * 0.001)
except Exception as err:
print(f"USBSocket: unexpected {err=}, {type(err)=}")
return success, result