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

STRING size and reading STRING with Modbus

scottyhos
2014-11-12
2014-11-15
  • scottyhos - 2014-11-12

    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).

    myVar  AT %MW100 : STRING := 'foobar';
    

    When I read 80 bytes with Modbus I get something to the effect ```

    'foobar           ?18$ot                              ';

    I assume that the target is storing somewhere how many bytes are actually being used by the string. 
    If I get a reference to the size of my variable, it seems to confirm this.
    

    length := SIZEOF(myVar);  ( gives 81 !!)

    If I look at the byte array that backs the string, I do not see anywhere the length being stored
    

    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

     
  • aellis - 2014-11-12

    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.

     
  • scottyhos - 2014-11-13

    @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.

     
  • shooter - 2014-11-15

    so if you want to send a fixed length you will have to change the rest with spaces or anything you like.

     

Log in to post a comment.