-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_serial.py
40 lines (30 loc) · 850 Bytes
/
main_serial.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
import serial
from inputs.parse import Parser, MeterMessage
def main():
port = serial.Serial(
port='/dev/ttyS0',
baudrate=115200,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout=10,
)
parser = Parser()
while True:
line = port.readline()
if len(line) == 0:
print("Timeout")
parser.reset()
continue
try:
line_str = line.decode()
except UnicodeDecodeError:
print("Unicode decode error")
parser.reset()
continue
raw_msg = parser.push_line(line_str)
if raw_msg is not None and raw_msg.is_clean:
msg = MeterMessage.from_raw(raw_msg)
print(msg)
if __name__ == '__main__':
main()