JSON

culius
2024-04-30
2024-05-01
  • culius - 2024-04-30

    Hey guys,

    I am trying to write a JSON. First time after login in PLC after download everthing works. But when I want to change values during runtime and try to recreate the JSON nothing happens.

    When forcing the xStart as an impulse i want to recreate it and see 2 as Key3 instead of 1 from the first run.

    Any Idea how to make this work?

    PROGRAM test
    VAR
      factory : JSON.JSONDataFactory;
      eDataFactoryError : JSON.FBF.ERROR;
      pJsonData : POINTER TO JSON.JSONData := factory.Create(eError => eDataFactoryError);
      fb_JBuilder : JSON.JSONBuilder;
      wsValue : WSTRING;
      diRootIndex, diObject1Index : DINT;
      iValue : INT;
      jsonArrayWriter : JSON.JSONByteArrayWriter;
      wsJsonData : WSTRING(1000);
      xFirst : BOOL := TRUE;
    END_VAR
    
    IF xFirst THEN
      fb_JBuilder(pJsonData := pJsonData, diRootObj => diRootIndex);
      wsValue := "Value1";
      fb_JBuilder.SetKeyWithValue("Key1", wsValue, diParentIndex := diRootIndex);
      diObject1Index := fb_JBuilder.SetKeyWithObject("Key2", diParentIndex := diRootIndex);
      iValue := iValue + 1 ;  // -----------!!! secound run should increment key3!!!!------------
      fb_JBuilder.SetKeyWithValue("Key3", iValue, diParentIndex := diObject1Index);
      xFirst := FALSE;
    END_IF
    
    jsonArrayWriter(xExecute := TRUE, pwData := ADR(wsJsonData), udiSize := SIZEOF(wsJsonData), jsonData := pJsonData^, xAsyncMode := FALSE);
    

    Kind Regards

     
  • TimvH

    TimvH - 2024-05-01

    I don't know the details of jsonArrayWriter, but the common behaviour for an xExecute input is that the FB starts on the trigger that it gets TRUE.

    In your case xExecute is never FALSE, so it is never triggered again to start the jsonArrayWriter.

    So change the condition from TRUE to a variable which you set to TRUE with the "xFirst". Then when the jsonArrayWriter is done (xDone), or has an error (xError), then set this variable to FALSE.

     

Log in to post a comment.