mirror of
https://github.com/S7NetPlus/s7netplus.git
synced 2025-12-17 17:06:50 +08:00
Updated Home (markdown)
103
Home.md
103
Home.md
@@ -289,3 +289,106 @@ then add the code to read or write the complete struct
|
|||||||
TestClass myTestClass = new TestClass();
|
TestClass myTestClass = new TestClass();
|
||||||
plc.ReadClass(myTestClass, 1, 0);
|
plc.ReadClass(myTestClass, 1, 0);
|
||||||
```
|
```
|
||||||
|
# Read multiple variables
|
||||||
|
|
||||||
|
This method reads multiple variables in a single request. The variables can be locatet in the same or in different data blocks.
|
||||||
|
|
||||||
|
```csharp
|
||||||
|
public void Plc.ReadMultibleVars(List<DataItem>dataItems);
|
||||||
|
```
|
||||||
|
* **List**: you have to specifiy a list which contains all DataItems you want to read
|
||||||
|
|
||||||
|
|
||||||
|
Example: this method reads several variables from a data block
|
||||||
|
|
||||||
|
```csharp
|
||||||
|
// define the data items first
|
||||||
|
private static DataItem varBit = new DataItem()
|
||||||
|
{
|
||||||
|
DataType = DataType.DataBlock,
|
||||||
|
VarType = VarType.Bit,
|
||||||
|
DB = 83,
|
||||||
|
BitAdr = 0,
|
||||||
|
Count = 1,
|
||||||
|
StartByteAdr = 0,
|
||||||
|
Value = new object()
|
||||||
|
};
|
||||||
|
|
||||||
|
private static DataItem varByteArray = new DataItem()
|
||||||
|
{
|
||||||
|
DataType = DataType.DataBlock,
|
||||||
|
VarType = VarType.Byte,
|
||||||
|
DB = 83,
|
||||||
|
BitAdr = 0,
|
||||||
|
Count = 100,
|
||||||
|
StartByteAdr = 0,
|
||||||
|
Value = new object()
|
||||||
|
};
|
||||||
|
|
||||||
|
private static DataItem varInt = new DataItem()
|
||||||
|
{
|
||||||
|
DataType = DataType.DataBlock,
|
||||||
|
VarType = VarType.Int,
|
||||||
|
DB = 83,
|
||||||
|
BitAdr = 0,
|
||||||
|
Count = 1,
|
||||||
|
StartByteAdr = 102,
|
||||||
|
Value = new object()
|
||||||
|
};
|
||||||
|
|
||||||
|
private static DataItem varReal = new DataItem()
|
||||||
|
{
|
||||||
|
DataType = DataType.DataBlock,
|
||||||
|
VarType = VarType.Real,
|
||||||
|
DB = 83,
|
||||||
|
BitAdr = 0,
|
||||||
|
Count = 1,
|
||||||
|
StartByteAdr = 112,
|
||||||
|
Value = new object()
|
||||||
|
};
|
||||||
|
|
||||||
|
private static DataItem varString = new DataItem()
|
||||||
|
{
|
||||||
|
DataType = DataType.DataBlock,
|
||||||
|
VarType = VarType.StringEx,
|
||||||
|
DB = 83,
|
||||||
|
BitAdr = 0,
|
||||||
|
Count = 20, // max lengt of string
|
||||||
|
StartByteAdr = 116,
|
||||||
|
Value = new object()
|
||||||
|
};
|
||||||
|
|
||||||
|
private static DataItem varDateTime = new DataItem()
|
||||||
|
{
|
||||||
|
DataType = DataType.DataBlock,
|
||||||
|
VarType = VarType.DateTime,
|
||||||
|
DB = 83,
|
||||||
|
BitAdr = 0,
|
||||||
|
Count = 1,
|
||||||
|
StartByteAdr = 138,
|
||||||
|
Value = new object()
|
||||||
|
};
|
||||||
|
|
||||||
|
// define a list where the DataItems will be stored in
|
||||||
|
private static List<DataItem> dataItemsRead = new List<DataItem>();
|
||||||
|
|
||||||
|
// add the DataItems to the list
|
||||||
|
dataItemsRead.Add(varBit);
|
||||||
|
dataItemsRead.Add(varByteArray);
|
||||||
|
dataItemsRead.Add(varInt);
|
||||||
|
dataItemsRead.Add(varReal);
|
||||||
|
dataItemsRead.Add(varString);
|
||||||
|
dataItemsRead.Add(varDateTime);
|
||||||
|
|
||||||
|
// open the connection to the plc
|
||||||
|
myPLC.Open();
|
||||||
|
|
||||||
|
// read the list of variables
|
||||||
|
myPLC.ReadMultipleVars(dataItemsRead);
|
||||||
|
|
||||||
|
// close the connection
|
||||||
|
myPLC.Close();
|
||||||
|
|
||||||
|
// access the values of the list
|
||||||
|
Console.WriteLine("Int:" + dataItemsRead[2].Value);
|
||||||
|
```
|
||||||
Reference in New Issue
Block a user