Skip to content
FalcoGoodbody edited this page Sep 9, 2020 · 14 revisions

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.ConvertToInt());
  • Read bool from byte
byte myByte = 5; // 0000 0101
myByte.SelectBit(0) // true
myByte.SelectBit(1) // false
Clone this wiki locally