diff options
author | Chris Liechti <cliechti@gmx.net> | 2015-09-03 23:49:48 +0200 |
---|---|---|
committer | Chris Liechti <cliechti@gmx.net> | 2015-09-03 23:49:48 +0200 |
commit | 1f0c9b4173094ee65b7465a90f1d92aa668ebf30 (patch) | |
tree | 542545dc7a0d5708045bf2ef70fc0c938a5ab5b1 /documentation/pyserial_api.rst | |
parent | d8137cc4bc3436e0d17fb86e7718b930db5732e7 (diff) | |
download | pyserial-git-1f0c9b4173094ee65b7465a90f1d92aa668ebf30.tar.gz |
doc update: add note about asyncio
Diffstat (limited to 'documentation/pyserial_api.rst')
-rw-r--r-- | documentation/pyserial_api.rst | 49 |
1 files changed, 48 insertions, 1 deletions
diff --git a/documentation/pyserial_api.rst b/documentation/pyserial_api.rst index 36293ad..85dd53a 100644 --- a/documentation/pyserial_api.rst +++ b/documentation/pyserial_api.rst @@ -1143,10 +1143,57 @@ Examples: - ``hwgrep://0451:f432`` (USB VID:PID) - ``spy://COM54?file=log.txt`` + +asyncio +======= + +.. warning:: This implementation is currently in an experimental state. Use + at your own risk. + +Experimental asyncio support is available for Python 3.4 and newer. The +:mod:`serial.aio` provides a :class:`asyncio.Transport`: +``SerialTransport``. + + +A factory function (`asyncio.coroutine`) is provided: + +.. function:: create_serial_connection(loop, protocol_factory, \*args, \*\*kwargs) + + :param loop: The event handler + :param protocol_factory: Factory function for a :class:`asyncio.Protocol` + :param args: Passed to the :class:`serial.Serial` init function + :param kwargs: Passed to the :class:`serial.Serial` init function + :platform: Posix + + Get a connection making coroutine. + +Example:: + + class Output(asyncio.Protocol): + def connection_made(self, transport): + self.transport = transport + print('port opened', transport) + transport.serial.setRTS(0) + transport.write(b'hello world\n') + + def data_received(self, data): + print('data received', repr(data)) + self.transport.close() + + def connection_lost(self, exc): + print('port closed') + asyncio.get_event_loop().stop() + + loop = asyncio.get_event_loop() + coro = create_serial_connection(loop, Output, '/dev/ttyUSB0', baudrate=115200) + loop.run_until_complete(coro) + loop.run_forever() + loop.close() + + Tools ===== - serial.tools.list_ports ----------------------- .. module:: serial.tools.list_ports |