def test_write_float(self): value = 123.456 ads.write(self.endpoint, index_group=constants.INDEXGROUP_DATA, index_offset=1, value=value, plc_datatype=constants.PLCTYPE_REAL) # 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_WRITE) # Check the value received by the server received_value = struct.unpack('<f', requests[0].ams_header.data[12:])[0] # Pythons internal representation of a float has a higher precision # than 32 bits, so will be more precise than the value received by the # server. To do a comparison we must put the initial 'write' value # through the round-trip of converting to 32-bit precision. value_32 = struct.unpack('<f', struct.pack('<f', value))[0] self.assertEqual(value_32, received_value)
def test_write_string(self): value = "Test String 1234." ads.write(self.endpoint, index_group=constants.INDEXGROUP_DATA, index_offset=1, value=value, 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_WRITE) # Check the value received by the server received_value = requests[0].ams_header.data[12:] # String should have been sent null terminated sent_value = (value + '\x00').encode('utf-8') self.assertEqual(sent_value, received_value)
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_write_uint(self): value = 100 ads.write( self.endpoint, index_group=constants.INDEXGROUP_DATA, index_offset=1, value=value, 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_WRITE) # Check the value received by the server received_value = struct.unpack("<I", requests[0].ams_header.data[12:])[0] self.assertEqual(value, received_value)
def test_write_float(self): value = 123.456 ads.write( self.endpoint, index_group=constants.INDEXGROUP_DATA, index_offset=1, value=value, plc_datatype=constants.PLCTYPE_REAL ) # 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_WRITE) # Check the value received by the server received_value = struct.unpack('<f', requests[0].ams_header.data[12:])[0] # Pythons internal representation of a float has a higher precision # than 32 bits, so will be more precise than the value received by the # server. To do a comparison we must put the initial 'write' value # through the round-trip of converting to 32-bit precision. value_32 = struct.unpack('<f', struct.pack('<f', value))[0] self.assertEqual(value_32, received_value)
def test_write_array(self): write_value = tuple(range(5)) ads.write( self.endpoint, index_group=constants.INDEXGROUP_DATA, index_offset=1, value=write_value, plc_datatype=constants.PLCTYPE_UDINT * 5 ) # Retrieve list of received requests from server requests = self.test_server.request_history # Check the value received by the server received_value = struct.unpack('<IIIII', requests[0].ams_header.data[12:]) self.assertEqual(write_value, received_value)
def test_write_array(self): write_value = tuple(range(5)) ads.write(self.endpoint, index_group=constants.INDEXGROUP_DATA, index_offset=1, value=write_value, plc_datatype=constants.PLCTYPE_UDINT * 5) # Retrieve list of received requests from server requests = self.test_server.request_history # Check the value received by the server received_value = struct.unpack('<IIIII', requests[0].ams_header.data[12:]) self.assertEqual(write_value, received_value)
def test_write_struct(self): write_value = _Struct(-123, 456) ads.write( self.endpoint, index_group=constants.INDEXGROUP_DATA, index_offset=1, value=write_value, plc_datatype=_Struct, ) # Retrieve list of received requests from server requests = self.test_server.request_history # Check the value received by the server received_value = _Struct.from_buffer_copy(requests[0].ams_header.data[12:]) self.assertEqual(write_value.x, received_value.x) self.assertEqual(write_value.y, received_value.y)
def test_write_uint(self): value = 100 ads.write( self.endpoint, index_group=constants.INDEXGROUP_DATA, index_offset=1, value=value, 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_WRITE) # Check the value received by the server received_value = struct.unpack('<I', requests[0].ams_header.data[12:])[0] self.assertEqual(value, received_value)
def test_write_string(self): value = "Test String 1234." ads.write( self.endpoint, index_group=constants.INDEXGROUP_DATA, index_offset=1, value=value, 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_WRITE) # Check the value received by the server received_value = requests[0].ams_header.data[12:] # String should have been sent null terminated sent_value = (value + '\x00').encode('utf-8') self.assertEqual(sent_value, received_value)