1- """**Modbus client async serial communication.**
2-
3- The serial communication is RS-485 based, and usually used vith a usb RS485 dongle.
4-
5- Example::
6-
7- from pymodbus.client import AsyncModbusSerialClient
8-
9- async def run():
10- client = AsyncModbusSerialClient(
11- "dev/pty0", # serial port
12- # Common optional paramers:
13- # modbus_decoder=ClientDecoder,
14- # framer=ModbusRtuFramer,
15- # timeout=10,
16- # retries=3,
17- # retry_on_empty=False,
18- # close_comm_on_error=False,
19- # strict=True,
20- # Serial setup parameters
21- # baudrate=9600,
22- # bytesize=8,
23- # parity="N",
24- # stopbits=1,
25- # handle_local_echo=False,
26- )
27-
28- await client.aConnect()
29- ...
30- await client.aClose()
31- """
1+ """Modbus client async serial communication."""
322import asyncio
333import logging
344
355from serial_asyncio import create_serial_connection
366
377from pymodbus .client .base import ModbusClientProtocol
8+ from pymodbus .framer import ModbusFramer
389from pymodbus .transaction import ModbusRtuFramer
3910from pymodbus .client .base import ModbusBaseClient
11+ from pymodbus .constants import Defaults
4012
4113_logger = logging .getLogger (__name__ )
4214
4315
4416class AsyncModbusSerialClient (ModbusBaseClient ):
45- r"""Modbus client for async serial (RS-485) communication.
46-
47- :param port: (positional) Serial port used for communication.
48- :param framer: (optional, default ModbusRtuFramer) Framer class.
49- :param baudrate: (optional, default 9600) Bits pr second.
50- :param bytesize: (optional, default 8) Number of bits pr byte 7-8.
51- :param parity: (optional, default None).
52- :param stopbits: (optional, default 1) Number of stop bits 0-2 to use.
53- :param handle_local_echo: (optional, default false) Handle local echo of the USB-to-RS485 dongle.
54- :param \*\*kwargs: (optional) Extra experimental parameters for transport
55- :return: client object
17+ """**AsyncModbusSerialClient**.
18+
19+ :param port: Serial port used for communication.
20+ :param framer: (optional) Framer class.
21+ :param baudrate: (optional) Bits pr second.
22+ :param bytesize: (optional) Number of bits pr byte 7-8.
23+ :param parity: (optional) 'E'ven, 'O'dd or 'N'one
24+ :param stopbits: (optional) Number of stop bits 0-2¡.
25+ :param handle_local_echo: (optional) Discard local echo from dongle.
26+ :param kwargs: (optional) Experimental parameters
27+
28+ The serial communication is RS-485 based, and usually used vith a usb RS485 dongle.
29+
30+ Example::
31+
32+ from pymodbus.client import AsyncModbusSerialClient
33+
34+ async def run():
35+ client = AsyncModbusSerialClient("dev/serial0")
36+
37+ await client.connect()
38+ ...
39+ await client.close()
5640 """
5741
5842 transport = None
5943 framer = None
6044
6145 def __init__ (
6246 self ,
63- port ,
64- framer = ModbusRtuFramer ,
65- baudrate = 9600 ,
66- bytesize = 8 ,
67- parity = "N" ,
68- stopbits = 1 ,
69- handle_local_echo = False ,
70- ** kwargs ,
71- ):
47+ port : str ,
48+ framer : ModbusFramer = ModbusRtuFramer ,
49+ baudrate : int = Defaults . Baudrate ,
50+ bytesize : int = Defaults . Bytesize ,
51+ parity : chr = Defaults . Parity ,
52+ stopbits : int = Defaults . Stopbits ,
53+ handle_local_echo : bool = Defaults . HandleLocalEcho ,
54+ ** kwargs : any ,
55+ ) -> None :
7256 """Initialize Asyncio Modbus Serial Client."""
7357 super ().__init__ (framer = framer , ** kwargs )
7458 self .params .port = port
@@ -81,8 +65,11 @@ def __init__(
8165 self .protocol = None
8266 self ._connected_event = asyncio .Event ()
8367
84- async def aClose (self ):
85- """Stop connection."""
68+ async def close (self ): # pylint: disable=invalid-overridden-method
69+ """Stop connection.
70+
71+ :meta private:
72+ """
8673 if self ._connected and self .protocol and self .protocol .transport :
8774 self .protocol .transport .close ()
8875
@@ -97,8 +84,11 @@ def _connected(self):
9784 """Connect internal."""
9885 return self ._connected_event .is_set ()
9986
100- async def aConnect (self ):
101- """Connect Async client."""
87+ async def connect (self ): # pylint: disable=invalid-overridden-method
88+ """Connect Async client.
89+
90+ :meta private:
91+ """
10292 # get current loop, if there are no loop a RuntimeError will be raised
10393 self .loop = asyncio .get_running_loop ()
10494
0 commit comments