mirror of
https://github.com/S7NetPlus/s7netplus.git
synced 2025-12-17 17:06:50 +08:00
Updated Home (markdown)
114
Home.md
114
Home.md
@@ -396,3 +396,117 @@ myPLC.Close();
|
||||
// access the values of the list
|
||||
Console.WriteLine("Int:" + dataItemsRead[2].Value);
|
||||
```
|
||||
|
||||
## Write multiple variables
|
||||
|
||||
This method writes multiple variables in a single request.
|
||||
```csharp
|
||||
public void Plc.Write(Array[DataItem] dataItems);
|
||||
```
|
||||
* **Array[]** you have to specifiy an array of DataItem which contains all the items you want to write
|
||||
|
||||
Example: this method writes multiple variables into one data block
|
||||
|
||||
define the data items
|
||||
```csharp
|
||||
private static DataItem varWordWrite = new DataItem()
|
||||
{
|
||||
DataType = DataType.DataBlock,
|
||||
VarType = VarType.Word,
|
||||
DB = 83,
|
||||
BitAdr = 0,
|
||||
Count = 1,
|
||||
StartByteAdr = 146,
|
||||
Value = new object()
|
||||
};
|
||||
|
||||
private static DataItem varIntWrite = new DataItem()
|
||||
{
|
||||
DataType = DataType.DataBlock,
|
||||
VarType = VarType.Int,
|
||||
DB = 83,
|
||||
BitAdr = 0,
|
||||
Count = 1,
|
||||
StartByteAdr = 148,
|
||||
Value = new object()
|
||||
};
|
||||
|
||||
private static DataItem varDWordWrite = new DataItem()
|
||||
{
|
||||
DataType = DataType.DataBlock,
|
||||
VarType = VarType.DWord,
|
||||
DB = 83,
|
||||
BitAdr = 0,
|
||||
Count = 1,
|
||||
StartByteAdr = 150,
|
||||
Value = new object()
|
||||
};
|
||||
|
||||
private static DataItem varDIntWrite = new DataItem()
|
||||
{
|
||||
DataType = DataType.DataBlock,
|
||||
VarType = VarType.DInt,
|
||||
DB = 83,
|
||||
BitAdr = 0,
|
||||
Count = 1,
|
||||
StartByteAdr = 154,
|
||||
Value = new object()
|
||||
};
|
||||
|
||||
private static DataItem varRealWrite = new DataItem()
|
||||
{
|
||||
DataType = DataType.DataBlock,
|
||||
VarType = VarType.Real,
|
||||
DB = 83,
|
||||
BitAdr = 0,
|
||||
Count = 1,
|
||||
StartByteAdr = 158,
|
||||
Value = new object()
|
||||
};
|
||||
|
||||
private static DataItem varStringWrite = new DataItem()
|
||||
{
|
||||
DataType = DataType.DataBlock,
|
||||
VarType = VarType.StringEx,
|
||||
DB = 83,
|
||||
BitAdr = 0,
|
||||
Count = 20,
|
||||
StartByteAdr = 162,
|
||||
Value = new object()
|
||||
};
|
||||
```
|
||||
Asign the values to the data items. Be aware to use the correct data conversion for the variables to fit into the S7-data types
|
||||
```csharp
|
||||
// asign values to the variable to be written
|
||||
varWordWrite.Value = (ushort)67;
|
||||
varIntWrite.Value = (ushort)33;
|
||||
varDWordWrite.Value = (uint)444;
|
||||
varDIntWrite.Value = 6666;
|
||||
varRealWrite.Value = 77.89;
|
||||
varStringWrite.Value = "Writting";
|
||||
```
|
||||
Then define a list to store the data items and add the created items to the list
|
||||
```csharp
|
||||
private static List<DataItem> dataItemsWrite = new List<DataItem>();
|
||||
|
||||
// add data items to list of data items to write
|
||||
dataItemsWrite.Add(varWordWrite);
|
||||
dataItemsWrite.Add(varIntWrite);
|
||||
dataItemsWrite.Add(varDWordWrite);
|
||||
dataItemsWrite.Add(varDIntWrite);
|
||||
dataItemsWrite.Add(varRealWrite);
|
||||
dataItemsWrite.Add(varStringWrite);
|
||||
```
|
||||
|
||||
Finally open a connection to the plc and write the items at once.
|
||||
Use `.ToArray()` to convert the list into an array.
|
||||
```csharp
|
||||
// open the connection
|
||||
myPLC.Open();
|
||||
|
||||
// write the items
|
||||
myPLC.Write(dataItemsWrite.ToArray());
|
||||
|
||||
// close the connection
|
||||
myPLC.Close();
|
||||
```
|
||||
Reference in New Issue
Block a user