Array to String

mmpl
2024-07-18
2024-07-24
  • mmpl - 2024-07-18

    I would like to create one large JSON string from an array of objects, where each object contains a value. My current approach involves using a for loop and string concatenation, but with 20,000 elements in the array, this method takes almost 7 seconds and negatively affects the PLC scan time.

    Is there a more efficient way to accomplish this?

     
  • drml - 2024-07-19

    If a size is not specified, CODESYS allocates 80 characters by default for a string. After 7 seconds, does your Json_string contain all of your 20000 key/values? Or is the result truncated to 80 characters?

     
  • peterned - 2024-07-20

    max. string length in Codesys is 255 chars. It's not possible to cram 20,000 things in a string.
    If you need to send the data to another device, send the whole data[] array (as byte array, without doing anything to it) and extract the information on the other end. As each struct member has a fixed size (81 bytes, as per you declaration), this will be easy.

    To save some resources, consider declaring string lengths - e.g. if the max possible length for key is 10 chars, declare it
    key: STRING(10); and it will occupy 11 bytes instead of 81

     
  • TimvH

    TimvH - 2024-07-22

    The new (UTF8) string library is faster and can handle large strings.
    https://content.helpme-codesys.com/en/CODESYS%20String%20Libraries/_strlib_start_page.html

    PS, in CODESYS, the maximum string length is not 255 characters. The String functions of the standard library are limited to strings of 255 characters. But you can use other (memory) functions to handle strings larger than 255 characters.

     
  • mmpl - 2024-07-23

    Hi Thank you for you reply. Yes I can concatenat longer string. I am using string_Util_Intern libraray for concat. The issue is abot plc performance. It takes 7 second. I am looking to improve time.

     
  • TimvH

    TimvH - 2024-07-24

    The string_Util_Intern is not the library I referred to.
    With a CONCAT function (also with the string util library) a search is done for the NULL character.
    If you concatenate 20000 characters to one string, then the longer the string, the longer it takes to find this NULL character.

     

Log in to post a comment.