FORUM CTRLX AUTOMATION
ctrlX World Partner Apps for ctrlX AUTOMATION
03-14-2024 08:04 AM
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
Solved! Go to Solution.
03-14-2024 08:12 AM
As I do not know the library, just a suggestion.
Could it be that the variable used as an output of the function block is not emptied by the application or the FB and so the new message is only overwriting a part of it? this could result in the additional curly bracket at the end if a number inside the string could be a digit shorter.
03-14-2024 02:07 PM
Hi @AndroidzZ ,
If you cannot find the source of the problem, you could correct for this by looping over the string and pushing each curly brace & bracket to seperate stacks. You then pop off the stack when you encounter an opposing brace or bracket. If you have any items remaining on either stack, you know you need to remove them from the end.
03-15-2024 09:39 AM
I addition to the 2 comments above you should do the following:
03-18-2024 01:50 PM
Hello,
I suspect that the problem is in this corner. at the moment I receive a string. If I get a shorter string after this I still see remnants of the previous (longer) string. I have tried to empty the string with: <string> := '';
I've also tried the MemCpy function but the string still shows remnants of the previous longer string.
The problem seems clear now only finding a solution for it is not yet successful. What suggestions could I try?
Kind regards
03-18-2024 02:13 PM - edited 03-18-2024 02:14 PM
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
04-02-2024 05:26 PM
Can you provide us with a copy of the string (as text) or an example?
In addition, which MQTT version is used here?
(Which MQTT parameters are used - Libman, MQTTParams)?
04-11-2024 10:05 AM