OPC writing errors with jsonSet() in nested folders

I am hoping someone can point me in the right direction, or offer up some suggestions. I are attempting to write to a Siemens PLC over OPC using nested derived tags in UDT folder.
Root derived tags are good with read and writes but the nested tags will read fine but when written to, I get… Bad(“Bad_EncodingError: Encoding halted because of invalid data in the objects being serialized.”)

I feel like I’m missing something very basic, here’s the setup:

UDT Structure
_sourceOPC = JSON value for name/value and objects
Value Source = OPC
Data Type = Document
OPC Server = myOPC
OPC Item Path = myBrowsedPath
Source Tag Path = _sourceOPC
Read Expression = jsonGet({source},“Tag1”)
Write Expression = jsonSet({source},“Tag1”,{value})
_sourceDerived = JSON value for Object1
Source Tag Path = _sourceOPC
Read Expression = jsonGet({source},“Object1”)
Write Expression = jsonSet({source},“Object1”,{value})
Source Tag Path = _sourceDerived
Read Expression = jsonGet({source},“Tag2”)
Write Expression = jsonSet({source},“Tag2”,{value})

Error only occurs on Tag2 writes, Tag2 reads are good


Do tag1 and tag2 have the same configurations? Are you able to write to tag2’s source tag path directly without involving the derived tag? If so, is it possible that there is a datatype mismatch involved?

I feel like it’s a syntax issue in the jsonSet() that is not accessing the object correctly in the parent JSON. In the image, the green writes works but the red does not. To test I created an “_OPC Object1” by browsing the Siemens OPC item path, drilling down in DataBlocksGlobal folder to structure. I confirmed that both the “_derived Object1 from sourceOPC JSON parent” and “_OPC Object1” have the same JSON.

_sourceOPC JSON parent value looks like:

  • [{
    “PLC Scan Value 1 PN_Lot”: “”,
    “PLC Scan Value 2 PN_Lot”: “”,
    “PLC_EDC Stock Counters”: { “Anchor Bolts”: 26981, “nextName”:0}},Good, Thu Apr 28 10:19:43 EDT 2022 (1651155583066)]

_derived Object1 from sourceOPC JSON parent :

  • Write Expression = jsonSet({source},“PLC_EDC Stock Counters”,{value})

derived Name1 from derived Object1

  • Write Expression = jsonSet({source},“Anchor Bolts”,{value})

Are my write expressions correct?

The expressions look fine to me, but troubleshooting the problem as a whole may require a closer look. I would suggest contacting support to get a more thorough investigation.

will do, thank you for your feedback

I’m having the same problem with writing to nested derived tags. Did you ever get a solution on this from support?

I am still a bit fuzzy on this, we are reading/writing to Siemens PLC and apparently there is some issues with how the white spaces in the key:value are handled. Reading doesn’t have give me issues but when writing we get: “‘Bad(“Bad_EncodingError: Encoding halted because of invalid data in the objects being serialized.”)’”

I am having positive results using the _sourceOPC JSON parent as the source and then using following:
jsonGet({source},“PLC_EDC Stock Counters[‘Anchor Bolts’]”)
jsonSet({source},“PLC_EDC Stock Counters[‘Anchor Bolts’]”,{value})

jsonSet({source},“PLC_EDC Stock Counters.Anchor Bolts”,{value}) gives me encoding error.

Hope this helps.

Thank you.

We are also using Siemens PLC and get the “Bad_EncodingError”. When I look at the logs I can see that on writing the derived object doesn’t parse correctly (newlines are parsed as “\n” and isn’t correct json format).

I tried your solution:
jsonSet({source},“PLC_EDC Stock Counters[‘Anchor Bolts’]”,{value})
and the Bad_Encoding error disappeared, but instead I got “Bad(“Bad_NotWritable: The access level does not allow writing to the Node.”)”… At least I’m one step closer to something.

Actually it was working, the NotWritable error was just because not all of the tags in PLC was set to writable.

1 Like