1515from pymodbus .exceptions import (
1616 ConnectionException ,
1717 NotImplementedException ,
18- ParameterException ,
1918)
2019from pymodbus .factory import ClientDecoder
2120from pymodbus .transaction import (
2221 DictTransactionManager ,
23- ModbusAsciiFramer ,
24- ModbusBinaryFramer ,
2522 ModbusRtuFramer ,
2623 ModbusSocketFramer ,
2724 ModbusTlsFramer ,
@@ -614,16 +611,9 @@ class ModbusSerialClient(
614611 inter_char_timeout = 0
615612 silent_interval = 0
616613
617- def __init__ (self , method = "ascii" , ** kwargs ):
614+ def __init__ (self , framer = ModbusRtuFramer , ** kwargs ):
618615 """Initialize a serial client instance.
619616
620- The methods to connect are::
621-
622- - ascii
623- - rtu
624- - binary
625-
626- :param method: The method to use for connection
627617 :param port: The serial port to attach to
628618 :param stopbits: The number of stop bits to use
629619 :param bytesize: The bytesize of the serial messages
@@ -633,10 +623,11 @@ def __init__(self, method="ascii", **kwargs):
633623 :param strict: Use Inter char timeout for baudrates <= 19200 (adhere
634624 to modbus standards)
635625 :param handle_local_echo: Handle local echo of the USB-to-RS485 adaptor
626+ :param framer: The modbus framer to use (default ModbusRtuFramer)
636627 """
637- self .method = method
628+ self .framer = framer
638629 self .socket = None
639- BaseModbusClient .__init__ (self , self . __implementation ( method , self ), ** kwargs )
630+ BaseModbusClient .__init__ (self , framer ( ClientDecoder () , self ), ** kwargs )
640631
641632 self .port = kwargs .get ("port" , 0 )
642633 self .stopbits = kwargs .get ("stopbits" , Defaults .Stopbits )
@@ -647,7 +638,7 @@ def __init__(self, method="ascii", **kwargs):
647638 self ._strict = kwargs .get ("strict" , False )
648639 self .last_frame_end = None
649640 self .handle_local_echo = kwargs .get ("handle_local_echo" , False )
650- if self .method == "rtu" :
641+ if isinstance ( self .framer , ModbusRtuFramer ) :
651642 if self .baudrate > 19200 :
652643 self .silent_interval = 1.75 / 1000 # ms
653644 else :
@@ -656,26 +647,6 @@ def __init__(self, method="ascii", **kwargs):
656647 self .silent_interval = 3.5 * self ._t0
657648 self .silent_interval = round (self .silent_interval , 6 )
658649
659- @staticmethod
660- def __implementation (method , client ):
661- """Return the requested framer.
662-
663- :method: The serial framer to instantiate
664- :returns: The requested serial framer
665- :raises ConnectionException:
666- :raises ParameterException:
667- """
668- method = method .lower ()
669- if method == "ascii" :
670- return ModbusAsciiFramer (ClientDecoder (), client )
671- if method == "rtu" :
672- return ModbusRtuFramer (ClientDecoder (), client )
673- if method == "binary" :
674- return ModbusBinaryFramer (ClientDecoder (), client )
675- if method == "socket" :
676- return ModbusSocketFramer (ClientDecoder (), client )
677- raise ParameterException ("Invalid framer method requested" )
678-
679650 def connect (self ):
680651 """Connect to the modbus serial server.
681652
@@ -692,7 +663,7 @@ def connect(self):
692663 baudrate = self .baudrate ,
693664 parity = self .parity ,
694665 )
695- if self .method == "rtu" :
666+ if isinstance ( self .framer , ModbusRtuFramer ) :
696667 if self ._strict :
697668 self .socket .interCharTimeout = self .inter_char_timeout
698669 self .last_frame_end = None
@@ -802,13 +773,13 @@ def __str__(self):
802773
803774 :returns: The string representation
804775 """
805- return f"ModbusSerialClient({ self .method } baud[{ self .baudrate } ])"
776+ return f"ModbusSerialClient({ self .framer } baud[{ self .baudrate } ])"
806777
807778 def __repr__ (self ):
808779 """Return string representation."""
809780 return (
810781 f"<{ self .__class__ .__name__ } at { hex (id (self ))} socket={ self .socket } , "
811- f"method ={ self .method } , timeout={ self .timeout } >"
782+ f"framer ={ self .framer } , timeout={ self .timeout } >"
812783 )
813784
814785
0 commit comments