Welcome to our new forum
All users of the legacy CODESYS Forums, please create a new account at account.codesys.com. But make sure to use the same E-Mail address as in the old Forum. Then your posts will be matched. Close

http data in JSON format - encoding

damian177
2022-01-27
2022-01-28
<< < 1 2 (Page 2 of 2)
  • damian177 - 2022-01-28

    In new function WSTRING_TO_JSONVALUE I have like you wrote. In property
    JSONVAR_16.CharString:

    {attribute 'monitoring':='call'}PROPERTY CharString : WSTRING(GPL_JSON_16.MAX_VALUE_SIZE)
    

    JSONVAR_16.CharString.Get:

    VAR
        TempVar:        WSTRING(255);
    END_VAR
    SysMemCpy(
        pDest:= ADR(TempVar), 
        pSrc:= ADR(_InternalValue), 
        udiCount:= _Size+1
    );
    CharString:= TempVar;
    

    JSONVAR_16.CharString.Set:

    _ValueType:= JSONVALUETYPE_16.json_wstring;
    _Size:= INT_TO_UINT(WLEN(CharString));
    
    //clear memory
    SysMemSet(
        pDest:= ADR(_InternalValue), 
        udiValue:= 0, 
        udiCount:= SIZEOF(_InternalValue)
    );
    
    //put data into internal value
    SysMemCpy(
        pDest:= ADR(_InternalValue), 
        pSrc:= ADR(CharString), 
        udiCount:= _Size+1
    );
    
     

    Last edit: damian177 2022-01-28
    • tvm - 2022-01-28

      Size should be doubled
      I think like this:

      _Size:= INT_TO_UINT(WLEN(CharString)) * 2;
      
       
  • damian177 - 2022-01-28

    Effect is like in attachement. Last special characters in "description" variable are bad decode ...

     

    Last edit: damian177 2022-01-28
  • tvm - 2022-01-28

    OK, what about leaving Size the way it was, and changing:

    //put data into internal value
    SysMemCpy(
        pDest:= ADR(_InternalValue), 
        pSrc:= ADR(CharString), 
        udiCount:= (_Size+1) * 2
    );
    

    I can't remember how Size is used elsewhere - it's probably something to do with whether it includes the trailing null character or not.

     
  • damian177 - 2022-01-28
     udiCount:= (_Size+1) * 2
    

    this line does not affect the result at all

     

    Last edit: damian177 2022-01-28
  • tvm - 2022-01-28

    Did you change it in CharString.Get as well?

     
  • damian177 - 2022-01-28

    sorry my fault, now it works.
    but still cuts characters. For example I put WSTRING:
    ΕΕšΔ˜Γ³Ε›uΔ…QΓ³Ε›ΕšΕ„Ε„NΕƒ

    but after Parase in JSON variable is :
    "ΕΕšΔ˜Γ³Ε›uΔ…QΓ³["

    what can still limit the length of the variable?

     

    Last edit: damian177 2022-01-28
  • tvm - 2022-01-28

    I think it must be something in STRING_TO_JSONVALUE function.
    If JSONVAR.HMIVarAsString is correct, that means the value is correct when JSONVAR.AsString is set. Because all it's doing there is copying the value, since it's already a string. I was using that for a certain HMI that couldn't accept properties, only variables.
    So, you might want to set a breakpoint in JSONVAR.AsString.Set, and then step through STRING_TO_JSONVAR to see where the limiting is happening.
    It might be useful to take out the {attribute 'hide'} from the InternalValue variable, so you can look at the actual bytes stored internally

     
  • damian177 - 2022-01-28

    Both HMIVarAsString and AsString have truncated characters.

     
  • tvm - 2022-01-28

    In that case, the value is set in JSON_TO_STRUCT, line 110

    JSONVars^[jsonvarindex].AsString:= NameValue[obj_level].Value;  
    

    if you set a breakpoint here, you should be able to step through each name:value pair, and see what's contained in the NameValue array at the one that's not working.

    The NameValue array is populated in the .Value method, which we've already modified, line 65-70.

            //copy buffer to temporary value as a string
            SysMemCpy(
                pDest:= ADR(NameValue[obj_level].Value), 
                pSrc:= ADR(JSONString^[value_start]), 
                udiCount:= MIN(((value_stop+1) - value_start) * 2, GPL_JSON.MAX_VALUE_SIZE-1)
            );
    

    Maybe something wrong with the size math?

     
  • damian177 - 2022-01-28

    It works!
    I forgot set my global variable GPL_JSON_16 in edited above line. Now it works.

     
  • tvm - 2022-01-28

    Excellent, glad to hear it. If you're interested in contributing to the library you can contact me directly, my email address is scattered all over the documentation. Some time in the future when I have some time I'd like to figure out how to allow for both STRING and WSTRING types in the same library. Not sure how I'm going to do that yet without creating two entirely separate systems.

     
<< < 1 2 (Page 2 of 2)

Log in to post a comment.