From db9bb6484eccfbb23e44ce2fc24f9f647b82a323 Mon Sep 17 00:00:00 2001 From: FalcoGoodbody Date: Mon, 21 Sep 2020 10:53:44 +0200 Subject: [PATCH] Updated Home (markdown) --- Home.md | 114 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) diff --git a/Home.md b/Home.md index 4b0a4e0..4bd58f1 100644 --- a/Home.md +++ b/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 dataItemsWrite = new List(); + +// 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(); ``` \ No newline at end of file