-
Notifications
You must be signed in to change notification settings - Fork 0
/
raspberry.consumer.py
79 lines (58 loc) · 1.56 KB
/
raspberry.consumer.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
79
#
# Author Phil Hall
# August 2018 - Public Domain
import posix
import termios
import time
DELAY=0.1
def main():
Raise=False
try:
fd=posix.open("/dev/serial0", posix.O_RDWR |\
posix.O_NOCTTY | posix.O_NDELAY)
except:
Raise=True
if Raise:
raise Exception("UART busy."
" Try /dev/serial1, /dev/ttyS0,"
" /dev/ttyAMA0.")
options=termios.tcgetattr(fd)
options[0]=termios.IGNPAR
options[1]=0
options[2]=termios.B9600 |\
termios.CS8 |\
termios.CLOCAL |\
termios.CREAD
options[3]=0
termios.tcflush(fd,termios.TCIFLUSH)
termios.tcsetattr(fd,termios.TCSANOW,options)
while True:
message="Consumer :"
print(message,end="\r"+message)
data=None
message=None
while data is None:
try:
data=bytearray(posix.read(fd,128))
except:
data=None
time.sleep(DELAY)
continue
if message is not None and data is None:
break
if message is None:
message=data
else:
message+=data
time.sleep(DELAY)
new=""
for i in message:
new+=chr(i)
message=new
print('"'+message+'"')
posix.close(fd)
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
pass