From 8bac21c43b5c6ae9dbe78a439e6983387ece2565 Mon Sep 17 00:00:00 2001 From: Michele Cattafesta Date: Thu, 31 Aug 2017 21:32:33 +0100 Subject: [PATCH] Created Value conversion (markdown) --- Value-conversion.md | 60 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 Value-conversion.md diff --git a/Value-conversion.md b/Value-conversion.md new file mode 100644 index 0000000..7996019 --- /dev/null +++ b/Value-conversion.md @@ -0,0 +1,60 @@ +## Value conversion between C# and S7 plc + +* Read S7 Word: +``` +ushort result = (ushort)plc.Read("DB1.DBW0"); +``` + +* Write S7 Word: +``` +ushort val = 40000; +plc.Write("DB1.DBW0", val); +``` + +* Read S7 Int / Dec, you need to use the method ConvertToShort(): +``` +short result = ((ushort)plc.Read("DB1.DBW0")).ConvertToShort(); +``` +* Write S7 Int / Dec, you need to use the method ConvertToUshort(): +``` +short value = -100; +plc.Write("DB1.DBW0", value.ConvertToUshort()); +``` + +* Read S7 DWord: +``` +uint result = (uint)plc.Read("DB1.DBD40"); +``` +* Write S7 DWord: +``` +uint val = 1000; +plc.Write("DB1.DBD40", val); +``` + +* Read S7 Dint, you need to use ConvertToInt(): +``` +int result2 = ((uint)plc.Read("DB1.DBD60")).ConvertToInt(); +``` + +* Write S7 Dint: +``` +int value = -60000; +plc.Write("DB1.DBD60", value); +``` +* Read S7 Real, you need to use ConvertToDouble(): +``` +double result = ((uint)plc.Read("DB1.DBD40")).ConvertToDouble(); +``` + +* Write S7 Real, you need to use ConvertToInt(): +``` +double val = 35.687; +plc.Write("DB1.DBD40", val.ConvertToUInt()); +``` + +* Read bool from byte +``` +byte myByte = 5; // 0000 0101 +myByte.SelectBit(0) // true +myByte.SelectBit(1) // false +``` \ No newline at end of file