Esempio n. 1
0
    def parse_response(self, response):
        """Extract the pdu from the Modbus RTU response"""
        #if len(response) < 3:
        #raise ModbusInvalidResponseError("Response length is invalid {0}".format(len(response)))

        (self._response_address, ) = struct.unpack(">B", response[0:1])

        if self._request_address != self._response_address:
            raise ModbusInvalidResponseError(
                "Response address {0} is different from request address {1}".
                format(self._response_address, self._request_address))

        (crc, ) = struct.unpack(">H", response[-2:])

        if crc != utils.calculate_crc(response[:-2]):
            raise ModbusInvalidResponseError("Invalid CRC in response")

        return response[1:-2]
Esempio n. 2
0
 def parse_response(self, response):
     """Extract the pdu from the Modbus TCP response"""
     if len(response) > 6:
         mbap, pdu = response[:7], response[7:]
         self._response_mbap.unpack(mbap)
         self._response_mbap.check_response(self._request_mbap, len(pdu))
         return pdu
     else:
         raise ModbusInvalidResponseError("Response length is only {0} bytes. ".format(len(response)))