1- """RTU framer."""
1+ """ RTU framer. """
22# pylint: disable=R0801
33import logging
44import struct
2222# Modbus RTU Message
2323# --------------------------------------------------------------------------- #
2424class ModbusRtuFramer (ModbusFramer ):
25- """
26- Modbus RTU Frame controller::
25+ """ Modbus RTU Frame controller::
2726
2827 [ Start Wait ] [Address ][ Function Code] [ Data ][ CRC ][ End Wait ]
2928 3.5 chars 1b 1b Nb 2b 3.5 chars
@@ -72,16 +71,15 @@ def __init__(self, decoder, client=None):
7271 # Private Helper Functions
7372 # ----------------------------------------------------------------------- #
7473 def decode_data (self , data ):
75- """Decode data."""
74+ """ Decode data. """
7675 if len (data ) > self ._hsize :
7776 uid = int (data [0 ])
7877 fcode = int (data [1 ])
7978 return dict (unit = uid , fcode = fcode )
8079 return {}
8180
8281 def checkFrame (self ):
83- """
84- Check if the next frame is available.
82+ """ Check if the next frame is available.
8583 Return True if we were successful.
8684
8785 1. Populate header
@@ -98,8 +96,7 @@ def checkFrame(self):
9896 return False
9997
10098 def advanceFrame (self ):
101- """
102- Skip over the current framed message
99+ """ Skip over the current framed message
103100 This allows us to skip over the current message after we have processed
104101 it or determined that it contains an error. It also has to reset the
105102 current frame header handle
@@ -110,8 +107,7 @@ def advanceFrame(self):
110107 self ._header = {'uid' : 0x00 , 'len' : 0 , 'crc' : b'\x00 \x00 ' }
111108
112109 def resetFrame (self ): # pylint: disable=invalid-name
113- """
114- Reset the entire message frame.
110+ """ Reset the entire message frame.
115111 This allows us to skip over errors that may be in the stream.
116112 It is hard to know if we are simply out of sync or if there is
117113 an error in the stream as we have no way to check the start or
@@ -125,8 +121,7 @@ def resetFrame(self): # pylint: disable=invalid-name
125121 self ._header = {'uid' : 0x00 , 'len' : 0 , 'crc' : b'\x00 \x00 ' }
126122
127123 def isFrameReady (self ):
128- """
129- Check if we should continue decode logic
124+ """ Check if we should continue decode logic
130125 This is meant to be used in a while loop in the decoding phase to let
131126 the decoder know that there is still data in the buffer.
132127
@@ -146,8 +141,7 @@ def isFrameReady(self):
146141 return True
147142
148143 def populateHeader (self , data = None ): # pylint: disable=invalid-name
149- """
150- Try to set the headers `uid`, `len` and `crc`.
144+ """ Try to set the headers `uid`, `len` and `crc`.
151145
152146 This method examines `self._buffer` and writes meta
153147 information into `self._header`.
@@ -168,17 +162,15 @@ def populateHeader(self, data=None): # pylint: disable=invalid-name
168162 self ._header ['crc' ] = data [size - 2 :size ]
169163
170164 def addToFrame (self , message ):
171- """
172- This should be used before the decoding while loop to add the received
165+ """ This should be used before the decoding while loop to add the received
173166 data to the buffer handle.
174167
175168 :param message: The most recent packet
176169 """
177170 self ._buffer += message
178171
179172 def getFrame (self ):
180- """
181- Get the next frame from the buffer
173+ """ Get the next frame from the buffer
182174
183175 :returns: The frame data or ''
184176 """
@@ -192,8 +184,7 @@ def getFrame(self):
192184 return b''
193185
194186 def populateResult (self , result ):
195- """
196- Populates the modbus result header
187+ """ Populates the modbus result header
197188
198189 The serial packets do not have any header information
199190 that is copied.
@@ -207,8 +198,7 @@ def populateResult(self, result):
207198 # Public Member Functions
208199 # ----------------------------------------------------------------------- #
209200 def processIncomingPacket (self , data , callback , unit , ** kwargs ): # pylint: disable=arguments-differ
210- """
211- The new packet processing pattern
201+ """ The new packet processing pattern
212202
213203 This takes in a new request packet, adds it to the current
214204 packet stream, and performs framing on it. That is, checks
@@ -246,8 +236,7 @@ def processIncomingPacket(self, data, callback, unit, **kwargs): # pylint: disab
246236 _logger .debug (txt )
247237
248238 def buildPacket (self , message ):
249- """
250- Creates a ready to send modbus packet
239+ """ Creates a ready to send modbus packet
251240
252241 :param message: The populated request/response to send
253242 """
@@ -261,8 +250,7 @@ def buildPacket(self, message):
261250 return packet
262251
263252 def sendPacket (self , message ):
264- """
265- Sends packets on the bus with 3.5char delay between frames
253+ """ Sends packets on the bus with 3.5char delay between frames
266254 :param message: Message to be sent over the bus
267255 :return:
268256 """
@@ -303,8 +291,7 @@ def sendPacket(self, message):
303291 return size
304292
305293 def recvPacket (self , size ):
306- """
307- Receives packet from the bus with specified len
294+ """ Receives packet from the bus with specified len
308295 :param size: Number of bytes to read
309296 :return:
310297 """
@@ -313,9 +300,7 @@ def recvPacket(self, size):
313300 return result
314301
315302 def _process (self , callback , error = False ):
316- """
317- Process incoming packets irrespective error condition
318- """
303+ """ Process incoming packets irrespective error condition. """
319304 data = self .getRawFrame () if error else self .getFrame ()
320305 result = self .decoder .decode (data )
321306 if result is None :
@@ -327,9 +312,7 @@ def _process(self, callback, error=False):
327312 callback (result ) # defer or push to a thread?
328313
329314 def getRawFrame (self ): # pylint: disable=invalid-name
330- """
331- Returns the complete buffer
332- """
315+ """ Returns the complete buffer. """
333316 txt = f"Getting Raw Frame - { hexlify_packets (self ._buffer )} "
334317 _logger .debug (txt )
335318 return self ._buffer
0 commit comments