Updated Home (markdown)

FalcoGoodbody
2020-09-21 10:53:44 +02:00
parent 16cea5881c
commit db9bb6484e

114
Home.md

@@ -395,4 +395,118 @@ 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();
```