Parsing JSON with "FbWrite_ToIEC_ByRule" - Wago CC100 + Codesys V3

Hello everyone, I have a problem here that I’m not quite sure how to proceed with. Maybe someone could give me a hand.

An API request was used to read the JSON response from https://api.awattar.at/v1/marketdata into the controller via the function block “fbHTTP_Client_Curl” → This worked quickly.

This information now needs to be mapped to the corresponding target array via the function block “FbWrite_ToIEC_ByRule”. The receive buffer (sresponsebuffer) is a String variable with a size of 10,000 bytes. The target array “strompreis” in Codesys has a size of 44 (0..43) and the data type “Strompreisdaten”.

The Problem: As soon as the block “FbWrite_ToIEC_ByRule” is triggered, it starts running but then stops at “iActualArrayRow”=13 and switches to fault mode.

Here are the declarations:

//Structure Strompreisdaten

{attribute ‘pack_mode’ := ‘1’}
TYPE strompreisdaten :
STRUCT
start_timestamp :LINT; //in ms
end_timestamp :LINT; //in ms
marketprice :REAL;
unit :STRING(10);
END_STRUCT
END_TYPE

//Declaration:

var
stringpath:‘/data’;
strompreis [0..43] of strompreisdaten;

//Rule definition for electricity price query
arrstromprrules :ARRAY[0..MAX_ARRAY_ELEMENTS_CUSTOM] OF typRuleBase:= [
(spath:=‘start_timestamp’, iconverttype:=eLINT),
(spath:=‘end_timestamp’, iconverttype:=eLINT),
(spath:=‘marketprice’, iconverttype:=ereal),
(spath:=‘unit’, iconverttype:=estring)
];

end_var

VAR CONSTANT
MAX_ARRAY_ELEMENTS_CUSTOM :INT:=3;
END_VAR

Program code:

udibuffersize:=strlength(sbuffer:=sresponsebuffer,udisize:=SIZEOF(sresponsebuffer));

//Parse REST API response “json-string” with “FbWrite_ToIEC_ByRule”

auswertapianfrage_strompreis(xtrigger:=xtriggerjsonparsing,spathbase:=stringpath,
arule:=arrstromprrules,pdatain:=ADR(sResponsebuffer),udiSizeDataIn:=udibuffersize,
piecvariable:=ADR(strompreis),
udiSizeIecVariable:=SIZEOF(strompreis),iArraySizeIecVariable:=44,
xerror=>xErrorJSONParsing,ostatus=>ostatus,iActualArrayRow=>iActualArrayRow);

//negative edge
ftrig1temp:= NOT xtriggerjsonparsing AND ftrig1stat;
ftrig1stat:= xtriggerjsonparsing;

//Evaluation of Parser Status

IF xerrorjsonparsing THEN
istat:=0;//Abort + Error handling
xtriggerjsonparsing:=FALSE;
ELSE
;
END_IF

IF NOT xerrorjsonparsing AND ftrig1temp THEN
istat:=0;//Parsing was successful
ELSE
;
END_IF

It would be great if someone could help me out. I’ve reached my wit’s end. I had parsed API requests before, but this time… Best regards.

Hello, I’m Matthew Kim, based in Korea.
I’ve encountered a similar issue myself (though my data was more complex). I initially tried to solve it using only CODESYS, but I ran into problems with parsing—specifically, the data stream would cut off at unexpected points. To get around this, I took a different approach:

  1. Install Node-RED (via Docker) on the CC100.
  2. Use Node-RED to handle the parsing.
  3. Transmit the data via Modbus.
  4. Receive the data in CODESYS using Modbus TCP.

This method resolved the issue of the parsing unexpectedly stopping (or cutting off), and the results were quite satisfactory.

Just keep in mind that you’ll need to pay a bit more attention to the Modbus handshake process.
I’m no expert in Node-RED myself, but Gemini can certainly help you with that. Once I decided to go this route, I was able to get it implemented in just two days.

Although the actual project has been delayed due to the client’s circumstances, this approach proved much faster to implement than continuing to struggle with the limitations within CODESYS.

If anyone else has managed to solve this kind of issue directly within CODESYS, please feel free to leave a comment!

Hi there,

Probably a string size miss match.

Try to use: (spath:=‘unit’, iconverttype:=eString10)

Additionally you could try to use {attribute ‘pack_mode’ := ‘0’} , or is there a specific fix/reason why you´ve chosen to use the packed no padding mode so that fields sit back to back?

BR Alexander

Hello wagoalex, I tried it with the "estring10" like this. The whole thing works perfectly now. Thank you very much for the tip. Best regards.