'How to write a Python async serial async reader?
I am porting some C++ code to Python and I am having a heck of a time figuring out how to have an onReceive handler for serial bytes. I am using
import serial_asyncio
class Arduino: #comms is with an Arduino in serial mode)
async def connect(self, serial_port):
(self.serial_reader, self.serial_writer) = await
serial_asyncio.open_serial_connection(url=serial_port, baudrate=115200)
print("serial opened:", serial_port)
self.buffer = b""
async def recv(self, data):
self.buffer += data
self.process_buffer(self.buffer)
if __name__ == '__main__':
ardunio = Arduino("/dev/ttyS0")
loop = asyncio.get_event_loop()
loop.run_until_complete(ardunio.connect())
loop.run_forever()
However I cannot figure out how to patch the recv handler in to the read. I Qt, I could:
connect(&QAbstractSocket::readyRead, &Arduino::handleBytes);
In Node:
arduino.on('data', line => console.log(line))
In Python, there doesn't seem to be any obvious answer? How do I get the serial port bytes that arrive to be passed into Arduino.receive(self, data)?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
