11"""Modbus client async serial communication."""
2+ from __future__ import annotations
3+
24import asyncio
35import time
46from contextlib import suppress
57from functools import partial
6- from typing import Any , Type
8+ from typing import Any
79
810from pymodbus .client .base import ModbusBaseClient
911from pymodbus .exceptions import ConnectionException
10- from pymodbus .framer import ModbusFramer
11- from pymodbus .framer .rtu_framer import ModbusRtuFramer
12+ from pymodbus .framer import FRAMER_NAME_TO_CLASS , Framer
1213from pymodbus .logging import Log
1314from pymodbus .transport import CommType
1415from pymodbus .utilities import ModbusTransactionState
@@ -27,7 +28,6 @@ class AsyncModbusSerialClient(ModbusBaseClient, asyncio.Protocol):
2728
2829 Optional parameters:
2930
30- :param framer: Framer class.
3131 :param baudrate: Bits per second.
3232 :param bytesize: Number of bits per byte 7-8.
3333 :param parity: 'E'ven, 'O'dd or 'N'one
@@ -36,6 +36,7 @@ class AsyncModbusSerialClient(ModbusBaseClient, asyncio.Protocol):
3636
3737 Common optional parameters:
3838
39+ :param framer: Framer enum name
3940 :param timeout: Timeout for a request, in seconds.
4041 :param retries: Max number of retries per request.
4142 :param retry_on_empty: Retry on empty response.
@@ -65,7 +66,7 @@ async def run():
6566 def __init__ (
6667 self ,
6768 port : str ,
68- framer : Type [ ModbusFramer ] = ModbusRtuFramer ,
69+ framer : Framer = Framer . RTU ,
6970 baudrate : int = 19200 ,
7071 bytesize : int = 8 ,
7172 parity : str = "N" ,
@@ -74,9 +75,10 @@ def __init__(
7475 ) -> None :
7576 """Initialize Asyncio Modbus Serial Client."""
7677 asyncio .Protocol .__init__ (self )
78+ framer_class = FRAMER_NAME_TO_CLASS .get (framer , framer )
7779 ModbusBaseClient .__init__ (
7880 self ,
79- framer = framer ,
81+ framer_class = framer_class , # type: ignore[arg-type]
8082 CommType = CommType .SERIAL ,
8183 host = port ,
8284 baudrate = baudrate ,
@@ -106,7 +108,6 @@ class ModbusSerialClient(ModbusBaseClient):
106108
107109 Optional parameters:
108110
109- :param framer: Framer class.
110111 :param baudrate: Bits per second.
111112 :param bytesize: Number of bits per byte 7-8.
112113 :param parity: 'E'ven, 'O'dd or 'N'one
@@ -115,6 +116,7 @@ class ModbusSerialClient(ModbusBaseClient):
115116
116117 Common optional parameters:
117118
119+ :param framer: Framer enum name
118120 :param timeout: Timeout for a request, in seconds.
119121 :param retries: Max number of retries per request.
120122 :param retry_on_empty: Retry on empty response.
@@ -150,7 +152,7 @@ def run():
150152 def __init__ (
151153 self ,
152154 port : str ,
153- framer : Type [ ModbusFramer ] = ModbusRtuFramer ,
155+ framer : Framer = Framer . RTU ,
154156 baudrate : int = 19200 ,
155157 bytesize : int = 8 ,
156158 parity : str = "N" ,
@@ -160,9 +162,10 @@ def __init__(
160162 """Initialize Modbus Serial Client."""
161163 self .transport = None
162164 kwargs ["use_sync" ] = True
165+ framer_class = FRAMER_NAME_TO_CLASS .get (framer , framer )
163166 ModbusBaseClient .__init__ (
164167 self ,
165- framer = framer ,
168+ framer_class = framer_class , # type: ignore[arg-type]
166169 CommType = CommType .SERIAL ,
167170 host = port ,
168171 baudrate = baudrate ,
@@ -207,10 +210,9 @@ def connect(self): # pylint: disable=invalid-overridden-method
207210 baudrate = self .comm_params .baudrate ,
208211 parity = self .comm_params .parity ,
209212 )
210- if isinstance (self .framer , ModbusRtuFramer ):
211- if self .params .strict :
212- self .socket .interCharTimeout = self .inter_char_timeout
213- self .last_frame_end = None
213+ if self .params .strict :
214+ self .socket .interCharTimeout = self .inter_char_timeout
215+ self .last_frame_end = None
214216 except serial .SerialException as msg :
215217 Log .error ("{}" , msg )
216218 self .close ()
0 commit comments