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.