예제 #1
0
 def build_request(self, pdu, slave):
     """Add the Modbus RTU part to the request"""
     self._request_address = slave
     if (self._request_address < 0) or (self._request_address > 255):
         raise InvalidArgumentError("Invalid address {0}".format(self._request_address))
     data = struct.pack(">B", self._request_address) + pdu
     crc = struct.pack(">H", utils.calculate_crc(data))
     return data + crc
예제 #2
0
 def build_request(self, pdu, slave):
     """Add the Modbus RTU part to the request"""
     self._request_address = slave
     if (self._request_address < 0) or (self._request_address > 255):
         raise InvalidArgumentError("Invalid address {0}".format(self._request_address))
     data = struct.pack(">B", self._request_address) + pdu
     crc = struct.pack(">H", utils.calculate_crc(data))
     return data + crc
예제 #3
0
 def testWriteSingleCoilInvalidValue(self):
     """Check that an error is raised when writing a coil with an invalid value"""
     bad_query = struct.pack(">BBHH", 1, modbus_tk.defines.WRITE_SINGLE_COIL, 0, 1)
     crc = struct.pack(">H", utils.calculate_crc(bad_query))
     bad_query += crc
     self.master.set_verbose(True)
     self.master._send(bad_query)
     response = self.master._recv()
     self.assertEqual(response[:-2], struct.pack(">BBB", 1, modbus_tk.defines.WRITE_SINGLE_COIL+128, 3))
예제 #4
0
 def testWriteSingleCoilInvalidValue(self):
     """Check that an error is raised when writing a coil with an invalid value"""
     bad_query = struct.pack(">BBHH", 1, modbus_tk.defines.WRITE_SINGLE_COIL, 0, 1)
     crc = struct.pack(">H", utils.calculate_crc(bad_query))
     bad_query += crc
     self.master.set_verbose(True)
     self.master._send(bad_query)
     response = self.master._recv()
     self.assertEqual(response[:-2], struct.pack(">BBB", 1, modbus_tk.defines.WRITE_SINGLE_COIL+128, 3))
예제 #5
0
 def compute_metric(self, trace):
     import struct
     incorrect_crc_count = 0
     for pkt in trace.packets:
         pkt_crc, = struct.unpack('>H',pkt.data[-2:])
         correct_crc = modbus_utils.calculate_crc(pkt.data[:-2])
         if pkt_crc != correct_crc:
             incorrect_crc_count += 1
     percent_incorrect = incorrect_crc_count/float(len(trace.packets))
     return percent_incorrect
예제 #6
0
    def parse_request(self, request):
        """Extract the pdu from the Modbus RTU request"""
        if len(request) < 3:
            raise ModbusInvalidRequestError("Request length is invalid {0}".format(len(request)))

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

        (crc, ) = struct.unpack(">H", request[-2:])
        if crc != utils.calculate_crc(request[:-2]):
            raise ModbusInvalidRequestError("Invalid CRC in request")

        return self._request_address, request[1:-2]
예제 #7
0
    def parse_request(self, request):
        """Extract the pdu from the Modbus RTU request"""
        if len(request) < 3:
            raise ModbusInvalidRequestError("Request length is invalid {0}".format(len(request)))

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

        (crc, ) = struct.unpack(">H", request[-2:])
        if crc != utils.calculate_crc(request[:-2]):
            raise ModbusInvalidRequestError("Invalid CRC in request")

        return self._request_address, request[1:-2]
예제 #8
0
 def testReadWithWrongFunction(self):
     """check that an error is raised when sending a query with an invalid function code"""
     self.assertRaises(modbus_tk.modbus.ModbusFunctionNotSupportedError, self.master.execute, 1, 55, 0, 10)
     bad_query = struct.pack(">BB", 1, 55)
     crc = struct.pack(">H", utils.calculate_crc(bad_query))
     bad_query += crc
     try:
         self.master._send(bad_query)
         self.master._recv()
     except modbus_tk.modbus.ModbusError, ex:
         self.assertEqual(ex.get_exception_code(), 1)
         return
예제 #9
0
 def testReadWithWrongFunction(self):
     """check that an error is raised when sending a query with an invalid function code"""
     self.assertRaises(modbus_tk.modbus.ModbusFunctionNotSupportedError, self.master.execute, 1, 55, 0, 10)
     bad_query = struct.pack(">BB", 1, 55)
     crc = struct.pack(">H", utils.calculate_crc(bad_query))
     bad_query += crc
     try:
         self.master._send(bad_query)
         self.master._recv()
     except modbus_tk.modbus.ModbusError as ex:
         self.assertEqual(ex.get_exception_code(), 1)
         return
예제 #10
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]
예제 #11
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])
        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]
예제 #12
0
 def build_response(self, response_pdu):
     """Build the response"""
     self._response_address = self._request_address
     data = struct.pack(">B", self._response_address) + response_pdu
     crc = struct.pack(">H", utils.calculate_crc(data))
     return data + crc
예제 #13
0
 def build_response(self, response_pdu):
     """Build the response"""
     self._response_address = self._request_address
     data = struct.pack(">B", self._response_address) + response_pdu
     crc = struct.pack(">H", utils.calculate_crc(data))
     return data + crc