aioserial.py icon indicating copy to clipboard operation
aioserial.py copied to clipboard

Supports any serial protocol supported by the serial module

Open Governa opened this issue 5 years ago • 4 comments

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"

Governa avatar Jul 19 '19 00:07 Governa

Thank for the contribution, @Governa. The idea looks great. Please allow me to take some time digesting the PR.

mrjohannchang avatar Jul 29 '19 03:07 mrjohannchang

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.

Governa avatar Jul 31 '19 06:07 Governa

Now there are conflicting changes in the repository. I could try to fix the conflicts, but only if you think this is mergeable

Governa avatar Nov 11 '19 22:11 Governa

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

skyareyes avatar Mar 28 '20 00:03 skyareyes