def test_payload_decoder_reset(self): """ Test the payload decoder reset functionality """ decoder = BinaryPayloadDecoder('\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())
def _update(self): """Poll MODBUS target server. Store results in self.cvt """ for reg in self.config['registers']: try: """TODO: filter by register_name_list""" if reg['type'] == '32bit_float': read_data = self.client.read_holding_registers( reg['mod_add'], 2, unit=1) decoded_data = BinaryPayloadDecoder.from_registers( list(reversed(read_data.registers)), endian=self.config['endian']) self.cvt[reg['name']] = decoded_data.decode_32bit_float( ) * reg['scale'] elif reg['type'] == '32bit_int': read_data = self.client.read_holding_registers( reg['mod_add'], 2, unit=1) decoded_data = BinaryPayloadDecoder.from_registers( read_data.registers, endian=self.config['endian']) self.cvt[reg['name']] = decoded_data.decode_32bit_int( ) * reg['scale'] elif reg['type'] == '32bit_uint': read_data = self.client.read_holding_registers( reg['mod_add'], 2, unit=1) decoded_data = BinaryPayloadDecoder.from_registers( read_data.registers, endian=self.config['endian']) self.cvt[reg['name']] = decoded_data.decode_32bit_uint( ) * reg['scale'] elif reg['type'] == '16bit_int': read_data = self.client.read_holding_registers( reg['mod_add'], 1, unit=1) decoded_data = BinaryPayloadDecoder.from_registers( read_data.registers, endian=self.config['endian']) self.cvt[reg['name']] = decoded_data.decode_16bit_int( ) * reg['scale'] elif reg['type'] == '16bit_uint': read_data = self.client.read_holding_registers( reg['mod_add'], 1, unit=1) decoded_data = BinaryPayloadDecoder.from_registers( read_data.registers, endian=self.config['endian']) self.cvt[reg['name']] = decoded_data.decode_16bit_uint( ) * reg['scale'] else: print(reg['type'], 'data type not supported') except AttributeError: print(self.process_name, 'MODBUS CLIENT: Read error') # TODO: How to import pymobus3 exceptions? self.timestamp = time.ctime()
def get(self, _type, address, count, _datatype=None): # get == Decoder """ Generic 'get' method for LockedDataBlock. Figures out the underlying method to call according to _type. :param _type: Type of modbus register ('co', 'di', 'hr', 'ir') :param address: Index of the register :param count: The amount of registers to get sequentially :return: Value of requested index(es). """ if _datatype == None: #log.debug("Retrieving a None type") if _type == "di": return self._get_di(address, count) elif _type == "co": return self._get_co(address, count) elif _type == "hr": return self._get_hr(address, count) elif _type == "ir": return self._get_ir(address, count) else: print("t: {} a: {} c: {}") raise ValueError elif _datatype == 'bool': if _type == "di": values = self._get_di(address, count) elif _type == "co": values = self._get_co(address, count) # elif _type == "hr": # values = self._get_hr(address, count) # elif _type == "ir": # values = self._get_ir(address, count) else: print("t: {} a: {} c: {}") raise ValueError decoder = BinaryPayloadDecoder.from_coils(values.bits, endian=Endian.Big) return decoder.decode_bits() else: if _type == "hr": values = self._get_hr(address, count) elif _type == "ir": values = self._get_ir(address, count) else: print("t: {} a: {} c: {}") raise ValueError if _datatype == '32bit_float': # TODO: try for the datatypes you have in the RTU for the mosaik simulation and do it also for set and test decoder = BinaryPayloadDecoder.from_registers( values, endian=Endian.Big) return decoder.decode_32bit_float() elif _datatype == '64bit_float': decoder = BinaryPayloadDecoder.from_registers( values, endian=Endian.Big) return decoder.decode_64bit_float()
def test_payload_decoder_coil_factory(self): """ Test the payload decoder reset functionality """ payload = [1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1] decoder = BinaryPayloadDecoder.from_coils(payload, endian=Endian.Little) encoded = '\x11\x88' self.assertEqual(encoded, decoder.decode_string(2)) decoder = BinaryPayloadDecoder.from_coils(payload, endian=Endian.Big) encoded = '\x11\x88' self.assertEqual(encoded, decoder.decode_string(2)) self.assertRaises(ParameterException, lambda: BinaryPayloadDecoder.from_coils('abcd'))
def test_payload_decoder_coil_factory(self): """ Test the payload decoder reset functionality """ payload = [1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1] decoder = BinaryPayloadDecoder.from_coils(payload, endian=Endian.Little) encoded = '\x11\x88' self.assertEqual(encoded, decoder.decode_string(2)) decoder = BinaryPayloadDecoder.from_coils(payload, endian=Endian.Big) encoded = '\x11\x88' self.assertEqual(encoded, decoder.decode_string(2)) self.assertRaises( ParameterException, lambda: BinaryPayloadDecoder.from_coils('abcd') )
def test_payload_decoder_register_factory(self): """ Test the payload decoder reset functionality """ payload = [1, 2, 3, 4] decoder = BinaryPayloadDecoder.from_registers(payload, endian=Endian.Little) encoded = '\x00\x01\x00\x02\x00\x03\x00\x04' self.assertEqual(encoded, decoder.decode_string(8)) decoder = BinaryPayloadDecoder.from_registers(payload, endian=Endian.Big) encoded = '\x00\x01\x00\x02\x00\x03\x00\x04' self.assertEqual(encoded, decoder.decode_string(8)) self.assertRaises( ParameterException, lambda: BinaryPayloadDecoder.from_registers('abcd') )
def test_payload_decoder_register_factory(self): """ Test the payload decoder reset functionality """ payload = [1, 2, 3, 4] decoder = BinaryPayloadDecoder.from_registers(payload, endian=Endian.Little) encoded = '\x00\x01\x00\x02\x00\x03\x00\x04' self.assertEqual(encoded, decoder.decode_string(8)) decoder = BinaryPayloadDecoder.from_registers(payload, endian=Endian.Big) encoded = '\x00\x01\x00\x02\x00\x03\x00\x04' self.assertEqual(encoded, decoder.decode_string(8)) self.assertRaises(ParameterException, lambda: BinaryPayloadDecoder.from_registers('abcd'))
fd = open(FILENAME, 'w+') csvrow = "timestamp;switch19;switch24;switch25;switch36;voltageb5;current19;current24;current25;current36\n" fd.write(csvrow) i=0 while i<10000: result = client.read_coils(0, 4, unit=1) ts = str(datetime.now()) print("*"*10+" SWITCHES "+"*"*10) switches = [result.bits[0], result.bits[1], result.bits[2], result.bits[3]] print("Switch branch 19: {}\nSwitch branch 24: {}\nSwitch branch 25: {}\nSwitch branch 36: {}".format(switches[0], switches[1], switches[2], switches[3])) # Voltage and current address = 12 count = 20 result = client.read_holding_registers(address, count, unit=1) decoder = BinaryPayloadDecoder.from_registers(result.registers, endian=Endian.Big) print("*"*10+" VOLTAGE "+"*"*10) voltage = decoder.decode_64bit_float() current = [decoder.decode_64bit_float(), decoder.decode_64bit_float(), decoder.decode_64bit_float(), decoder.decode_64bit_float()] address = 50 count = 4 result = client.read_holding_registers(address, count, unit=1) decoder = BinaryPayloadDecoder.from_registers(result.registers, endian=Endian.Big) trafo = decoder.decode_64bit_float() print("Voltage bus b5: {}\n".format(voltage)+"*"*10+" CURRENTS "+"*"*10+"\nCurrent line 19: {}\nCurrent line 24: {}\nCurrent line 25: {}\nCurrent line 36: {}".format(current[0], current[1], current[2], current[3])) print("*"*10+" TRAFO "+"*"*10) print("Transformer tap position: {}".format(trafo)) print("*"*30+"\n")
def test_big_endian_payload_decoder(self): """ Test basic bit message encoding/decoding """ decoder = BinaryPayloadDecoder(self.big_endian_payload, endian=Endian.Big) 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)) self.assertEqual(self.bitstring, decoder.decode_bits())