Created Value conversion (markdown)

Michele Cattafesta
2017-08-31 21:32:33 +01:00
parent 2fca5ab26c
commit 8bac21c43b

60
Value-conversion.md Normal file

@@ -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
```