|
| 1 | +# auart_hd.py |
| 2 | +# Author: Peter Hinch |
| 3 | +# Copyright Peter Hinch 2018 Released under the MIT license |
| 4 | + |
| 5 | +# Demo of running a half-duplex protocol to a device. The device never sends |
| 6 | +# unsolicited messages. An example is a communications device which responds |
| 7 | +# to AT commands. |
| 8 | +# The master sends a message to the device, which may respond with one or more |
| 9 | +# lines of data. The master assumes that the device has sent all its data when |
| 10 | +# a timeout has elapsed. |
| 11 | + |
| 12 | +# In this test a physical device is emulated by the DEVICE class |
| 13 | +# To test link X1-X4 and X2-X3 |
| 14 | + |
| 15 | +from pyb import UART |
| 16 | +import uasyncio as asyncio |
| 17 | +import aswitch |
| 18 | + |
| 19 | +# Dummy device waits for any incoming line and responds with 4 lines at 1 second |
| 20 | +# intervals. |
| 21 | +class DEVICE(): |
| 22 | + def __init__(self, uart_no = 4): |
| 23 | + self.uart = UART(uart_no, 9600) |
| 24 | + self.loop = asyncio.get_event_loop() |
| 25 | + self.swriter = asyncio.StreamWriter(self.uart, {}) |
| 26 | + self.sreader = asyncio.StreamReader(self.uart) |
| 27 | + loop = asyncio.get_event_loop() |
| 28 | + loop.create_task(self._run()) |
| 29 | + |
| 30 | + async def _run(self): |
| 31 | + responses = ['Line 1', 'Line 2', 'Line 3', 'Goodbye'] |
| 32 | + while True: |
| 33 | + res = await self.sreader.readline() |
| 34 | + for response in responses: |
| 35 | + await self.swriter.awrite("{}\r\n".format(response)) |
| 36 | + # Demo the fact that the master tolerates slow response. |
| 37 | + await asyncio.sleep_ms(300) |
| 38 | + |
| 39 | +# The master's send_command() method sends a command and waits for a number of |
| 40 | +# lines from the device. The end of the process is signified by a timeout, when |
| 41 | +# a list of lines is returned. This allows line-by-line processing. |
| 42 | +# A special test mode demonstrates the behaviour with a non-responding device. If |
| 43 | +# None is passed, no commend is sent. The master waits for a response which never |
| 44 | +# arrives and returns an empty list. |
| 45 | +class MASTER(): |
| 46 | + def __init__(self, uart_no = 2, timeout=4000): |
| 47 | + self.uart = UART(uart_no, 9600) |
| 48 | + self.timeout = timeout |
| 49 | + self.loop = asyncio.get_event_loop() |
| 50 | + self.swriter = asyncio.StreamWriter(self.uart, {}) |
| 51 | + self.sreader = asyncio.StreamReader(self.uart) |
| 52 | + self.delay = aswitch.Delay_ms() |
| 53 | + self.response = [] |
| 54 | + loop = asyncio.get_event_loop() |
| 55 | + loop.create_task(self._recv()) |
| 56 | + |
| 57 | + async def _recv(self): |
| 58 | + while True: |
| 59 | + res = await self.sreader.readline() |
| 60 | + self.response.append(res) # Append to list of lines |
| 61 | + self.delay.trigger(self.timeout) # Got something, retrigger timer |
| 62 | + |
| 63 | + async def send_command(self, command): |
| 64 | + self.response = [] # Discard any pending messages |
| 65 | + if command is None: |
| 66 | + print('Timeout test.') |
| 67 | + else: |
| 68 | + await self.swriter.awrite("{}\r\n".format(command)) |
| 69 | + print('Command sent:', command) |
| 70 | + self.delay.trigger(self.timeout) # Re-initialise timer |
| 71 | + while self.delay.running(): |
| 72 | + await asyncio.sleep(1) # Wait for 4s after last msg received |
| 73 | + return self.response |
| 74 | + |
| 75 | +async def test(): |
| 76 | + print('This test takes 10s to complete.') |
| 77 | + for cmd in ['Run', None]: |
| 78 | + print() |
| 79 | + res = await master.send_command(cmd) |
| 80 | + # can use b''.join(res) if a single string is required. |
| 81 | + if res: |
| 82 | + print('Result is:') |
| 83 | + for line in res: |
| 84 | + print(line.decode('UTF8'), end='') |
| 85 | + else: |
| 86 | + print('Timed out waiting for result.') |
| 87 | + |
| 88 | +loop = asyncio.get_event_loop() |
| 89 | +master = MASTER() |
| 90 | +device = DEVICE() |
| 91 | +loop.run_until_complete(test()) |
| 92 | + |
| 93 | +# Expected output |
| 94 | +# >>> import auart_hd |
| 95 | +# This test takes 10s to complete. |
| 96 | +# |
| 97 | +# Command sent: Run |
| 98 | +# Result is: |
| 99 | +# Line 1 |
| 100 | +# Line 2 |
| 101 | +# Line 3 |
| 102 | +# Goodbye |
| 103 | +# |
| 104 | +# Timeout test. |
| 105 | +# Timed out waiting for result. |
| 106 | +# >>> |
0 commit comments