def testPayloadDecoderCoilFactory(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.fromCoils(payload, endian=Endian.Little) encoded = "\x11\x88" self.assertEqual(encoded, decoder.decode_string(2)) decoder = BinaryPayloadDecoder.fromCoils(payload, endian=Endian.Big) encoded = "\x11\x88" self.assertEqual(encoded, decoder.decode_string(2)) self.assertRaises(ParameterException, lambda: BinaryPayloadDecoder.fromCoils("abcd"))
def testPayloadDecoderCoilFactory(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.fromCoils(payload, byteorder=Endian.Little) encoded = b'\x11\x88' self.assertEqual(encoded, decoder.decode_string(2)) decoder = BinaryPayloadDecoder.fromCoils(payload, byteorder=Endian.Big) encoded = b'\x11\x88' self.assertEqual(encoded, decoder.decode_string(2)) self.assertRaises(ParameterException, lambda: BinaryPayloadDecoder.fromCoils('abcd'))
def evaluate_coils(cls, **kwargs): """ supported methods: - read_coils - read_discrete_inputs supported evaluation_modes: - string-ascii-register - string-ascii-byte - bitmask_int - bitmask_bool unsupported/unimplimented evaluation_modes: - floating point: ieee754 - whole-remainder - signed integer - unsigned """ LOG.debug('evaluate_coils: {}'.format(kwargs)) decoder = BinaryPayloadDecoder.fromCoils( kwargs.get('coils'), byteorder=kwargs.get('byte_endianness'), ) eval_mode = kwargs.get('evaluation_mode') bitmask = kwargs.get('bitmask') coil_count = kwargs.get('register_count') assert eval_mode in [ 'string-ascii-register', 'string-ascii-byte', 'bitmask_int', 'bitmask_bool' ], "unsupported evaluation mode: {}".format(eval_mode) decoder_method = cls.get_decoder_method(eval_mode, coil_count) LOG.debug('using method: {}'.format(decoder_method)) if eval_mode == 'string-ascii-register': return getattr(decoder, decoder_method)(coil_count) elif eval_mode == 'string-ascii-byte': return getattr(decoder, decoder_method)(1) else: LOG.debug("trying to decode with: {}".format(decoder_method)) decoded = getattr(decoder, decoder_method)() LOG.debug("decoded: {}".format(decoded)) bools_to_ints = [1 if c else 0 for c in decoded] LOG.debug('bools_to_ints: {}'.format(bools_to_ints)) payload = bools_to_ints val = 0 for bit in bools_to_ints: val = (val << 1) | bit LOG.debug("bitmask: {}".format(bitmask)) LOG.debug("val: {}".format(val)) if eval_mode == 'bitmask_int': payload = int(bitmask, 0) & val LOG.debug("bitmask_int: {}".format(payload)) elif eval_mode == 'bitmask_bool': payload = bool(int(bitmask, 0) & val) LOG.debug("bitmask_bool: {}".format(payload)) return payload
def decode(self, raw, size, mb_type, mb_funcall=3): log.debug('decode param (raw=%s, size=%s, mb_type=%s, mb_funcall=%s)' % (raw, size, mb_type, mb_funcall)) if mb_funcall == 1: # Read Coil Status (FC=01) log.debug("decoder FC1 (raw: %s)" % raw) decoder = BinaryPayloadDecoder.fromCoils(raw, endian=self.endian) elif mb_funcall == 2: # Read Discrete Input (FC=02) log.debug("decoder FC2 (raw: %s)" % raw) decoder = BinaryPayloadDecoder(raw, endian=self.endian) elif mb_funcall == 3: # Read Holding Registers (FC=03) log.debug("decoder FC3 (raw: %s)" % raw) decoder = BinaryPayloadDecoder.fromRegisters(raw, endian=self.endian) elif mb_funcall == 4: # Read Input Registers (FC=04) log.debug("decoder stub FC4 (raw: %s)" % raw) decoder = BinaryPayloadDecoder(raw, endian=self.endian) else: log.debug("Function call not supported: %s" % mb_funcall) decoder = None result = "" if mb_type == 'bitmap': if size == 1: mb_type = 'int8' elif size == 2: mb_type = 'int16' elif size == 2: mb_type = 'int32' elif size == 4: mb_type = 'int64' if decoder is None: log.debug("decode none") result = raw else: try: if mb_type == 'string' or mb_type == 'utf8': result = decoder.decode_string(size) #elif mb_type == 'bitmap': # result = decoder.decode_string(size) elif mb_type == 'datetime': result = decoder.decode_string(size) elif mb_type == 'uint8': result = int(decoder.decode_8bit_uint()) elif mb_type == 'int8': result = int(decoder.decode_8bit_int()) elif mb_type == 'uint16': result = int(decoder.decode_16bit_uint()) elif mb_type == 'int16': result = int(decoder.decode_16bit_int()) elif mb_type == 'uint32': result = int(decoder.decode_32bit_uint()) elif mb_type == 'int32': result = int(decoder.decode_32bit_int()) elif mb_type == 'uint64': result = int(decoder.decode_64bit_uint()) elif mb_type == 'int64': result = int(decoder.decode_64bit_int()) elif mb_type == 'float32' or mb_type == 'float': result = float(decoder.decode_32bit_float()) elif mb_type == 'float64': result = float(decoder.decode_64bit_float()) elif mb_type == 'bit': result = int(decoder.decode_bits()) elif mb_type == 'bool': result = bool(raw[0]) elif mb_type == 'raw': result = raw[0] else: result = raw except ValueError as e: log.exception(e) result = raw return result