I am trying to read hex values from a Siemens PLC S7-1500 series. I use the S7NetPlus library. I read the database values as bytes and try to print them to the console to check if the data I receive is the same as the PLC has. Since PLCs uses some different datatypes than C# uses there is a little puzzle involved. The following snippet is my attempt to read the PLC data:
var wordS = sp.ReadBytes(DataType.DataBlock, 2, 2, 2);
string test1 = Encoding.BigEndianUnicode.GetString(wordS);
Console.WriteLine($"Word: {test1}");
var dWordS = sp.ReadBytes(DataType.DataBlock, 2, 4, 4);
string test2 = Encoding.UTF8.GetString(dWordS);
Console.WriteLine($"DWord: {test2}");
I read the following result in the console when I run the program: Console write result
The hex values are converted to ASCII characters if I am correct.
My question is:
- How can I read the word and Dword datatype and print them as a string with the hex value intact.