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) with self.plc: result = self.plc.read( 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_notification_decorator_array(self): # type: () -> None """Test decoding of array value by notification decorator""" @self.plc.notification(constants.PLCTYPE_ARR_INT(5)) def callback(handle, name, timestamp, value): self.assertEqual(value, [0, 1, 2, 3, 4]) notification = create_notification_struct( b"\x00\x00\x01\x00\x02\x00\x03\x00\x04\x00") callback(pointer(notification), "")
def test_arrays(self): n = 7 self.assertSizeOf(constants.PLCTYPE_ARR_REAL(n), 4 * n) self.assertSizeOf(constants.PLCTYPE_ARR_LREAL(n), 8 * n) self.assertSizeOf(constants.PLCTYPE_ARR_BOOL(n), 1 * n) self.assertSizeOf(constants.PLCTYPE_ARR_INT(n), 2 * n) self.assertSizeOf(constants.PLCTYPE_ARR_UINT(n), 2 * n) self.assertSizeOf(constants.PLCTYPE_ARR_SHORT(n), 2 * n) self.assertSizeOf(constants.PLCTYPE_ARR_USHORT(n), 2 * n) self.assertSizeOf(constants.PLCTYPE_ARR_DINT(n), 4 * n) self.assertSizeOf(constants.PLCTYPE_ARR_UDINT(n), 4 * n) self.assertSizeOf(constants.PLCTYPE_ARR_USINT(n), 1 * n)
def test_init_by_name_array(self): """Test symbol creation when it's an array""" var = PLCVariable( "ArrayVar", ads_type=constants.ADST_INT16, # dataType does not represent # array unfortunately symbol_type="ARRAY [1..5] OF INT", # Array looks like this in PLC ) var.plc_type = constants.PLCTYPE_ARR_INT(5) # Have to do this # manually self.handler.add_variable(var) self.plc.open() symbol = AdsSymbol(self.plc, name=var.name) # Verify looked up info self.assertEqual(var.name, symbol.name) self.assertEqual(var.index_group, symbol.index_group) self.assertEqual(var.index_offset, symbol.index_offset) self.assertEqual(var.plc_type, symbol.plc_type) self.assertEqual(var.symbol_type, symbol.symbol_type) self.assertIsNone(symbol.comment) my_list = symbol.read() self.assertIsInstance(my_list, list) self.assertEqual(5, len(my_list)) my_list[4] = 420 symbol.write(my_list) # Modify array my_list2 = symbol.read() # Read again self.assertEqual(my_list, my_list2) self.assertAdsRequestsCount(4) # A READWRITE (for info), READ,