66that can be used in the pymodbus library. Below is a
77simple binary coded decimal builder and decoder.
88'''
9+ import unittest
910from struct import pack , unpack
1011from pymodbus .constants import Endian
1112from pymodbus .interfaces import IPayloadBuilder
@@ -23,7 +24,7 @@ def convert_to_bcd(decimal):
2324 while decimal > 0 :
2425 nibble = decimal % 10
2526 bcd += nibble << place
26- decimal /= 10
27+ decimal // = 10
2728 place += 4
2829 return bcd
2930
@@ -76,12 +77,12 @@ def __init__(self, payload=None, endian=Endian.Little):
7677 self ._payload = payload or []
7778 self ._endian = endian
7879
79- def __str__ (self ):
80+ def to_string (self ):
8081 ''' Return the payload buffer as a string
8182
8283 :returns: The payload buffer as a string
8384 '''
84- return '' .join (self ._payload )
85+ return b '' .join (self ._payload )
8586
8687 def reset (self ):
8788 ''' Reset the payload buffer
@@ -96,10 +97,10 @@ def build(self):
9697
9798 :returns: The payload buffer as a list
9899 '''
99- string = str ( self )
100+ string = self . to_string ( )
100101 length = len (string )
101- string = string + ('\x00 ' * (length % 2 ))
102- return [string [i :i + 2 ] for i in xrange (0 , length , 2 )]
102+ string = string + (b '\x00 ' * (length % 2 ))
103+ return [string [i :i + 2 ] for i in range (0 , length , 2 )]
103104
104105 def add_bits (self , values ):
105106 ''' Adds a collection of bits to be encoded
@@ -217,6 +218,44 @@ def decode_string(self, size=1):
217218 return self ._payload [self ._pointer - size :self ._pointer ]
218219
219220
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+
220259#---------------------------------------------------------------------------#
221260# Exported Identifiers
222261#---------------------------------------------------------------------------#
0 commit comments