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:
|
4
|
29 reader: asyncio.StreamReader
|
|
30 writer: asyncio.StreamWriter
|
3
|
31
|
4
|
32 def __init__(self, com_port='/dev/ttyACM0', baud_rate=115200):
|
|
33 self.conn = serial_asyncio.open_serial_connection(
|
|
34 url=com_port, baudrate=baud_rate, timeout=1,
|
|
35 writeTimeout=1)
|
3
|
36
|
|
37 self.com_port = com_port
|
|
38
|
4
|
39 async def open(self):
|
|
40 self.reader, self.writer = await self.conn
|
3
|
41
|
4
|
42 async def write(self, data: bytes):
|
|
43 self.writer.write(data)
|
|
44 await self.writer.drain()
|
3
|
45
|
|
46 async def read(self, size=1):
|
4
|
47 return await self.reader.readexactly(size)
|