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.
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?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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?
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?
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
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.
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.
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.