<?xml version="1.0" encoding="utf-8"?>
<feed xml:lang="en" xmlns="http://www.w3.org/2005/Atom"><title>Recent posts to STRING size and reading STRING with Modbus</title><link href="https://forge.codesys.com/forge/talk/CODESYS-V2/thread/3d792b587f/" rel="alternate"/><link href="https://forge.codesys.com/forge/talk/CODESYS-V2/thread/3d792b587f/feed.atom" rel="self"/><id>https://forge.codesys.com/forge/talk/CODESYS-V2/thread/3d792b587f/</id><updated>2025-01-31T12:26:21.155000Z</updated><subtitle>Recent posts to STRING size and reading STRING with Modbus</subtitle><entry><title>STRING size and reading STRING with Modbus</title><link href="https://forge.codesys.com/forge/talk/CODESYS-V2/thread/3d792b587f/?limit=250#9f9a" rel="alternate"/><published>2025-01-31T12:26:21.155000Z</published><updated>2025-01-31T12:26:21.155000Z</updated><author><name>dkugler</name><uri>https://forge.codesys.com/u/dkugler/</uri></author><id>https://forge.codesys.come5df6f55181b7adb59460ff9835e72b07f9eefd1</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;here a helpful explanation of the additional byte of every codesys string: &lt;a href="https://forge.codesys.com/forge/talk/CODESYS-V2/thread/3d792b587f/#1046"&gt;https://forge.codesys.com/forge/talk/CODESYS-V2/thread/3d792b587f/#1046&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;good luck and happy coding!&lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>STRING size and reading STRING with Modbus</title><link href="https://forge.codesys.com/forge/talk/Codesys%2520V2.3/thread/3d792b587f/?limit=25#38cd" rel="alternate"/><published>2020-02-27T19:49:58.543000Z</published><updated>2020-02-27T19:49:58.543000Z</updated><author><name>shooter</name><uri>https://forge.codesys.com/u/shooter/</uri></author><id>https://forge.codesys.com5dd76d36e400e3fd4309142eb89a234c6583addd</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;so if you want to send a fixed length you will have to change the rest with spaces or anything you like.&lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>STRING size and reading STRING with Modbus</title><link href="https://forge.codesys.com/forge/talk/Codesys%2520V2.3/thread/3d792b587f/?limit=25#6b06" rel="alternate"/><published>2020-02-27T19:49:57.264000Z</published><updated>2020-02-27T19:49:57.264000Z</updated><author><name>scottyhos</name><uri>https://forge.codesys.com/u/scottyhos/</uri></author><id>https://forge.codesys.comc8c236a005c0389a0a1435bcecdb252c9cb29118</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;&lt;a class="user-mention" href="/u/aellis/"&gt;@aellis&lt;/a&gt;_scatts&lt;/p&gt;
&lt;p&gt;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.&lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>STRING size and reading STRING with Modbus</title><link href="https://forge.codesys.com/forge/talk/Codesys%2520V2.3/thread/3d792b587f/?limit=25#1046" rel="alternate"/><published>2020-02-27T19:49:53.600000Z</published><updated>2020-02-27T19:49:53.600000Z</updated><author><name>aellis</name><uri>https://forge.codesys.com/u/aellis/</uri></author><id>https://forge.codesys.comd1f60f1311dd002f4e1579588bbbefd819ea1169</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;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.&lt;/p&gt;
&lt;p&gt;So even though myVar is an 80 character string (81 bytes) in your example byte_array&lt;span&gt;[17]&lt;/span&gt; contains the null character.&lt;/p&gt;
&lt;p&gt;You can use this knowledge to find the end of the string.&lt;/p&gt;
&lt;p&gt;I hope that helps.&lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>STRING size and reading STRING with Modbus</title><link href="https://forge.codesys.com/forge/talk/Codesys%2520V2.3/thread/3d792b587f/?limit=25#85f4" rel="alternate"/><published>2020-02-27T19:49:52.250000Z</published><updated>2020-02-27T19:49:52.250000Z</updated><author><name>scottyhos</name><uri>https://forge.codesys.com/u/scottyhos/</uri></author><id>https://forge.codesys.combe07d2be39e70fac97a5a639df21139716901efb</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;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. &lt;/p&gt;
&lt;p&gt;If I define a STRING variable without length, I know it defaults to 80 characters (bytes).&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="n"&gt;myVar&lt;/span&gt;&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="k"&gt;AT&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="n"&gt;MW100&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;STRING&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'foobar'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;When I read 80 bytes with Modbus I get something to the effect ```&lt;/p&gt;
&lt;p&gt;'foobar           ?18$ot                              ';&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="nv"&gt;I&lt;/span&gt; &lt;span class="nv"&gt;assume&lt;/span&gt; &lt;span class="nv"&gt;that&lt;/span&gt; &lt;span class="nv"&gt;the&lt;/span&gt; &lt;span class="nv"&gt;target&lt;/span&gt; &lt;span class="nv"&gt;is&lt;/span&gt; &lt;span class="nv"&gt;storing&lt;/span&gt; &lt;span class="nv"&gt;somewhere&lt;/span&gt; &lt;span class="nv"&gt;how&lt;/span&gt; &lt;span class="nv"&gt;many&lt;/span&gt; &lt;span class="nv"&gt;bytes&lt;/span&gt; &lt;span class="nv"&gt;are&lt;/span&gt; &lt;span class="nv"&gt;actually&lt;/span&gt; &lt;span class="nv"&gt;being&lt;/span&gt; &lt;span class="nv"&gt;used&lt;/span&gt; &lt;span class="nv"&gt;by&lt;/span&gt; &lt;span class="nv"&gt;the&lt;/span&gt; &lt;span class="nv"&gt;string&lt;/span&gt;. 
&lt;span class="k"&gt;If&lt;/span&gt; &lt;span class="nv"&gt;I&lt;/span&gt; &lt;span class="nv"&gt;get&lt;/span&gt; &lt;span class="nv"&gt;a&lt;/span&gt; &lt;span class="nv"&gt;reference&lt;/span&gt; &lt;span class="nv"&gt;to&lt;/span&gt; &lt;span class="nv"&gt;the&lt;/span&gt; &lt;span class="nv"&gt;size&lt;/span&gt; &lt;span class="nv"&gt;of&lt;/span&gt; &lt;span class="nv"&gt;my&lt;/span&gt; &lt;span class="nv"&gt;variable&lt;/span&gt;, &lt;span class="nv"&gt;it&lt;/span&gt; &lt;span class="nv"&gt;seems&lt;/span&gt; &lt;span class="nv"&gt;to&lt;/span&gt; &lt;span class="nv"&gt;confirm&lt;/span&gt; &lt;span class="nv"&gt;this&lt;/span&gt;.
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;length := SIZEOF(myVar);  (&lt;em&gt; gives 81 !!&lt;/em&gt;)&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="k"&gt;If&lt;/span&gt; &lt;span class="nv"&gt;I&lt;/span&gt; &lt;span class="nv"&gt;look&lt;/span&gt; &lt;span class="nv"&gt;at&lt;/span&gt; &lt;span class="nv"&gt;the&lt;/span&gt; &lt;span class="nv"&gt;byte&lt;/span&gt; &lt;span class="nv"&gt;array&lt;/span&gt; &lt;span class="nv"&gt;that&lt;/span&gt; &lt;span class="nv"&gt;backs&lt;/span&gt; &lt;span class="nv"&gt;the&lt;/span&gt; &lt;span class="nv"&gt;string&lt;/span&gt;, &lt;span class="nv"&gt;I&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="nv"&gt;not&lt;/span&gt; &lt;span class="nv"&gt;see&lt;/span&gt; &lt;span class="nv"&gt;anywhere&lt;/span&gt; &lt;span class="nv"&gt;the&lt;/span&gt; &lt;span class="nv"&gt;length&lt;/span&gt; &lt;span class="nv"&gt;being&lt;/span&gt; &lt;span class="nv"&gt;stored&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;bArray : ARRAY(0..100) of BYTE; (&lt;em&gt; look at 100 bytes so I can see bytes before and after my string &lt;/em&gt;)&lt;br/&gt;
startByte: POINTER TO ARRAY(0..100) OF BYTE;&lt;br/&gt;
startByte := ADR(myVar) - 10; (&lt;em&gt; look 10 bytes before the string &lt;/em&gt;)&lt;br/&gt;
bArray := startByte^; (&lt;em&gt; dereference the pointer to look at the byte array starting 10 bytes before the string &lt;/em&gt;)&lt;br/&gt;
bArray --------&lt;br/&gt;
bArray&lt;span&gt;[0]&lt;/span&gt;=00&lt;br/&gt;
bArray&lt;span&gt;[1]&lt;/span&gt;=00&lt;br/&gt;
bArray&lt;span&gt;[2]&lt;/span&gt;=00&lt;br/&gt;
bArray&lt;span&gt;[3]&lt;/span&gt;=00&lt;br/&gt;
bArray&lt;span&gt;[4]&lt;/span&gt;=00&lt;br/&gt;
bArray&lt;span&gt;[5]&lt;/span&gt;=00&lt;br/&gt;
bArray&lt;span&gt;[6]&lt;/span&gt;=00&lt;br/&gt;
bArray&lt;span&gt;[7]&lt;/span&gt;=00&lt;br/&gt;
bArray&lt;span&gt;[8]&lt;/span&gt;=00&lt;br/&gt;
bArray&lt;span&gt;[9]&lt;/span&gt;=00&lt;br/&gt;
bArray&lt;span&gt;[10]&lt;/span&gt;=00&lt;br/&gt;
bArray&lt;span&gt;[11]&lt;/span&gt;=66 (&lt;em&gt; f &lt;/em&gt;)&lt;br/&gt;
bArray&lt;span&gt;[12]&lt;/span&gt;=6F (&lt;em&gt; o &lt;/em&gt;)&lt;br/&gt;
bArray&lt;span&gt;[13]&lt;/span&gt;=6F (&lt;em&gt; o&lt;/em&gt;)&lt;br/&gt;
bArray&lt;span&gt;[14]&lt;/span&gt;=62 (&lt;em&gt; b&lt;/em&gt;)&lt;br/&gt;
bArray&lt;span&gt;[15]&lt;/span&gt;=61 (&lt;em&gt; a &lt;/em&gt;)&lt;br/&gt;
bArray&lt;span&gt;[16]&lt;/span&gt;=72 (&lt;em&gt; r&lt;/em&gt;)&lt;br/&gt;
bArray&lt;span&gt;[17]&lt;/span&gt;=00&lt;br/&gt;
bArray&lt;span&gt;[18]&lt;/span&gt;=00&lt;/p&gt;
&lt;p&gt;```&lt;br/&gt;
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.&lt;/p&gt;
&lt;p&gt;Can anyone help me understand how strings are stored and if it is possible to read with Modbus only the actual values stored?&lt;/p&gt;
&lt;p&gt;Regards&lt;/p&gt;&lt;/div&gt;</summary></entry></feed>