Exemplo n.º 1
0
 def testPayloadDecoderReset(self):
     ''' Test the payload decoder reset functionality '''
     decoder = PayloadDecoder('\x12\x34')
     self.assertEqual(0x12, decoder.decode_8bit_uint())
     self.assertEqual(0x34, decoder.decode_8bit_uint())
     decoder.reset()   
     self.assertEqual(0x3412, decoder.decode_16bit_uint())
Exemplo n.º 2
0
 def testPayloadDecoderReset(self):
     ''' Test the payload decoder reset functionality '''
     decoder = PayloadDecoder('\x12\x34')
     self.assertEqual(0x12, decoder.decode_8bit_uint())
     self.assertEqual(0x34, decoder.decode_8bit_uint())
     decoder.reset()
     self.assertEqual(0x3412, decoder.decode_16bit_uint())
Exemplo n.º 3
0
# 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:
#
# - 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]
#---------------------------------------------------------------------------# 
address = 0x01
count   = 8
result  = client.read_input_registers(address, count)
decoder = PayloadDecoder.fromRegisters(result.registers, endian=Endian.Little)
decoded = [
    decoder.decode_string(8),
    decoder.decode_32bit_float(),
    decoder.decode_16bit_uint(),
    decoder.decode_8bit_int(),
    decoder.decode_bits(),
]
for decode in decoded:
    print decode

#---------------------------------------------------------------------------# 
# close the client
#---------------------------------------------------------------------------# 
client.close()
Exemplo n.º 4
0
 def testLittleEndianPayloadDecoder(self):
     ''' Test basic bit message encoding/decoding '''
     decoder = PayloadDecoder(self.little_endian_payload, endian=Endian.Little)
     self.assertEqual(1,      decoder.decode_8bit_uint())
     self.assertEqual(2,      decoder.decode_16bit_uint())
     self.assertEqual(3,      decoder.decode_32bit_uint())
     self.assertEqual(4,      decoder.decode_64bit_uint())
     self.assertEqual(-1,     decoder.decode_8bit_int())
     self.assertEqual(-2,     decoder.decode_16bit_int())
     self.assertEqual(-3,     decoder.decode_32bit_int())
     self.assertEqual(-4,     decoder.decode_64bit_int())
     self.assertEqual(1.25,   decoder.decode_32bit_float())
     self.assertEqual(6.25,   decoder.decode_64bit_float())
     self.assertEqual('test', decoder.decode_string(4))
Exemplo n.º 5
0
 def testLittleEndianPayloadDecoder(self):
     ''' Test basic bit message encoding/decoding '''
     decoder = PayloadDecoder(self.little_endian_payload,
                              endian=Endian.Little)
     self.assertEqual(1, decoder.decode_8bit_uint())
     self.assertEqual(2, decoder.decode_16bit_uint())
     self.assertEqual(3, decoder.decode_32bit_uint())
     self.assertEqual(4, decoder.decode_64bit_uint())
     self.assertEqual(-1, decoder.decode_8bit_int())
     self.assertEqual(-2, decoder.decode_16bit_int())
     self.assertEqual(-3, decoder.decode_32bit_int())
     self.assertEqual(-4, decoder.decode_64bit_int())
     self.assertEqual(1.25, decoder.decode_32bit_float())
     self.assertEqual(6.25, decoder.decode_64bit_float())
     self.assertEqual('test', decoder.decode_string(4))