Valid JSON is sent via AWS MQTT and recieved in PLC as an Invalid JSON structure

Hello,

We receive a JSON payload from an overhead system, every 15 minutes, via AWS MQTT. We receive this payload via the FB "AWS_IOT.AWSIoTSubscribe". Well, we noticed that several times a day, the payload coming in has one brace too many at the end resulting in invalid JSON structure and thus cannot be parsed. We can reproduce this error message. We have a payload that 100% of the time is sent as Valid JSON from AWS and comes in on the subscribe FB as Invalid JSON (1 curly brache to many at the end). And this while there are no additional process steps in the payload route.

We are using the CodeSyS IIOT libarary (1.9.0.0). CORE version: 1.20, PLC App 1.20.3, CodeSyS version .3.5.19.10.

See attachment: the left text is recieved as string on our PLC side en the right text is being send by the AWS broker.

Kind regards

Best reply by HmiGuide

Ok. Now this sounds like a string handling problem.

I expect that the function you are using is just copying the received characters, but not the end of string character. That's pretty common for many CoDeSys features. While a string needs a end of string (Ascii code =0) character after the last letter, you have to put this manually after the last letter. The easiest way is to use sysMemSet() from library SysMem. If you do not set this character, the string continues until the next random position of char(0).

 

VAR
  strJson: STRING(1000); // sizeOf(strJson) = 1000+1
END_VAR

// fill the complete string with Char(0)=End of string char
SysMemSet(pDest:=ADR(strJson), udiValue:=0, udiCount:=SIZEOF(strJson));
// insert here your code which copies the data into strJson
// e.g. receive JSON string

 

 

View original
TextCompare.JPG
310.82KB
7 replies