Weird JSON file reading behaviour

Hi, I am making a ST task where I send messages to AWS IoT Core. If the PLC is not connected to AWS via MQTT anymore (no internet, etc.) It will then buffer these messages by saving them as JSON files to the USER partition. After reconnecting, the program will first open, read, close, send content to AWS and delete all files.

I use the fileRead FB to read files.
I have noticed some strange behaviour when reading to buffered files. (This is happening at random.)

This is a normal JSON message, correctly read out:

This is a weird case (corrupted?) where it suddenly adds an extra “}”:

Sometimes I even get stuff like this output:

this is the part of the code that reads files:

these are the variables that go with it:
image

When a corrupted messages arrives in AWS IoT Core, it doesn’t get parsed because the format isn’t correct. Anyone have any idea what is happening here?

Hi,

from only having a code snippet it’s hard to say.

But most often such issues happen when a buffer (=memory / variable) is used more then once but isn’t initialized before next usage.

So I would propose to clear (= overwrite with 0) all buffers before re-usage, for example “file_read_buffer” with “brsmemset(ADR(file_read_buffer), 0, SIZEOF(file_read_buffer))” or similar … because I assume, that the extra / unwanted characters are fragments from a “before-usage” of the buffers with a longer content in addition with a missing string termination (= 0 at the end of the string length) at the end of the new content.

Best regards!

1 Like

Hi,

Got it working by resetting “file_read_buffer”.

brsmemset(ADR(file_read_buffer), 0, SIZEOF(file_read_buffer));

I was resetting it at a wrong time in the code at first, because i was getting empty files.

Resetting it at a right moment in the code completally fixed everything.

Thanks for your response.

:slight_smile:

3 Likes