Skip to content

Commit

Permalink
Merge pull request #4 from Lei-k/feat/async
Browse files Browse the repository at this point in the history
feat(async): Enhance async-pyserial with comprehensive async support and detailed documentation
  • Loading branch information
Lei-k authored Jul 29, 2024
2 parents 65aac69 + d6c21ce commit bc664f4
Show file tree
Hide file tree
Showing 20 changed files with 1,472 additions and 183 deletions.
4 changes: 3 additions & 1 deletion async_pyserial/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

from async_pyserial.common import *

from async_pyserial.backend import set_async_worker

__version__ = '0.1.3'

VERSION = __version__

__all__ = ["SerialPort", "SerialPortOptions", "SerialPortEvent", "SerialPortParity"]
__all__ = ["SerialPort", "SerialPortOptions", "SerialPortEvent", "SerialPortParity", "set_async_worker"]

sys_platform = sys.platform

Expand Down
2 changes: 1 addition & 1 deletion async_pyserial/async_pyserial_core.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class SerialPort:
...
def open(self) -> None:
...
def write(self, arg0: str) -> None:
def write(self, data: str, callback: function) -> None:
...
def set_data_callback(self, callback: function) -> None:
...
Expand Down
20 changes: 20 additions & 0 deletions async_pyserial/backend.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
async_worker = 'none'

async_loop = None

def set_async_worker(w: str, loop = None):
global async_worker, async_loop

if w == 'gevent':
try:
import gevent
except Exception:
raise ModuleNotFoundError('can\'t set async worker to gevent when not installed')
elif w == 'eventlet':
try:
import eventlet
except Exception:
raise ModuleNotFoundError('can\'t set async worker to eventlet when not installed')

async_worker = w
async_loop = loop
25 changes: 23 additions & 2 deletions async_pyserial/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,31 @@ def __init__(self, *args: object) -> None:
super().__init__(*args)

class SerialPortOptions:
"""
SerialPortOptions class defines the configuration options for a serial port connection.
Attributes:
`baudrate` (int): The baud rate for the serial communication. Default is 9600.
`bytesize` (int): The number of data bits in each character. Default is 8.
`stopbits` (int): The number of stop bits. Default is 1.
`parity` (SerialPortParity): The parity check setting. Default is SerialPortParity.NONE.
Options are SerialPortParity.NONE (0), SerialPortParity.ODD (1),
SerialPortParity.EVEN (2).
`write_timeout` (int): The write timeout in milliseconds. Default is 50.
`read_timeout` (int): The read timeout in milliseconds. Default is 50.
`read_bufsize` (int): The read buffer size. Default is 0. When read_bufsize is 0, the internal buffer
is not used, and the user will only get the data received after the read call.
If read_bufsize is not 0, the user will get the data present in the internal buffer
as well as any new data received after the read call.
"""
def __init__(self) -> None:
self.baudrate = 9600
self.bytesize = 8
self.stopbits = 1
self.parity = 0
self.parity = SerialPortParity.NONE # NONE: 0, ODD: 1, EVEN: 2
self.write_timeout = 50
self.read_timeout = 50
self.read_bufsize = 0

class SerialPortEvent:
ON_DATA = 'data'
Expand All @@ -84,4 +102,7 @@ def __init__(self, portName: str, options: SerialPortOptions) -> None:
self.internal_options.stopbits = options.stopbits
self.internal_options.parity = options.parity
self.internal_options.write_timeout = options.write_timeout
self.internal_options.read_timeout = options.read_timeout
self.internal_options.read_timeout = options.read_timeout

class SerialPortError(Exception):
pass
Loading

0 comments on commit bc664f4

Please sign in to comment.