-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSerialBin.py
78 lines (62 loc) · 1.86 KB
/
SerialBin.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import serial
import threading
import time
class Fore:
MAGENTA = "\033[35m"
red = "\033[31m"
reset = "\033[m"
class Receive(threading.Thread):
def __init__(self, ser):
super().__init__()
self.serial = ser
self.cont = True
def stop(self):
self.cont = False
def run(self):
actual_ms = int(time.time() * 1000)
received = True
while self.cont:
if self.serial.in_waiting > 0:
byte_received = bytes(self.serial.read())
x = int.from_bytes(byte_received, byteorder="big")
print(Fore.MAGENTA + "{:02X}".format(x) + Fore.reset, end=" ", flush=True)
received = True
actual_ms = int(time.time() * 1000)
elif (int(time.time() * 1000) - actual_ms) > 1000 and received:
print("")
received = False
class Send(threading.Thread):
def __init__(self, ser):
super().__init__()
self.serial = ser
self.cont = True
def stop(self):
self.cont = False
def run(self):
while self.cont:
numbers = input("")
number_list = numbers.split(" ")
ind = []
try:
for n in number_list:
if len(n) != 2:
raise ValueError
n = int(n, 16)
ind.append(n)
ind_b = bytearray(ind)
self.serial.write(ind_b)
except ValueError:
print(Fore.red + "Invalid input!" + Fore.reset)
s = serial.Serial("/dev/serial0")
s.baudrate = 9600
# s.timeout = 1
send_thread = Send(s)
receive_thread = Receive(s)
send_thread.start()
receive_thread.start()
try:
send_thread.join()
receive_thread.join()
except KeyboardInterrupt:
send_thread.stop()
receive_thread.stop()