Search code examples
c#siemens

Reading string from Siemens PLC using s7.net pulls new data along with old leftover


i am using s7.net library to read string value from S7 1200 plc. it is reading data but incorrectly. For example if data is "My Name", it read it well When it is changed to "ABC", it read it as "ABCName". below is the code i am using

var ss1 = plc.Read(DataType.DataBlock, 30, 0, S7.Net.VarType.String, 20);
txt_Textbox.Text =  ss1.ToString();

Somehow writing new value not clearing all old values. How can i do this using s7.net


Solution

  • The thing is that on the PLC system, the length of the string is always reserved in memory, so you just need to explicitly read the first 2 bytes in the DB containing your string as an unsigned 16-bit integer. It stores the current actual length of your string. It is also written here but for a different system.