|
2 | 2 | pySerial-asyncio API
|
3 | 3 | ======================
|
4 | 4 |
|
5 |
| -asyncio |
6 |
| -======= |
7 |
| - |
8 | 5 | .. module:: serial_asyncio
|
9 | 6 |
|
10 |
| -.. warning:: This implementation is currently in an experimental state. Use |
11 |
| - at your own risk. |
12 | 7 |
|
13 |
| -Experimental asyncio support is available for Python 3.4 and newer. The module |
14 |
| -:mod:`serial_asyncio` provides a :class:`asyncio.Transport`: |
15 |
| -``SerialTransport``. |
| 8 | +The following high-level functions are provided for initiating a serial connection: |
| 9 | + |
| 10 | +.. function:: create_serial_connection(loop, protocol_factory, *args, **kwargs) |
| 11 | + :async: |
| 12 | + |
| 13 | + Open a streaming connection to the specified serial port. |
| 14 | + |
| 15 | + :param loop: The *asyncio* event loop |
| 16 | + :param protocol_factory: A callable which when invoked without arguments and which should |
| 17 | + return a :class:`asyncio.Protocol` subclass. Most commonly the protocol *class* |
| 18 | + (*e.g.* ``MyProtocol``) can be passed as this argument. If it is required to use an |
| 19 | + existing protocol *instance*, pass a zero-argument lambda which evaluates to the instance, |
| 20 | + such as ``lambda: my_protocol`` |
| 21 | + :param args: Forwarded to the :class:`serial.Serial` constructor |
| 22 | + :param kwargs: Forwarded to the :class:`serial.Serial` constructor |
| 23 | + :returns: A coroutine for managing a serial port connection, which when |
| 24 | + awaited returns transport and protocol instances. |
| 25 | + :platform: Posix |
| 26 | + |
| 27 | + Use this function to associate an asynchronous call-back based protocol with an |
| 28 | + new :class:`serial.Serial` instance that will be created on your behalf. |
| 29 | + |
| 30 | + The chronological order of the operation is: |
| 31 | + |
| 32 | + 1. ``protocol_factory`` is called without arguments and must return |
| 33 | + a :class:`asyncio.Protocol` subclass instance. |
| 34 | + |
| 35 | + 2. The protocol instance is tied to a :class:`~serial_asyncio.SerialTransport`. |
16 | 36 |
|
| 37 | + 3. This coroutine returns successfully with a ``(transport, protocol)`` pair. |
17 | 38 |
|
18 |
| -A factory function (`asyncio.coroutine`) is provided: |
| 39 | + 4. The :meth:`~serial_asyncio.SerialTransport.connection_made()` method of the protocol |
| 40 | + will be called at some point by the event loop. |
19 | 41 |
|
20 |
| -.. function:: create_serial_connection(loop, protocol_factory, \*args, \*\*kwargs) |
21 | 42 |
|
22 |
| - :param loop: The event handler |
23 |
| - :param protocol_factory: Factory function for a :class:`asyncio.Protocol` |
24 |
| - :param args: Passed to the :class:`serial.Serial` init function |
25 |
| - :param kwargs: Passed to the :class:`serial.Serial` init function |
| 43 | + |
| 44 | +.. function:: connection_for_serial(loop, protocol_factory, serial_instance) |
| 45 | + :async: |
| 46 | + |
| 47 | + Open a streaming connection to an existing serial port instance. |
| 48 | + |
| 49 | + :param loop: The *asyncio* event loop |
| 50 | + :param protocol_factory: A callable which when invoked without arguments and which should |
| 51 | + return a :class:`asyncio.Protocol` subclass. Most commonly the protocol *class* |
| 52 | + (*e.g.* ``MyProtocol``) can be passed as this argument. If it is required to use an |
| 53 | + existing protocol *instance*, pass a zero-argument lambda which evaluates to the instance, |
| 54 | + such as ``lambda: my_protocol`` |
| 55 | + :param serial_instance: A :class:`serial.Serial` instance. |
| 56 | + :returns: A coroutine for managing a serial port connection, which when |
| 57 | + awaited returns transport and protocol instances. |
26 | 58 | :platform: Posix
|
27 | 59 |
|
28 |
| - Get a connection making coroutine. |
| 60 | + Use this function to associate an asynchronous call-back based protocol with an |
| 61 | + existing :class:`serial.Serial` instance. |
| 62 | + |
| 63 | + The chronological order of the operation is: |
29 | 64 |
|
30 |
| -Example:: |
| 65 | + 1. ``protocol_factory`` is called without arguments and must return |
| 66 | + a :class:`asyncio.Protocol` subclass instance. |
31 | 67 |
|
32 |
| - import asyncio |
33 |
| - import serial_asyncio |
34 |
| - |
35 |
| - |
36 |
| - class Output(asyncio.Protocol): |
37 |
| - def connection_made(self, transport): |
38 |
| - self.transport = transport |
39 |
| - print('port opened', transport) |
40 |
| - transport.serial.rts = False |
41 |
| - transport.write(b'hello world\n') |
| 68 | + 2. The protocol instance is tied to a :class:`~serial_asyncio.SerialTransport`. |
42 | 69 |
|
43 |
| - def data_received(self, data): |
44 |
| - print('data received', repr(data)) |
45 |
| - self.transport.close() |
| 70 | + 3. This coroutine returns successfully with a ``(transport, protocol)`` pair. |
46 | 71 |
|
47 |
| - def connection_lost(self, exc): |
48 |
| - print('port closed') |
49 |
| - asyncio.get_event_loop().stop() |
| 72 | + 4. The :meth:`~serial_asyncio.SerialTransport.connection_made()` method of the protocol |
| 73 | + will be called at some point by the event loop. |
50 | 74 |
|
51 |
| - loop = asyncio.get_event_loop() |
52 |
| - coro = serial_asyncio.create_serial_connection(loop, Output, '/dev/ttyUSB0', baudrate=115200) |
53 |
| - loop.run_until_complete(coro) |
54 |
| - loop.run_forever() |
55 |
| - loop.close() |
56 | 75 |
|
| 76 | +.. function:: open_serial_connection(*, loop=None, limit=None, **kwargs) |
| 77 | + :async: |
| 78 | + |
| 79 | + Open a streaming connection to an existing serial port instance. |
| 80 | + |
| 81 | + :param loop: The *asyncio* event loop |
| 82 | + :param limit: A optional buffer limit in bytes for the :class:`asyncio.StreamReader` |
| 83 | + :param kwargs: Forwarded to the :class:`serial.Serial` constructor |
| 84 | + :returns: A coroutine for managing a serial port connection, which when |
| 85 | + awaited returns an :class:`asyncio.StreamReader` and a :class:`asyncio.StreamWriter`. |
| 86 | + :platform: Posix |
| 87 | + |
| 88 | + Use this function to open connections where serial traffic is handled by |
| 89 | + an asynchronous coroutine interacting with :class:`asyncio.StreamReader` and a :class:`asyncio.StreamWriter` objects. |
0 commit comments