Package: aioserial
aioserialQuick startA simple serial port readerpyserial-asyncio example replacementAPIAioSerialConstructorMethodsread_asyncread_until_asyncreadinto_asyncreadline_asyncreadlines_asyncwrite_asyncwritelines_asyncOther APIsWhy aioserial?A Python package that combinesasyncioandpySerial.Quick startA simple serial port readerimportasyncioimportaioserialasyncdefread_and_print(aioserial_instance:aioserial.AioSerial):whileTrue:print((awaitaioserial_instance.read_async()).decode(errors='ignore'),end='',flush=True)asyncio.run(read_and_print(aioserial.AioSerial(port='COM1')))pyserial-asyncio example replacementThe example usage from pyserial-asynciohttps://pyserial-asyncio.readthedocs.io/en/latest/shortintro.htmlimportasyncioimportserial_asyncioclassOutput(asyncio.Protocol):defconnection_made(self,transport):self.transport=transportprint('port opened',transport)transport.serial.rts=False# You can manipulate Serial object via transporttransport.write(b'Hello, World!\n')# Write serial data via transportdefdata_received(self,data):print('data received',repr(data))ifb'\n'indata:self.transport.close()defconnection_lost(self,exc):print('port closed')self.transport.loop.stop()defpause_writing(self):print('pause writing')print(self.transport.get_write_buffer_size())defresume_writing(self):print(self.transport.get_write_buffer_size())print('resume writing')loop=asyncio.get_event_loop()coro=serial_asyncio.create_serial_connection(loop,Output,'/dev/ttyUSB0',baudrate=115200)loop.run_until_complete(coro)loop.run_forever()loop.close()aioserial equivalenceimportasyncioimportaioserialasyncdefread_and_print(aioserial_instance:aioserial.AioSerial):whileTrue:data:bytes=awaitaioserial_instance.read_async()print(data.decode(errors='ignore'),end='',flush=True)ifb'\n'indata:aioserial_instance.close()breakaioserial_instance:aioserial.AioSerial=aioserial.AioSerial(port='/dev/ttyUSB0',baudrate=115200)asyncio.run(asyncio.gather(read_and_print(aioserial_instance),aioserial_instance.write_async(b'Hello, World!\n')))APIAioSerial>>>importaioserial>>>importserial>>>isinstance(aioserial.AioSerial(),serial.Serial)True>>>issubclass(aioserial.AioSerial,serial.Serial)True>>>aioserial.Serialisserial.SerialTrueConstructoraioserial_instance:aioserial.AioSerial=aioserial.AioSerial(# ... same with what can be passed to serial.Serial ...,loop:Optional[asyncio.AbstractEventLoop]=None,cancel_read_timeout:int=1,cancel_write_timeout:int=1)Methodsread_asyncbytes_read:bytes=\awaitaioserial_instance.read_async(size:int=1)read_until_asyncat_most_certain_size_of_bytes_read:bytes=\awaitaioserial_instance.read_until_async(expected:bytes=aioserial.LF,size:Optional[int]=None)readinto_asyncnumber_of_byte_read:int=\awaitaioserial_instance.readinto_async(b:Union[array.array,bytearray])readline_asynca_line_of_at_most_certain_size_of_bytes_read:bytes=\awaitaioserial_instance.readline_async(size:int=-1)readlines_asynclines_of_at_most_certain_size_of_bytes_read:bytes=\awaitaioserial_instance.readlines_async(hint:int=-1)write_asyncnumber_of_byte_like_data_written:int=\awaitaioserial_instance.write_async(bytes_like_data)writelines_asyncnumber_of_byte_like_data_in_the_given_list_written:int=\awaitaioserial_instance.writelines_async(list_of_bytes_like_data)Other APIsAll the other APIs in the mother packagepySerialare supported in aioserial as-is.Why aioserial?Want to use an asyncio-based but not a (self-built) thread-based serial library.pySerial-asynciodoesnot support Windows.APIs in all the other packages (pySerial-asyncio,asyncserial) that target the same goal are not designed in high level.