This follows on from the discussion here, reading whole data structures from siemens PLC OPC server for better performance: OPC UA struct data type? - #2 by Kevin.Herron
This works very well when reading large numbers of tags however I can't find a way to read an array from the JSON source tag into a Float Array. I can access individual items in the array but when I try to read the whole array I get this:
[null, Error_TypeConversion("Cannot coerce value '[
]' into type: class [Ljava.lang.Float;"), Wed Mar 01 01:58:31 GMT 2023 (1677635911284)]
Hmm, it looks like the way the
jsonGet function is implemented it will only return primitives or other JSON elements, it doesn't know anything about primitive arrays.
Best you can do for now is use a Document tag instead of Float Array, or pull out the elements individually.
We'll have to add some implicit conversion logic for going from a JSON element that turns out to be an array of numbers to a primitive array. The behavior of
jsonGet unfortunately cannot be modified without breaking compatibility.
I have tried to read and write arrays and other data types from document tags, the ones that are generated when reading siemens data structures. But get an error when trying to write back to to them.
I created 2 identical data structures in document tag type. One that is generated by reading the structure from a siemens OPC server and another exactly the same but as a memory tag. Reading and writing works fine for the memory tag but errors for the siemens OPC tag when writing:
I would be really useful to read and write to the PLC using document tags. Infrequently accessed data like configuration parameters could be read and written to on demand without bloating the tag count. Perspective is also based around JSON docs so its inherently a great way of working with data.
My intention in the first place was to use a tag type like document or array that I can add whatever I need to from the PLC side and have it automatically appear in ignition, eg setpoints. It means I can define one generic ignition udt and views for many different controls without having to create specific ones every time.
Any ideas? Something I've done wrong?
You didn't do anything wrong, normally the document/structure setup is read-write compatible, but it looks like it's broken with embedded arrays. We'll have to do some testing and fix whatever bugs are causing the issue.
Its not just the embedded arrays that can't be written to, trying to write to the int1 or real1 data values also causes the same error, again just with the Siemens OPC document tag.
I'm not sure what the issue is in that case, works fine here. Can you share the full UDT definition from TIA portal? and your Ignition tag setup?
I'm just writing to the tag directly in the Tag Browser, I'm not sure what you've got going with the Perspective props mirroring it.
Text Area with bi-directional binding to the Document tag:
In this setup I can modify the array elements. But if I make a derived tag targeting one of the array members that's where things start to break.
If I also use a text area bidirectionally linked to the document tag then writing also works for me. It seems to be only a problem when writing to a document using a bidirectional binding from a custom object.
This change script reads the data on the tagData object and writes it back to the tag. You can see in the output that it tries to write the objectWrapper back to the tag and fails.
If I encode the object wrapper into a JSON string with the system.util.jsonEncode function and write that back then it writes fine.
This suggests that when bidirectional bindings are use to write back to document tags the object wrapper is not been encoded back to JSON, or something like that. I'm guessing that the text area works because it is a string that is been written back to the document tag which can be encoded to JSON.