Skip to content

Commit c21ac9a

Browse files
kadykovksunden
andauthored
Implement get_aserial helper function (#67)
* Implement get_aserial helper function * Fix get_aserial does not save any connections * Fix 'gets multiple values for keyword argumetn' error * Remove positional only parameters * Add get_serial to the changelog * Convert to numpydoc format --------- Co-authored-by: Aleksandr Kadykov <[email protected]> Co-authored-by: Kyle Sunden <[email protected]>
1 parent 6b2f386 commit c21ac9a

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

yaqd-core/yaqd_core/aserial/_aserial.py

+39-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
__all__ = ["ASerial"]
2-
1+
__all__ = ["ASerial", "get_aserial"]
32

43
import asyncio
4+
from typing import Any, Dict
5+
56
import serial # type: ignore
67

78

@@ -50,3 +51,39 @@ async def awrite_then_readline(self, data, size=-1):
5051
async with self._readlock:
5152
self.write(data)
5253
return await self._areadline(size)
54+
55+
56+
_serial_objects: Dict[str, ASerial] = {}
57+
58+
59+
def get_aserial(
60+
port: str,
61+
baudrate: int = 9600,
62+
eol: bytes = b"\n",
63+
**kwargs: Any,
64+
) -> ASerial:
65+
"""Create a new ASerial object or return already existed one.
66+
67+
Parameters
68+
----------
69+
port: str
70+
Serial port identificator, e.g. 'COM0' or '/dev/ttyUSB0'
71+
If an matching open port exists, all other arguments are ignored.
72+
baudrate: int, optional
73+
Baud rate of the port. Defaults to 9600.
74+
eol: bytes, optional
75+
End of line terminator. Defaults to new-line.
76+
77+
Other Parameters
78+
----------------
79+
**kwargs: `serial.Serial` arguments, optional
80+
81+
82+
Returns
83+
-------
84+
ASerial
85+
Corresponding ASerial object
86+
"""
87+
if port not in _serial_objects:
88+
_serial_objects[port] = ASerial(port=port, baudrate=baudrate, eol=eol, **kwargs)
89+
return _serial_objects[port]

yaqd-fakes/CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/).
55

66
## [Unreleased]
77

8+
### Added
9+
- aserial.get_aserial(), helper function that caches already created serial objects
10+
811
## [2023.6.0]
912

1013
### Added

0 commit comments

Comments
 (0)