def testBuildRequest(self): """Test the string returned by building a request""" query = modbus_rtu.RtuQuery() request = query.build_request(to_data(""), 0) expected = struct.pack(">B", 0) expected_crc = crc16_alternative(expected) expected += struct.pack(">H", expected_crc) self.assertEqual(expected, request)
def testBuildRequestWithSlave(self): """Test the string returned by building a request with a slave""" query = modbus_rtu.RtuQuery() for i in range(0, 256): request = query.build_request(to_data(""), i) expected = struct.pack(">B", i) expected_crc = crc16_alternative(expected) expected += struct.pack(">H", expected_crc) self.assertEqual(expected, request)
def testParseWrongCrcResponse(self): """Test an error is raised if wrong transaction id""" query = modbus_rtu.RtuQuery() pdu = to_data("a") request = query.build_request(pdu, 5) response = struct.pack(">B" + str(len(pdu)) + "s", 5, pdu) response_crc = crc16_alternative(response)+1 response += struct.pack(">H", response_crc) self.assertRaises(modbus_tk.modbus.ModbusInvalidResponseError, query.parse_response, response)
def testBuildResponse(self): """Test that the response of an request is build properly""" query = modbus_rtu.RtuQuery() i = 0 for pdu in ["", "a", "a" * 127, "abcdefghi"]: request = query.build_request(to_data(pdu), i) response = query.build_response(to_data(pdu)) response_pdu = query.parse_response(response) self.assertEqual(to_data(pdu), response_pdu) i += 1
def testParseRequest(self): """Test that Modbus Rtu part of the request is understood""" query = modbus_rtu.RtuQuery() i = 0 for pdu in ["", "a", "a" * 127, "abcdefghi"]: request = query.build_request(to_data(pdu), i) (slave, extracted_pdu) = query.parse_request(request) self.assertEqual(extracted_pdu, to_data(pdu)) self.assertEqual(slave, i) i += 1
def testParseWrongSlaveResponse(self): """Test an error is raised if the slave id is wrong""" query = modbus_rtu.RtuQuery() pdu = "a" request = query.build_request(pdu, 5) response = struct.pack(">B" + str(len(pdu)) + "s", 8, pdu) response_crc = crc16_alternative(response) response += struct.pack(">H", response_crc) self.assertRaises(modbus_tk.modbus.ModbusInvalidResponseError, query.parse_response, response)
def testBuildRequestWithPdu(self): """Test the request returned by building a request with a pdu""" query = modbus_rtu.RtuQuery() for i in xrange(247): for pdu in ["", "a", "a" * 127, "abcdefghi"]: request = query.build_request(pdu, i) expected = struct.pack(">B" + str(len(pdu)) + "s", i, pdu) expected_crc = crc16_alternative(expected) expected += struct.pack(">H", expected_crc) self.assertEqual(expected, request)
def testParseRespone(self): """Test that Modbus Rtu part of the response is understood""" query = modbus_rtu.RtuQuery() for i in xrange(247): for pdu in ["", "a", "a" * 127, "abcdefghi"]: request = query.build_request("a", i) response = struct.pack(">B" + str(len(pdu)) + "s", i, pdu) response_crc = crc16_alternative(response) response += struct.pack(">H", response_crc) extracted = query.parse_response(response) self.assertEqual(extracted, pdu)
def testParseTooShortRequest(self): """Test an error is raised if the request is too short""" query = modbus_rtu.RtuQuery() for i in range(3): self.assertRaises(modbus_tk.modbus.ModbusInvalidRequestError, query.parse_request, to_data("a" * i))
def testParseTooShortRespone(self): """Test an error is raised if the response is too short""" query = modbus_rtu.RtuQuery() for i in range(3): self.assertRaises(modbus_tk.modbus.ModbusInvalidResponseError, query.parse_response, "a" * i)
def testBuildRequestWithInvalidSlave(self): """Test that an error is raised when invalid slave is passed""" query = modbus_rtu.RtuQuery() for i in ([256, -1, 312, 3541, 65536, 65656]): self.assertRaises(modbus_tk.modbus.InvalidArgumentError, query.build_request, "", i)