-
Notifications
You must be signed in to change notification settings - Fork 11
/
async_serial_protocol_queue.py
86 lines (73 loc) · 2.46 KB
/
async_serial_protocol_queue.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
80
81
82
83
84
85
86
import asyncio
import serial
import serial_asyncio
from functools import partial
class Reader(asyncio.Protocol):
def __init__(self, q):
"""Store the queue.
"""
super().__init__()
self.transport = None
self.buf = None
self.msgs_recvd = None
self.q = q
def connection_made(self, transport):
"""Store the serial transport and prepare to receive data.
"""
self.transport = transport
self.buf = bytes()
self.msgs_recvd = 0
print('Reader connection created')
def data_received(self, data):
"""Store characters until a newline is received.
"""
self.buf += data
if b'\n' in self.buf:
lines = self.buf.split(b'\n')
self.buf = lines[-1] # whatever was left over
for line in lines[:-1]:
asyncio.ensure_future(self.q.put(line))
self.msgs_recvd += 1
if self.msgs_recvd == 4:
self.transport.close()
def connection_lost(self, exc):
print('Reader closed')
class Writer(asyncio.Protocol):
def connection_made(self, transport):
"""Store the serial transport and schedule the task to send data.
"""
self.transport = transport
self.buf = bytes()
print('Writer connection created')
asyncio.ensure_future(self.send())
print('Writer.send() scheduled')
def connection_lost(self, exc):
print('Writer closed')
async def send(self):
"""Send four newline-terminated messages, one byte at a time.
"""
message = b'foo\nbar\nbaz\nqux\n'
for b in message:
await asyncio.sleep(0.5)
self.transport.serial.write(bytes([b]))
print(f'Writer sent: {bytes([b])}')
self.transport.close()
async def print_received(loop, q):
counter = 0
while counter < 4:
msg = await q.get()
print(f'Message received: {msg}')
counter += 1
loop.stop()
queue = asyncio.Queue()
reader_partial = partial(Reader, queue)
loop = asyncio.get_event_loop()
reader = serial_asyncio.create_serial_connection(loop, reader_partial, 'reader', baudrate=115200)
writer = serial_asyncio.create_serial_connection(loop, Writer, 'writer', baudrate=115200)
asyncio.ensure_future(reader)
print('Reader scheduled')
asyncio.ensure_future(writer)
print('Writer scheduled')
asyncio.ensure_future(print_received(loop, queue))
loop.run_forever()
print('Done')