def test_read_check_length(self): # Write data shorter than what should be read ads.write( self.endpoint, value=1, index_group=constants.INDEXGROUP_DATA, index_offset=1, plc_datatype=constants.PLCTYPE_USINT, ) with self.assertRaises(RuntimeError): # Since the length is checked, this must give an error ads.read( self.endpoint, index_group=constants.INDEXGROUP_DATA, index_offset=1, plc_datatype=constants.PLCTYPE_UINT, check_length=True, ) # If the length is not checked, no error should be raised value = ads.read( self.endpoint, index_group=constants.INDEXGROUP_DATA, index_offset=1, plc_datatype=constants.PLCTYPE_UINT, check_length=False, ) self.assertEqual(value, 1)
def test_read_array_return_ctypes(self): # Make request to read array data from a random index (the test server will # return the same thing regardless) result = ads.read( self.endpoint, index_group=constants.INDEXGROUP_DATA, index_offset=1, plc_datatype=constants.PLCTYPE_ARR_INT(5), return_ctypes=True, ) # Retrieve list of received requests from server requests = self.test_server.request_history # Assert that the server received a request self.assertEqual(len(requests), 1) # Assert that the server received the correct command self.assert_command_id(requests[0], constants.ADSCOMMAND_READ) # The string buffer is 1024 bytes long, this will be filled with \x0F # and null terminated with \x00 by our test server. The \x00 will get # chopped off during parsing to python string type expected_result_raw = b"\x0F" * 9 + b"\x00" expected_result = list(struct.unpack("<hhhhh", expected_result_raw)) self.assertEqual([x for x in result], expected_result)
def test_read_string(self): # Make request to read data from a random index (the test server will # return the same thing regardless) result = ads.read( self.endpoint, index_group=constants.INDEXGROUP_DATA, index_offset=1, plc_datatype=constants.PLCTYPE_STRING, ) # Retrieve list of received requests from server requests = self.test_server.request_history # Assert that the server received a request self.assertEqual(len(requests), 1) # Assert that the server received the correct command self.assert_command_id(requests[0], constants.ADSCOMMAND_READ) # The string buffer is 1024 bytes long, this will be filled with \x0F # and null terminated with \x00 by our test server. The \x00 will get # chopped off during parsing to python string type expected_result = "\x0F" * 1023 self.assertEqual(result, expected_result)
def test_read_uint(self): # Make request to read data from a random index (the test server will # return the same thing regardless) result = ads.read( self.endpoint, index_group=constants.INDEXGROUP_DATA, index_offset=1, plc_datatype=constants.PLCTYPE_UDINT ) # Retrieve list of received requests from server requests = self.test_server.request_history # Assert that the server received a request self.assertEqual(len(requests), 1) # Assert that the server received the correct command self.assert_command_id(requests[0], constants.ADSCOMMAND_READ) # Test server just returns repeated bytes of 0x0F terminated with 0x00 expected_result = struct.unpack('<I', '\x0F\x0F\x0F\x00'.encode('utf-8'))[0] self.assertEqual(result, expected_result)
def test_read_uint(self): # Make request to read data from a random index (the test server will # return the same thing regardless) result = ads.read(self.endpoint, index_group=constants.INDEXGROUP_DATA, index_offset=1, plc_datatype=constants.PLCTYPE_UDINT) # Retrieve list of received requests from server requests = self.test_server.request_history # Assert that the server received a request self.assertEqual(len(requests), 1) # Assert that the server received the correct command self.assert_command_id(requests[0], constants.ADSCOMMAND_READ) # Test server just returns repeated bytes of 0x0F terminated with 0x00 expected_result = struct.unpack('<I', '\x0F\x0F\x0F\x00'.encode('utf-8'))[0] self.assertEqual(result, expected_result)
def test_read_array(self): # Make request to read array data from a random index (the test server will # return the same thing regardless) result = ads.read( self.endpoint, index_group=constants.INDEXGROUP_DATA, index_offset=1, plc_datatype=constants.PLCTYPE_ARR_INT(5) ) # Retrieve list of received requests from server requests = self.test_server.request_history # Assert that the server received a request self.assertEqual(len(requests), 1) # Assert that the server received the correct command self.assert_command_id(requests[0], constants.ADSCOMMAND_READ) # The string buffer is 1024 bytes long, this will be filled with \x0F # and null terminated with \x00 by our test server. The \x00 will get # chopped off during parsing to python string type expected_result = list(struct.unpack('<hhhhh', b'\x0F' * 9 + b'\x00')) self.assertEqual(result, expected_result)