aioserial.py
aioserial.py copied to clipboard
Supports any serial protocol supported by the serial module
Usage:
import aioserial
import serial
import asyncio
loop = asyncio.get_event_loop()
serial_port = serial.serial_from_url("aioserial://<device>", loop=loop)
loop_port = serial.serial_from_url("aioserial://test?handler=loop", loop=loop)
alt_port = serial.serial_from_url("aioserial://<device>?handler=alt&class=<class>", loop=loop)
telnet_port = serial.serial_from_url("aioserial://<host>:<port>?handler=rfc2217", loop=loop)
async def test():
await loop_port.write_async(b"OK\n")
assert (await loop_port.readline_async()) == b"OK\n"
Thank for the contribution, @Governa. The idea looks great. Please allow me to take some time digesting the PR.
If this helps, I'm going to try explaining the code a bit.
I've tried to change as little as possible. Divided AioSerial in two. The first part, AioSerialMixin, is the old implementation of all methods. The AioSerial is there just so the constructor signature is fully documented and the API doesn't change.
The Mixin class has a generic constructor that extracts the loop parameter and forwards all other parameters to the super class.
The magic is done in protocol_serial.py. Here we find the needed serial class (using code shamelessly stolen from the serial module :) ) and create a new class using the class we found and the AioSerialMixin. The created class is named AioSerial[class name].
And finally, in the module init, we register this module as the handler of "aioserial://" URLs so that by importing aioserial this functionality is already working. This is the part I like the least, as it is a bit magical and inspecting the code, the import may seem not to be used.
Now there are conflicting changes in the repository. I could try to fix the conflicts, but only if you think this is mergeable
Hello to everyone,
Following the documentation that states that all of the pyserial's API can be called from your library, can you illustrate a quick example of a RS485 support? https://pyserial.readthedocs.io/en/latest/pyserial_api.html#rs485-support