|
4 | 4 | # Experimental implementation of asyncio support.
|
5 | 5 | #
|
6 | 6 | # This file is part of pySerial. https://github.com/pyserial/pyserial-asyncio
|
7 |
| -# (C) 2015-2017 pySerial-team |
| 7 | +# (C) 2015-2020 pySerial-team |
8 | 8 | #
|
9 | 9 | # SPDX-License-Identifier: BSD-3-Clause
|
10 | 10 | """\
|
@@ -406,10 +406,61 @@ def _call_connection_lost(self, exc):
|
406 | 406 |
|
407 | 407 |
|
408 | 408 | async def create_serial_connection(loop, protocol_factory, *args, **kwargs):
|
409 |
| - ser = serial.serial_for_url(*args, **kwargs) |
| 409 | + """Create a connection to a new serial port instance. |
| 410 | +
|
| 411 | + This function is a coroutine which will try to establish the |
| 412 | + connection. |
| 413 | +
|
| 414 | + The chronological order of the operation is: |
| 415 | +
|
| 416 | + 1. protocol_factory is called without arguments and must return |
| 417 | + a protocol instance. |
| 418 | +
|
| 419 | + 2. The protocol instance is tied to the transport |
| 420 | +
|
| 421 | + 3. This coroutine returns successfully with a (transport, |
| 422 | + protocol) pair. |
| 423 | +
|
| 424 | + 4. The connection_made() method of the protocol |
| 425 | + will be called at some point by the event loop. |
| 426 | +
|
| 427 | + Note: protocol_factory can be any kind of callable, not |
| 428 | + necessarily a class. For example, if you want to use a pre-created |
| 429 | + protocol instance, you can pass lambda: my_protocol. |
| 430 | +
|
| 431 | + Any additional arguments will be forwarded to the Serial constructor. |
| 432 | + """ |
| 433 | + serial_instance = serial.serial_for_url(*args, **kwargs) |
| 434 | + transport, protocol = await connection_for_serial(loop, protocol_factory, serial_instance) |
| 435 | + return transport, protocol |
| 436 | + |
| 437 | + |
| 438 | +async def connection_for_serial(loop, protocol_factory, serial_instance): |
| 439 | + """Create a connection to the given serial port instance. |
| 440 | +
|
| 441 | + This function is a coroutine which will try to establish the |
| 442 | + connection. |
| 443 | +
|
| 444 | + The chronological order of the operation is: |
| 445 | +
|
| 446 | + 1. protocol_factory is called without arguments and must return |
| 447 | + a protocol instance. |
| 448 | +
|
| 449 | + 2. The protocol instance is tied to the transport |
| 450 | +
|
| 451 | + 3. This coroutine returns successfully with a (transport, |
| 452 | + protocol) pair. |
| 453 | +
|
| 454 | + 4. The connection_made() method of the protocol |
| 455 | + will be called at some point by the event loop. |
| 456 | +
|
| 457 | + Note: protocol_factory can be any kind of callable, not |
| 458 | + necessarily a class. For example, if you want to use a pre-created |
| 459 | + protocol instance, you can pass lambda: my_protocol. |
| 460 | + """ |
410 | 461 | protocol = protocol_factory()
|
411 |
| - transport = SerialTransport(loop, protocol, ser) |
412 |
| - return (transport, protocol) |
| 462 | + transport = SerialTransport(loop, protocol, serial_instance) |
| 463 | + return transport, protocol |
413 | 464 |
|
414 | 465 |
|
415 | 466 | async def open_serial_connection(*,
|
|
0 commit comments