-
Notifications
You must be signed in to change notification settings - Fork 2
/
bus.py
executable file
·53 lines (44 loc) · 1.4 KB
/
bus.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
#!/usr/bin/python
import serial
import threading
import struct
import time
class Serial:
def __init__(self):
self.lock = threading.Lock()
self.connection = serial.Serial(
port = '/dev/ttyAMA0',
baudrate = 57600,
parity = serial.PARITY_NONE,
stopbits = serial.STOPBITS_ONE,
bytesize = serial.EIGHTBITS,
timeout = 1
)
def write(self, command):
with self.lock:
print('-> %s' % str(command))
self.connection.write(str(command))
return self.read() if command.replies() else time.sleep(0.05)
def read(self):
retry = 0
self.connection.reset_input_buffer()
while retry < 1000:
if self.connection.in_waiting > 0:
line = self.connection.readline().strip()
if '\t' in line:
print(line)
return line.split('\t')
time.sleep(0.05)
retry += 1
class SerialCommand:
def __init__(self, name, *args):
self.token = name[0]
self.name = name
self.args = args
def __str__(self):
if self.token in ['l', 'i']:
return self.name + struct.pack('b' * len(self.args), *self.args)
else:
return self.name + ' '.join(list(map(str, self.args)))
def replies(self):
return self.token in ['j']