def testPayloadBuilderReset(self): ''' Test basic bit message encoding/decoding ''' builder = PayloadBuilder() builder.add_8bit_uint(0x12) builder.add_8bit_uint(0x34) self.assertEqual('\x12\x34', builder.tostring()) self.assertEqual(['\x12', '\x34'], builder.tolist()) builder.reset() self.assertEqual('', builder.tostring()) self.assertEqual([], builder.tolist())
client.connect() #---------------------------------------------------------------------------# # If you need to build a complex message to send, you can use the payload # builder to simplify the packing logic. # # Here we demonstrate packing a random payload layout, unpacked it looks # like the following: # # - a 8 byte string 'abcdefgh' # - a 32 bit float 22.34 # - a 16 bit unsigned int 0x1234 # - an 8 bit int 0x12 # - an 8 bit bitstring [0,1,0,1,1,0,1,0] #---------------------------------------------------------------------------# builder = PayloadBuilder(endian=Endian.Little) builder.add_string('abcdefgh') builder.add_32bit_float(22.34) builder.add_16bit_uint(0x1234) builder.add_8bit_int(0x12) builder.add_bites([0,1,0,1,1,0,1,0]) payload = builder.tolist() address = 0x01 result = client.write_registers(address, payload) #---------------------------------------------------------------------------# # If you need to decode a collection of registers in a weird layout, the # payload decoder can help you as well. # # Here we demonstrate decoding a random register layout, unpacked it looks # like the following:
def testBigEndianPayloadBuilder(self): ''' Test basic bit message encoding/decoding ''' builder = PayloadBuilder(endian=Endian.Big) builder.add_8bit_uint(1) builder.add_16bit_uint(2) builder.add_32bit_uint(3) builder.add_64bit_uint(4) builder.add_8bit_int(-1) builder.add_16bit_int(-2) builder.add_32bit_int(-3) builder.add_64bit_int(-4) builder.add_32bit_float(1.25) builder.add_64bit_float(6.25) builder.add_string('test') self.assertEqual(self.big_endian_payload, builder.tostring())