mirror of
https://github.com/S7NetPlus/s7netplus.git
synced 2025-12-17 17:06:50 +08:00
Created Value conversion (markdown)
60
Value-conversion.md
Normal file
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
|
||||
```
|
||||
Reference in New Issue
Block a user