3
|
1 # -*- coding: utf-8 -*-
|
|
2 """
|
|
3 Copyright (c) 2015-2020 Alan Yorinks All rights reserved.
|
|
4
|
|
5 This program is free software; you can redistribute it and/or
|
|
6 modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
|
|
7 Version 3 as published by the Free Software Foundation; either
|
|
8 or (at your option) any later version.
|
|
9 This library is distributed in the hope that it will be useful,
|
|
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
12 General Public License for more details.
|
|
13
|
|
14 You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
|
|
15 along with this library; if not, write to the Free Software
|
|
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
17 """
|
|
18
|
|
19 import asyncio
|
4
|
20 import logging
|
3
|
21
|
4
|
22 import serial_asyncio
|
|
23
|
|
24 log = logging.getLogger('serial')
|
3
|
25
|
|
26
|
|
27 # noinspection PyStatementEffect,PyUnresolvedReferences,PyUnresolvedReferences
|
|
28 class TelemetrixAioSerial:
|
|
29 """
|
|
30 This class encapsulates management of the serial port that communicates
|
|
31 with the Arduino Firmata
|
|
32 It provides a 'futures' interface to make Pyserial compatible with asyncio
|
|
33 """
|
|
34
|
4
|
35 reader: asyncio.StreamReader
|
|
36 writer: asyncio.StreamWriter
|
3
|
37
|
4
|
38 def __init__(self, com_port='/dev/ttyACM0', baud_rate=115200):
|
|
39 self.conn = serial_asyncio.open_serial_connection(
|
|
40 url=com_port, baudrate=baud_rate, timeout=1,
|
|
41 writeTimeout=1)
|
3
|
42
|
|
43 self.com_port = com_port
|
|
44
|
4
|
45 async def open(self):
|
|
46 self.reader, self.writer = await self.conn
|
3
|
47
|
4
|
48 async def write(self, data: bytes):
|
|
49 self.writer.write(data)
|
|
50 await self.writer.drain()
|
3
|
51
|
|
52 async def read(self, size=1):
|
4
|
53 return await self.reader.readexactly(size)
|