I am trying to read a real value from a Siemens PLC (S7-1200) and display it in Windows Form. I am using the S7.NET library to communicate with the PLC and a TIA Portal V15 to program/Monitor the PLC.
I am able to read the particular data block (DB3.DBD0) in the Windows Form from the PLC, it does return a value, but the value is displayed in some other format. For example, if I modify the value in DB3.DBD0 to "2.22", it gives me "1.074665+09". I would like it to return the same value as in the TIA Portal which is "2.22".
Below is the code I am using to convert the values.
object real0 = Convert.ToSingle(plc.Read("DB3.DBD0"));
label43.Text = real0.ToString();
If my question is not clear, please let me know, I can try to explain in more detail.
Thank you in advance!
The PLC data type appears to be a REAL (Floating-point number) 'IEEE Floating-point number' as per http://www.plcdev.com/step_7_elementary_data_types
These numbers are represented as fractions, whereby 2.2 cannot be accurately defined. Great detailed explanation: Why are floating point numbers inaccurate?
Good simple explanation: https://floating-point-gui.de/basic
Try converting to decimal data types, then rounding to remove trailing 0's