66that can be used in the pymodbus library. Below is a
77simple binary coded decimal builder and decoder.
88'''
9- import unittest
109from struct import pack , unpack
1110from pymodbus .constants import Endian
1211from pymodbus .interfaces import IPayloadBuilder
@@ -24,7 +23,7 @@ def convert_to_bcd(decimal):
2423 while decimal > 0 :
2524 nibble = decimal % 10
2625 bcd += nibble << place
27- decimal // = 10
26+ decimal /= 10
2827 place += 4
2928 return bcd
3029
@@ -77,12 +76,12 @@ def __init__(self, payload=None, endian=Endian.Little):
7776 self ._payload = payload or []
7877 self ._endian = endian
7978
80- def to_string (self ):
79+ def __str__ (self ):
8180 ''' Return the payload buffer as a string
8281
8382 :returns: The payload buffer as a string
8483 '''
85- return b '' .join (self ._payload )
84+ return '' .join (self ._payload )
8685
8786 def reset (self ):
8887 ''' Reset the payload buffer
@@ -97,10 +96,10 @@ def build(self):
9796
9897 :returns: The payload buffer as a list
9998 '''
100- string = self . to_string ( )
99+ string = str ( self )
101100 length = len (string )
102- string = string + (b '\x00 ' * (length % 2 ))
103- return [string [i :i + 2 ] for i in range (0 , length , 2 )]
101+ string = string + ('\x00 ' * (length % 2 ))
102+ return [string [i :i + 2 ] for i in xrange (0 , length , 2 )]
104103
105104 def add_bits (self , values ):
106105 ''' Adds a collection of bits to be encoded
@@ -218,44 +217,6 @@ def decode_string(self, size=1):
218217 return self ._payload [self ._pointer - size :self ._pointer ]
219218
220219
221- #---------------------------------------------------------------------------#
222- # Fixture
223- #---------------------------------------------------------------------------#
224- class BcdPayloadUtilityTests (unittest .TestCase ):
225-
226- def setUp (self ):
227- '''
228- Initializes the test environment and builds request/result
229- encoding pairs
230- '''
231- self .bitstring = [True , False , False , False , True , False , False , False ]
232- self .payload = \
233- b'\x01 \x02 \x00 \x03 \x00 \x00 \x00 \x04 \x00 \x00 \x00 \x00 ' \
234- b'\x00 \x00 \x00 \xff \xfe \xff \xfd \xff \xff \xff \xfc \xff ' \
235- b'\xff \xff \xff \xff \xff \xff \x00 \x00 \xa0 \x3f \x00 \x00 ' \
236- b'\x00 \x00 \x00 \x00 \x19 \x40 \x74 \x65 \x73 \x74 \x11 '
237-
238- def testPayloadBuilder (self ):
239- ''' Test basic bit message encoding/decoding '''
240- builder = BcdPayloadBuilder (endian = Endian .Little )
241- builder .add_number (1 )
242- builder .add_string (b'test' )
243- builder .add_bits (self .bitstring )
244- self .assertEqual (self .payload , builder .to_string ())
245-
246- def testPayloadDecoder (self ):
247- ''' Test basic bit message encoding/decoding '''
248- decoder = BcdPayloadDecoder (self .payload )
249- self .assertEqual (1 , decoder .decode_int ())
250- self .assertEqual (b'test' , decoder .decode_string (4 ))
251- self .assertEqual (self .bitstring , decoder .decode_bits ())
252-
253- #---------------------------------------------------------------------------#
254- # unit tests
255- #---------------------------------------------------------------------------#
256- if __name__ == "__main__" :
257- unittest .main ()
258-
259220#---------------------------------------------------------------------------#
260221# Exported Identifiers
261222#---------------------------------------------------------------------------#
0 commit comments