コード例 #1
0
 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)
コード例 #2
0
 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)
コード例 #3
0
 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)
コード例 #4
0
 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
コード例 #5
0
 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
コード例 #6
0
 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)
コード例 #7
0
 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)
コード例 #8
0
 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)
コード例 #9
0
 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))
コード例 #10
0
 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)
コード例 #11
0
 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)