JSON

culius
2024-04-30
2024-06-21
  • 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.

     
    πŸ‘
    1
  • culius - 2024-05-21

    Sorry that I haven't replyed to youranwser in so long time. Thank you for explaining the issue. Now everything works without a problem!

     

    Last edit: culius 2024-05-21
    • calviniscoding - 2024-06-21

      Can you post the new code that you have by chance? Also, are you able to send the values of tags dynamically with the RTAC in this JSON format? I'm running into a lot of road blocks with it.

       
    • calviniscoding - 2024-06-21

      Can you post the new code that you have by chance? Also, are you able to send the values of tags dynamically with the RTAC in this JSON format? I'm running into a lot of road blocks with it.

       
    • calviniscoding - 2024-06-21

      Can you post the new code that you have by chance? Also, are you able to send the values of tags dynamically with the RTAC in this JSON format? I'm running into a lot of road blocks with it.

       

Log in to post a comment.