Skip to content

Commit 474abec

Browse files
Adds a connection_for_serial API which allows creation of a connection around an existing Serial instance. (#59)
Co-authored-by: Robert Smallshire <[email protected]>
1 parent 55877fb commit 474abec

File tree

1 file changed

+55
-4
lines changed

1 file changed

+55
-4
lines changed

serial_asyncio/__init__.py

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# Experimental implementation of asyncio support.
55
#
66
# This file is part of pySerial. https://github.com/pyserial/pyserial-asyncio
7-
# (C) 2015-2017 pySerial-team
7+
# (C) 2015-2020 pySerial-team
88
#
99
# SPDX-License-Identifier: BSD-3-Clause
1010
"""\
@@ -406,10 +406,61 @@ def _call_connection_lost(self, exc):
406406

407407

408408
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+
"""
410461
protocol = protocol_factory()
411-
transport = SerialTransport(loop, protocol, ser)
412-
return (transport, protocol)
462+
transport = SerialTransport(loop, protocol, serial_instance)
463+
return transport, protocol
413464

414465

415466
async def open_serial_connection(*,

0 commit comments

Comments
 (0)