///<summaray> ///Метод ReadIntegerValue осуществляет чтение данных OPC node ///и преобразования в тип int ///</summaray> ///<param name="intValue">Ссылка на объект DiscreteValue</param> ///<param name="Path">Путь до Node</param> ///<param name="client">OpcClient</param> private static void ReadIntegerValue(ref IntValue intValue, string Path, OpcClient client) { OpcValue var = client.ReadNode(Path); intValue.Path = Path; intValue.Value = int.Parse(var.ToString()); }
///<summaray> ///Метод ReadAnalogValue осуществляет чтение данных OPC node ///и преобразования в тип float ///</summaray> ///<param name="analogValue">Ссылка на объект AnalogValue</param> ///<param name="Path">Путь до Node</param> ///<param name="client">OpcClient</param> private static void ReadAnalogValue(ref AnalogValue analogValue, string Path, OpcClient client) { OpcValue var = client.ReadNode(Path); analogValue.Path = Path; analogValue.Value = float.Parse(var.ToString()); }
///<summaray> ///Метод ReadDiscreteValue осуществляет чтение данных OPC node ///и преобразования в тип bool ///</summaray> ///<param name="discreteValue">Ссылка на объект DiscreteValue</param> ///<param name="Path">Путь до Node</param> ///<param name="client">OpcClient</param> private static void ReadDiscreteValue(ref DiscreteValue discreteValue, string Path, OpcClient client) { OpcValue var = client.ReadNode(Path); discreteValue.Path = Path; discreteValue.Value = (bool)var.Value; }
public void Initialize(OpcClient client) { var node = client.BrowseNode(this.Id); var analogNode = node as OpcAnalogItemNodeInfo; if (analogNode != null) { this.Unit = analogNode.EngineeringUnit; } this.Value = client.ReadNode(this.Id); }
public void OPCConnectAndReadTest() { // connect to local kepware server (UA) OPC.Client client = new UAClient("127.0.0.1", 49320); Assert.IsTrue(client.Connect()); // demand read a value OpcValue value = client.Read("Simulation Examples.Functions.Ramp2", 2); Assert.IsTrue(value.Status.IsGood); // subscribe to a value client.DataChange += Client_DataChange; client.Subscribe("Simulation Examples.Functions.Ramp2", 2); // this format is outdated // wait up to 2 seconds for a DataChanged event Thread.Sleep(2000); // and clean up client.DataChange -= Client_DataChange; client.Dispose(); // now do it again, using OPC DA client = new DAClient("127.0.0.1", "Kepware.KEPServerEX.V6", "{7BC0CC8E-482C-47CA-ABDC-0FE7F9C6E729}"); Assert.IsTrue(client.Connect()); // demand read a value value = client.Read("Simulation Examples.Functions.Ramp2", 2); Assert.IsTrue(value.Status.IsGood); // subscribe to a value client.DataChange += Client_DataChange; //client.Subscribe("Simulation Examples.Functions.Ramp2", 2); this format is outdated // wait up to 2 seconds for a DataChanged event Thread.Sleep(2000); // and clean up client.DataChange -= Client_DataChange; client.Dispose(); }
private Task DoWork(OpcValue value) { return(Task.Run(() => { if (value != null && value.Value != null && !StopLoadTasks) { var currentTaskId = Convert.ToInt64(value.ToString()); if (currentTaskId == 0) { GetTaskToDosing(); } else { Logger.Error("Значение параметра CurrentTaskId не равно 0."); } } else { Logger.Error("Значение параметра CurrentTaskId равно null."); } })); }
// Update is called once per frame void Update() { Debug.Log("OPC: Inside unity Update function"); var client = new OpcClient("opc.tcp://192.168.0.103:53530/OPCUA/SimulationServer"); Debug.Log("OPC: Setting Client Successfull"); /* var certificate = OpcCertificateManager.CreateCertificate(client); * OpcCertificateManager.SaveCertificate("MyClientCertificate.pfx", certificate); * if (!client.CertificateStores.ApplicationStore.Contains(certificate)) * { * client.CertificateStores.ApplicationStore.Add(certificate); * } */ //client.Security.AutoAcceptUntrustedCertificates = true; //client.Security.VerifyServersCertificateDomains = false; //client.Security.UseOnlySecureEndpoints = false; Debug.Log("OPC: Setting security Parameters successfull"); Debug.Log("OPC: Trying to connect OPC using connect"); client.Connect(); // Connect to Server Debug.Log("OPC: opc Client Connected"); OpcValue value = client.ReadNode("ns=5;s=Counter1"); Debug.Log("OPC: Read Value Successfull"); Debug.Log("Counter1: " + value.ToString()); try { opcCurrentData.text = value.ToString(); Debug.Log("OPC: Value Display Successfull"); } catch (Exception e) { Debug.Log("OPC: Value Setting failed" + e.InnerException); } }
/// <summary> /// Read the values from the server /// </summary> /// <param name="opcNodes">opc ua nodes</param> /// <returns>null (error) or List<OpcValue> with the Values and StatusCodes</OpcValue></returns> public List <OpcValue> ReadValues(List <string> opcNodes) { List <OpcValue> opcValues = null; try { int count = opcNodes.Count; List <object> values; List <ServiceResult> statusCodes; List <NodeId> nodeIds = new List <NodeId>(count); List <Type> expectedTypes = new List <Type>(count); foreach (string node in opcNodes) { nodeIds.Add(new NodeId(node)); expectedTypes.Add(null); //no need to specify the data type } session.ReadValues(nodeIds, expectedTypes, out values, out statusCodes); opcValues = new List <OpcValue>(count); OpcValue opcValue; for (int i = 0; i < count; i++) { opcValue = new OpcValue(values[i], statusCodes[i]); opcValues.Add(opcValue); } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("Exception OpcUaClient::ReadValues " + ex.Message); opcValues = null; } return(opcValues); }
public static object GetParameterValue(OpcValue opcValue, ApiOpcParameter parameter) { if (opcValue != null) { try { var value = opcValue.ToString(); switch (parameter.Type) { case TagTypes.Decimal: return(Math.Round(Convert.ToDecimal(value), parameter.CharactersAmount)); case TagTypes.Int16: return(Convert.ToInt16(value)); case TagTypes.Int32: return(Convert.ToInt32(value)); case TagTypes.Long: return(Convert.ToInt64(value)); case TagTypes.Float: return(Convert.ToSingle(Math.Round(Convert.ToSingle(value), parameter.CharactersAmount))); default: return(opcValue.ToString()); } } catch (Exception ex) { Service.GetInstance().GetLogger().Error(ex); return(null); } } return(null); }
//public override OpcSubscription Subscribe(Common.DataSubscription subscriptionDef) //{ // string[] nodes = subscriptionDef.monitored_items.Split("$"); // string[] nodeparts; // int ns; // string path; // List<OpcSubscribeNode> nodesList = new List<OpcSubscribeNode>(); // foreach (string node in nodes) // { // nodeparts = node.Split(":"); // ns = int.Parse(nodeparts[0]); // path = nodeparts[1]; // nodesList.Add(new OpcSubscribeNode(path, ns)); // } // OpcSubscription sub = _client.SubscribeNodes(nodesList); // return sub; //} public override OpcValue Read(string node, int ns) { OpcValue value = _client.ReadNode("ns=" + ns + ";s=" + node); return(value); }