public void ReadSynchronously(Dictionary <string, string> TAGVALUE) { if (DAServer.ServerState == ServerState.CONNECTED) { DateTime itemTimeStamp = DateTime.Now; ItemIdentifier[] OPCItems = new ItemIdentifier[IDTAG.Count]; for (int i = 0; i < IDTAG.Count; i++) { OPCItems[i] = new ItemIdentifier(); OPCItems[i].ItemName = IDTAG[i]; OPCItems[i].ClientHandle = IDTAG.ElementAt(i).Key; } int maxAge = 0; ItemValue[] OPCItemValues = null; DAServer.Read(maxAge, ref OPCItems, out OPCItemValues); if (OPCItemValues == null) { return; } String itemName = String.Empty; for (int i = 0; i < OPCItems.Length; i++) { if ((OPCItems[i] == null) || (OPCItemValues[i] == null)) { continue; } if (OPCItems[i].ResultID.Succeeded & OPCItemValues[i].Quality.IsGood) { itemName = OPCItems[i].ItemName; /////////////////////////////////////////////////////// // TODO ALL TAGS SHOULD BE TRANSFORMED TO UPPER if (itemName.ToUpper() == "PlantDO.PLCB.Common.IsDOMasterInOperation".ToUpper()) { itemName = "PlantDO.PLCB.Common.IsDOMasterInOperation".ToUpper(); } /////////////////////////////////////////////////////// if (TAGVALUE.ContainsKey(itemName)) { TAGVALUE[itemName] = OPCItemValues[i].Value.ToString(); } } } } }
private void AggiornaDati() { // Aggiorno a mano i valori di due tag int maxAge = 0; Kepware.ClientAce.OpcDaClient.ItemIdentifier[] OPCItems = new Kepware.ClientAce.OpcDaClient.ItemIdentifier[2]; Kepware.ClientAce.OpcDaClient.ItemValue[] OPCItemValues = null; OPCItems[0] = new Kepware.ClientAce.OpcDaClient.ItemIdentifier(); OPCItems[0].ItemName = "its-iot-device.Device1.PlantStatus"; OPCItems[0].ClientHandle = 1; OPCItems[1] = new Kepware.ClientAce.OpcDaClient.ItemIdentifier(); OPCItems[1].ItemName = "Simulation Examples.Functions.Ramp1"; OPCItems[1].ClientHandle = 2; label3.Text = OPCItems[0].ItemName; label6.Text = OPCItems[1].ItemName; try { daServerMgt.Read(maxAge, ref OPCItems, out OPCItemValues); if (OPCItems[0].ResultID.Succeeded & OPCItemValues[0].Quality.IsGood) { label4.Text = OPCItemValues[0].Value.ToString(); } else { debugBox.Text += OPCItems[0].ResultID.Description; } if (OPCItems[1].ResultID.Succeeded & OPCItemValues[1].Quality.IsGood) { label5.Text = OPCItemValues[1].Value.ToString(); } else { debugBox.Text += OPCItems[1].ResultID.Description; } } catch (Exception ex) { debugBox.Text += ex.ToString(); } }
private void InitPlantState() { // Aggiorno a mano i valori di tre tag int maxAge = 0; ItemIdentifier[] OPCItems = new ItemIdentifier[1]; ItemValue[] OPCItemValues = null; OPCItems[0] = new ItemIdentifier { ItemName = "its-iot-device.Device1.PlantStatus", ClientHandle = 1 }; //OPCItems[1] = new ItemIdentifier(); //OPCItems[1].ItemName = "its-iot-device.Device1.PieceCounter"; //OPCItems[1].ClientHandle = 2; //OPCItems[2] = new ItemIdentifier(); //OPCItems[2].ItemName = "its-iot-device.Device1.DefectedPiecesCounter"; //OPCItems[2].ClientHandle = 3; Console.WriteLine(OPCItems[0].ItemName + "--STATUS \n"); //Console.WriteLine(OPCItems[1].ItemName + "--PIECE COUNTER \n"); //Console.WriteLine(OPCItems[2].ItemName + "--DEFECTED PIECES\n"); try { daServerMgt.Read(maxAge, ref OPCItems, out OPCItemValues); /*if (OPCItems[0].ResultID.Succeeded & OPCItemValues[0].Quality.IsGood) * { * Console.WriteLine(OPCItemValues[0].Value.ToString() + "\n"); * } * else * { * Console.WriteLine(OPCItems[0].ResultID.Description + "\n"); * }*/ if (OPCItems[0].ResultID.Succeeded & OPCItemValues[0].Quality.IsGood) { Console.WriteLine(OPCItemValues[0].Value.ToString() + "\n"); Enum.TryParse <PlantState>(OPCItemValues[0].Value.ToString(), out PlantState plant); if (_plantState == null) { _plantState = new PlantStateHandler(OPCItemValues[0].TimeStamp, plant); } OnPlantStateChange(OPCItemValues[0].TimeStamp, plant); } else { Console.WriteLine(OPCItems[0].ResultID.Description + "\n"); } /*if (OPCItems[2].ResultID.Succeeded & OPCItemValues[1].Quality.IsGood) * { * Console.WriteLine(OPCItemValues[2].Value.ToString() + "\n"); * } * else * { * Console.WriteLine(OPCItems[2].ResultID.Description + "\n"); * }*/ } catch (Exception ex) { // ab edit: aggiungiamo ilcontesto per capire l'errore Console.WriteLine($"InitPlantState exeception: {ex.ToString()}"); } }