Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access arrays?

Tags:

python

opc-ua

I have an OPC server running on a milling machine and want to access the names of the tools which are currently available in the machine.

I do know that the names of the variables I am looking for are

Sinumerik/Tool/Catalogue/toolIdent[1]
Sinumerik/Tool/Catalogue/toolIdent[2]

and so on. I can see the corresponding values in a viewer such as uaexpert.

While I am able to access

Sinumerik/Tool/Catalogue/toolIdent

by successively walking down from the root, using commands such as

children=Position.get_children()
for child in children:
    Position=child

this approach does not work for toolIdent[1] since this is not a child.

How can I access these values of the array?

EDIT: One additional remark: The name of the tool is stored as a value of the node, not as a variable. I have come across the function get_array_dimensions, but this only seems to work for variables.

EDIT2: I have attached a screenshot of the UAExpert view of the array I am looking for. The three first values of the array are 3D_BLUM, 12, and 98. enter image description here

EDIT3: In UAExpert, I am able to see to content of toolIdent[2] by using the "add custom node" command, selecting a string node and offering " /Tool/Catalogue/toolIdent[2]" as parameter for the NodeId. I am trying to find out how I can do the same using python (preferrably opcua library, and I am offering a bounty for reaching this goal in Python.

like image 319
tfv Avatar asked Nov 22 '19 09:11

tfv


People also ask

How do you access arrays of objects?

A nested data structure is an array or object which refers to other arrays or objects, i.e. its values are arrays or objects. Such structures can be accessed by consecutively applying dot or bracket notation. Here is an example: const data = { code: 42, items: [{ id: 1, name: 'foo' }, { id: 2, name: 'bar' }] };

How do you access elements in an array of arrays?

To access the elements of the inner arrays, you simply use two sets of square brackets. For example, pets[1][2] accesses the 3rd element of the array inside the 2nd element of the pets array.

How do you access all elements in an array?

We can access elements of an array using the index operator [] . All you need do in order to access a particular element is to call the array you created. Beside the array is the index [] operator, which will have the value of the particular element's index position from a given array.


2 Answers

From your screenshots it looks like each of these "array elements" actually has its own Node and NodeId.

You should just be able to read each of these NodeIds:

  • ns=2;s=/Tool/Catalogue/toolIdent[0]
  • ns=2;s=/Tool/Catalogue/toolIdent[1]
  • ns=2;s=/Tool/Catalogue/toolIdent[2]
like image 175
Kevin Herron Avatar answered Oct 30 '22 07:10

Kevin Herron


Based on what I've read, it looks like this isn't an array, but a naming convention. I would start by hacking it:

    for x in range(10):
        if x == 0:
            availTools = [toolIdent]
        else:
            availTools.append(toolIdent[x])

Now you have your available tool in an actual array (availTools) that you can access however you'd like.

like image 40
Kyle Hurst Avatar answered Oct 30 '22 07:10

Kyle Hurst