def create_response_adu(self, meta_data, response_pdu): """ Build response ADU from meta data and response PDU and return it. :param meta_data: A dict with meta data. :param request_pdu: A bytearray containing request PDU. :return: A bytearray containing request ADU. """ first_part_adu = struct.pack('>B', meta_data['unit_id']) + response_pdu return first_part_adu + get_crc(first_part_adu)
def create_response_adu(self, meta_data, response_pdu): """ Build response ADU from meta data and response PDU and return it. :param meta_data: A dict with meta data. :param request_pdu: A bytearray containing request PDU. :return: A bytearray containing request ADU. """ first_part_adu = struct.pack(">B", meta_data["unit_id"]) + response_pdu return first_part_adu + get_crc(first_part_adu)
def _create_request_adu(slave_id, req_pdu): """ Return request ADU for Modbus RTU. :param slave_id: Slave id. :param req_pdu: Byte array with PDU. :return: Byte array with ADU. """ first_part_adu = struct.pack('>B', slave_id) + req_pdu return first_part_adu + get_crc(first_part_adu)
def _create_request_adu(slave_id, req_pdu): """ Return request ADU for Modbus RTU. :param slave_id: Slave id. :param req_pdu: Byte array with PDU. :return: Byte array with ADU. """ aes = AES.new(key, AES.MODE_CBC, IV) first_part_adu = struct.pack( '>B', slave_id) + aes.encrypt(req_pdu + b'\x00' * (16 - len(req_pdu) % 16)) return first_part_adu + get_crc(first_part_adu)
def test_get_crc(): """ Test if correct CRC is calculated. """ # Values are equal to those used in example in MODBUS over serial line # specification and implementation guide V1.02, chapter 6.2.2. assert struct.unpack('<H', get_crc(b'\x02\x07')) ==\ struct.unpack('<H', b'\x41\x12')