bArray : ARRAY(0..100) of BYTE; ( look at 100 bytes so I can see bytes before and after my string )
startByte: POINTER TO ARRAY(0..100) OF BYTE;
startByte := ADR(myVar) - 10; ( look 10 bytes before the string )
bArray := startByte^; ( dereference the pointer to look at the byte array starting 10 bytes before the string )
bArray --------
bArray[0]=00
bArray[1]=00
bArray[2]=00
bArray[3]=00
bArray[4]=00
bArray[5]=00
bArray[6]=00
bArray[7]=00
bArray[8]=00
bArray[9]=00
bArray[10]=00
bArray[11]=66 ( f )
bArray[12]=6F ( o )
bArray[13]=6F ( o)
bArray[14]=62 ( b)
bArray[15]=61 ( a )
bArray[16]=72 ( r)
bArray[17]=00
bArray[18]=00
```
I can setup my own structure for storing strings where I keep track of the length of the string, but would prefer not to if it is not necessary.
Can anyone help me understand how strings are stored and if it is possible to read with Modbus only the actual values stored?
Regards
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Yes it's true that strings are stored with an extra byte e.g. 30 characters = 31 bytes. However the extra byte does not contain a string length. The extra byte is at the end of the string and is a "null terminator" i.e it will contain the value 00 to signify the end of the string. 00 is a value that cannot be interpreted as a character in a string and so can be safely used as a terminator. The terminator is placed immediately after the last character in the string.
So even though myVar is an 80 character string (81 bytes) in your example byte_array[17] contains the null character.
You can use this knowledge to find the end of the string.
I hope that helps.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Thank you for your reply. That is very helpful information. I cannot believe that I did not notice the null there before. Greatly simplifies things for me.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hello, I could use help with how strings are stored in Codesys or in a Wago 750-841 controller, and with reading them using Modbus.
If I define a STRING variable without length, I know it defaults to 80 characters (bytes).
When I read 80 bytes with Modbus I get something to the effect ```
'foobar      ?18$ot               ';
length := SIZEOF(myVar);Â ( gives 81 !!)
bArray : ARRAY(0..100) of BYTE; ( look at 100 bytes so I can see bytes before and after my string )
startByte: POINTER TO ARRAY(0..100) OF BYTE;
startByte := ADR(myVar) - 10; ( look 10 bytes before the string )
bArray := startByte^; ( dereference the pointer to look at the byte array starting 10 bytes before the string )
bArray --------
bArray[0]=00
bArray[1]=00
bArray[2]=00
bArray[3]=00
bArray[4]=00
bArray[5]=00
bArray[6]=00
bArray[7]=00
bArray[8]=00
bArray[9]=00
bArray[10]=00
bArray[11]=66 ( f )
bArray[12]=6F ( o )
bArray[13]=6F ( o)
bArray[14]=62 ( b)
bArray[15]=61 ( a )
bArray[16]=72 ( r)
bArray[17]=00
bArray[18]=00
```
I can setup my own structure for storing strings where I keep track of the length of the string, but would prefer not to if it is not necessary.
Can anyone help me understand how strings are stored and if it is possible to read with Modbus only the actual values stored?
Regards
Hi,
Yes it's true that strings are stored with an extra byte e.g. 30 characters = 31 bytes. However the extra byte does not contain a string length. The extra byte is at the end of the string and is a "null terminator" i.e it will contain the value 00 to signify the end of the string. 00 is a value that cannot be interpreted as a character in a string and so can be safely used as a terminator. The terminator is placed immediately after the last character in the string.
So even though myVar is an 80 character string (81 bytes) in your example byte_array[17] contains the null character.
You can use this knowledge to find the end of the string.
I hope that helps.
@aellis_scatts
Thank you for your reply. That is very helpful information. I cannot believe that I did not notice the null there before. Greatly simplifies things for me.
so if you want to send a fixed length you will have to change the rest with spaces or anything you like.