public static void Main(string[] args) { //// If the server domain name does not match localhost just replace it //// e.g. with the IP address or name of the server machine. var client = new OpcClient("opc.tcp://localhost:4840/SampleServer"); client.Connect(); // The mapping of the UNECE codes to OPC UA (OpcEngineeringUnitInfo.UnitId) is available here: // http://www.opcfoundation.org/UA/EngineeringUnits/UNECE/UNECE_to_OPCUA.csv var temperatureNode = client.BrowseNode("ns=2;s=Machine_1/Temperature") as OpcAnalogItemNodeInfo; if (temperatureNode != null) { var temperatureUnit = temperatureNode.EngineeringUnit; var temperatureRange = temperatureNode.EngineeringUnitRange; var temperature = client.ReadNode(temperatureNode.NodeId); Console.WriteLine( "Temperature: {0} {1}, Range: {3} {1} to {4} {1} ({2})", temperature.Value, temperatureUnit.DisplayName, temperatureUnit.Description, temperatureRange.Low, temperatureRange.High); } var pressureNode = client.BrowseNode("ns=2;s=Machine_1/Pressure") as OpcAnalogItemNodeInfo; if (pressureNode != null) { var pressureUnit = pressureNode.EngineeringUnit; var pressureInstrumentRange = pressureNode.InstrumentRange; var pressure = client.ReadNode(pressureNode.NodeId); Console.WriteLine( "Pressure: {0} {1}, Range: {3} {1} to {4} {1} ({2})", pressure.Value, pressureUnit.DisplayName, pressureUnit.Description, pressureInstrumentRange.Low, pressureInstrumentRange.High); } client.Disconnect(); Console.ReadKey(true); }
public static void Main(string[] args) { //// If the server domain name does not match localhost just replace it //// e.g. with the IP address or name of the server machine. var client = new OpcClient("opc.tcp://localhost:4840/SampleServer"); client.Connect(); // The class OpcObjectTypes contains all default OpcNodeId's of all nodes typically // provided by default by every OPC UA server. Here we start browsing at the top most // node the ObjectsFolder. Beneath this node there are all further nodes in an // OPC UA server registered. // Creating an OpcBrowseNode does only prepare an OpcBrowseNodeContext which is linked // to the OpcClient which creates it and contains all further browsing relevant // contextual metadata (e.g. the view and the referenceTypeIds). The used overload of // CreateBrowseNode(...) browses the default view of the server while it takes care of // HierarchicalReferences(see ReferenceTypeIds). // After creating the browse node for the ObjectsFolder we traverse the whole node // tree in preorder. // In case there you have the OpcNodeId of a specific node (e.g. after browsing for a // specific node) it is also possible to pass that OpcNodeId to CreateBrowseNode(...) // to browse starting at that node. var node = client.BrowseNode(OpcObjectTypes.ObjectsFolder); Program.Browse(node); client.Disconnect(); Console.ReadKey(true); }
private static IEnumerable <OpcMonitoredItem> CreateMonitoredItems(OpcClient client, OpcNodeId rootNodeId) { var node = client.BrowseNode(rootNodeId); foreach (var childNode in node.Children()) { yield return(new OpcMonitoredItem(childNode.NodeId, OpcAttribute.Value)); } }
private static IEnumerable <OpcSubscribeDataChange> CreateCommands(OpcClient client, OpcNodeId rootNodeId) { var node = client.BrowseNode(rootNodeId); foreach (var childNode in node.Children()) { yield return(new OpcSubscribeDataChange(childNode.NodeId, HandleDataChange)); } }
//private BackgroundWorker backgroundWorker; public MainWindow() { //opcUaWorker.OPCNotify += OpcUaWorker_OPCNotify; InitializeComponent(); Firstcheck = false; on = new SolidColorBrush(Color.FromRgb(0, 255, 0)); error = new SolidColorBrush(Color.FromRgb(255, 0, 0)); neutral = new SolidColorBrush(Color.FromRgb(221, 221, 221)); Main_pressure.IsReadOnly = true; Crio_pressure.IsReadOnly = true; ForVac_pressure.IsReadOnly = true; PnePressure.IsReadOnly = true; CamTemperature.IsReadOnly = true; Crio_temperature.IsReadOnly = true; UpdateTimerCallBack = new TimerCallback(delegate { UpdateGUI(null); }); UpdateTimer = new Timer(UpdateTimerCallBack, null, 0, 1000); var client = new OpcClient("opc.tcp://192.168.0.10:4840/"); client.Connect(); var node = client.BrowseNode("ns=3;s=\"Crio_DB\".\"Crio\".\"Input\""); if (node is OpcVariableNodeInfo variablenode) { OpcNodeId datatypeid = variablenode.DataTypeId; OpcDataTypeInfo datatype = client.GetDataTypeSystem().GetType(datatypeid); Console.WriteLine(datatype.TypeId); Console.WriteLine(datatype.Encoding); Console.WriteLine(datatype.Name); foreach (OpcDataFieldInfo field in datatype.GetFields()) { Console.WriteLine(".{0} : {1}", field.Name, field.FieldType); } Console.WriteLine(); Console.WriteLine("data type attributes:"); Console.WriteLine( "\t[opcdatatype(\"{0}\")]", datatype.TypeId.ToString(OpcNodeIdFormat.Foundation)); Console.WriteLine( "\t[opcdatatypeencoding(\"{0}\", namespaceuri = \"{1}\")]", datatype.Encoding.Id.ToString(OpcNodeIdFormat.Foundation), datatype.Encoding.Namespace.Value); } Console.WriteLine("А я из основного потока. Просто надо так сделать"); }
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); }
private static IEnumerable <OpcNodeId> BrowseNodes(OpcClient client, OpcNodeId rootNodeId) { var rootNode = client.BrowseNode(rootNodeId); foreach (var node in rootNode.Children()) { var name = node.Name.Value; if (name.StartsWith("Var") || name == "Timestamp") { yield return(node.NodeId); } } }
private static List <OpcNodeInfo> AddNodesToList(string nodesEntered, OpcClient opcClient) { string[] nodeArray = nodesEntered.Split(' '); List <OpcNodeInfo> returnList = new List <OpcNodeInfo>(); OpcNodeInfo nodeInfo; foreach (string substring in nodeArray) { nodeInfo = opcClient.BrowseNode($"ns=2;s={substring}"); if (!nodeInfo.Name.IsNull) { returnList.Add(nodeInfo); } } return(returnList); }
static void Main(string[] args) { var client = new OpcClient("opc.tcp://opcuaserver.com:48010"); client.Connected += new EventHandler(ConnectedEvent); client.Disconnected += new EventHandler(DisconnectedEvent); client.Connect(); var node = client.BrowseNode(OpcObjectTypes.ObjectsFolder); Browse(node); Thread.Sleep(6000); client.Disconnect(); }
public JsonResult autoInsert([FromBody] JObject json) { var id_opcClient = json.GetValue("id").ToString(); var opcClient = repo._context.sys_opc_clients.Where(t => t.id == id_opcClient).Select(t => t.opc_client).SingleOrDefault(); var client = new OpcClient(opcClient); try { client.Connect(); } catch (Exception e) { ModelState.AddModelError("connectfail", e.ToString()); return(generateError()); } var node = client.BrowseNode(OpcObjectTypes.ObjectsFolder); Browse(id_opcClient, node); return(generateSuscess()); }
public static void Main() { using (var client = new OpcClient("opc.tcp://localhost:4841")) { client.Connect(); client.SubscribeEvent("ns=2;s=Machine/Jobs", HandleJobsEvent); while (true) { Console.WriteLine("Press any key to schedule a new job."); Console.ReadLine(); var jobsNode = client.BrowseNode("ns=2;s=Machine/Jobs"); var job = $"JOB{GetLastJob(jobsNode) + 1:D2}"; client.AddNode(new OpcAddDataVariableNode <int>( name: job, nodeId: OpcNodeId.Null, parentNodeId: jobsNode.NodeId, value: 0)); Console.WriteLine($"CLIENT: New job '{job}' schedulded."); } } }
public static void Main(string[] args) { //// If the server domain name does not match localhost just replace it //// e.g. with the IP address or name of the server machine. var client = new OpcClient("opc.tcp://localhost:4840/SampleServer"); client.Connect(); var methodsNode = client.BrowseNode("ns=2;s=Methods"); var methods = BrowseMethods(methodsNode).ToList(); do { Console.WriteLine("Enter index of method to call or 'exit':"); for (int index = 0; index < methods.Count; index++) { Console.WriteLine("{0}. {1}", index, methods[index].Node.DisplayName.Value); } var line = Console.ReadLine(); if (line == "exit") { break; } if (int.TryParse(line, out var methodIndex)) { var method = methods[methodIndex]; Console.WriteLine(); Console.WriteLine("Calling method '{0}' ...", method.Node.DisplayName.Value); var inputs = method.Node.GetInputArguments(); foreach (var input in inputs) { Console.WriteLine( "\t[IN] {0} : {1} // {2}", input.Name, OpcDataTypes.GetDataType(input.DataTypeId), input.Description); } var outputs = method.Node.GetOutputArguments(); foreach (var output in outputs) { Console.WriteLine( "\t[OUT] {0} : {1} // {2}", string.IsNullOrEmpty(output.Name) ? "ret" : output.Name, OpcDataTypes.GetDataType(output.DataTypeId), output.Description); } var inputArguments = new List <object>(); if (inputs.Length > 0) { Console.WriteLine(); foreach (var input in inputs) { Console.Write( "Enter '{0}' value of '{1}': ", OpcDataTypes.GetDataType(input.DataTypeId), input.Name); var value = Console.ReadLine(); if (input.DataTypeId == OpcDataTypes.String) { inputArguments.Add(value); } else if (input.DataTypeId == OpcDataTypes.Int32) { inputArguments.Add(int.Parse(value)); } } } try { var outputArguments = client.CallMethod(method.ParentId, method.Node.NodeId, inputArguments); Console.WriteLine("Call succeeded!"); for (int index = 0; index < outputs.Length; index++) { var output = outputs[index]; Console.WriteLine( "'{0}' value of '{1}': {2}", OpcDataTypes.GetDataType(output.DataTypeId), string.IsNullOrEmpty(output.Name) ? "ret" : output.Name, outputArguments[index]); } } catch (OpcException ex) { Console.WriteLine("Call failed: {0}", ex.Message); } Console.WriteLine(); } } while (true); client.Disconnect(); }
public static void Main(string[] args) { //// If the server domain name does not match localhost just replace it //// e.g. with the IP address or name of the server machine. var client = new OpcClient("opc.tcp://localhost:4840/SampleServer"); client.Connect(); var statusNode = client.BrowseNode("ns=2;s=Machine_1/Status") as OpcVariableNodeInfo; if (statusNode != null) { var statusValues = statusNode.DataType.GetEnumMembers(); var currentStatus = client.ReadNode(statusNode.NodeId); var currentStatusValue = null as OpcEnumMember; foreach (var statusValue in statusValues) { if (statusValue.Value == currentStatus.As <int>()) { currentStatusValue = statusValue; break; } } Console.WriteLine( "Status: {0} ({1})", currentStatusValue.Value, currentStatusValue.Name); Console.WriteLine("-> " + currentStatusValue.Description); Console.WriteLine(); Console.WriteLine("Possible status values..."); foreach (var statusValue in statusValues) { Console.WriteLine("{0} = {1}", statusValue.Value, statusValue.Name); } Console.Write("Enter new status: "); var value = Console.ReadLine(); var newStatus = 0; if (int.TryParse(value, out newStatus)) { client.WriteNode(statusNode.NodeId, newStatus); } currentStatus = client.ReadNode(statusNode.NodeId); foreach (var statusValue in statusValues) { if (statusValue.Value == currentStatus.As <int>()) { currentStatusValue = statusValue; break; } } Console.WriteLine(); Console.WriteLine( "New Status: {0} ({1})", currentStatusValue.Value, currentStatusValue.Name); Console.WriteLine("-> " + currentStatusValue.Description); } client.Disconnect(); Console.ReadKey(true); }
public void GetNodes() { var node = _client.BrowseNode(OpcObjectTypes.ObjectsFolder); Browse(node); }
public static void Main() { string stringEntered = ""; bool exitSession = false; List <OpcNodeInfo> listOfNodes; using (var client = new OpcClient("opc.tcp://localhost:4840")) { client.Connect(); while (true)//(!exitSession) { Console.WriteLine("Available commands: 'view' 'edit' 'disconnect'"); Console.Write("Enter a command from the above list: "); stringEntered = Convert.ToString(Console.ReadLine()); switch (stringEntered) { case "view": { Console.WriteLine("Enter the name(s) of the node(s) separated by whitespace(s):"); string fullString = Console.ReadLine(); List <OpcNodeInfo> nodeInfoList = AddNodesToList(fullString, client); Console.WriteLine("****************************"); foreach (OpcNodeInfo infoElement in nodeInfoList) { //Start here! } Console.WriteLine("****************************"); DisplayNodeInfo(nodeInfoList); break; } case "editValue": { //EditNodeValue(); break; } case "disconnect": { client.Disconnect(); Console.WriteLine("Session is exiting..."); exitSession = true; break; } default: { Console.WriteLine(stringEntered + " is not an accepted command"); break; } } string[] splittedString = stringEntered.Split(" "); listOfNodes = new List <OpcNodeInfo>(); //or initialize it when declared and clear the list here? OpcNodeInfo machineNode; foreach (string substring in splittedString) { machineNode = client.BrowseNode($"ns=2;"); // if(!machineNode.Name.IsNull) // { OpcNodeInfo jobnode = machineNode.Child("Job"); listOfNodes.Add(machineNode); //} } OpcBrowseNode test = new OpcBrowseNode("s=Message"); IEnumerable <OpcNodeInfo> infoAboutNodes = client.BrowseNodes(test); foreach (string element in splittedString) { List <OpcReadNode> liste = new List <OpcReadNode>(); liste.Add(new OpcReadNode("s=Message")); //OpcNodeInfo info = client.BrowseNode($"ns=2;s=Message"); List <OpcBrowseNode> liste2 = new List <OpcBrowseNode>(); //OpcBrowseNode test = new OpcBrowseNode("s=Message"); IEnumerable <OpcNodeInfo> info = client.BrowseNodes(test); Console.WriteLine("\n**********************************"); Console.WriteLine("Writing the infoElements..."); Console.WriteLine("**********************************\n"); foreach (OpcNodeInfo infoElement in info) { Console.WriteLine("NodeID: " + infoElement.NodeId); Console.WriteLine("InfoElement:: " + infoElement.ToString() + "\n"); } var nodeOfInterest = client.ReadNode($"ns=2;s={stringEntered}"); if (nodeOfInterest.Value != null) { Console.Write($"The value of the node is: {nodeOfInterest.Value}\t"); Console.WriteLine($"The ID of the node is: {nodeOfInterest.DataTypeId}\n"); } } if (client.State == OpcClientState.Connected) { var temperature = client.ReadNode("ns=2;s=Temperature"); var message = client.ReadNode("ns=2;s=Message"); var level = client.ReadNode("ns=2;s=Level"); Console.WriteLine($"Current Temperature is {temperature} °C"); Console.WriteLine($"Current message is {message}"); Console.WriteLine($"Level: {level}"); } Thread.Sleep(1000); } } }
public static void Main(string[] args) { if (args == null || args.Length == 0) { Console.WriteLine("Enter ip address of the server (or localhost)"); return; } string serverIP = args[0]; //// If the server domain name does not match localhost just replace it //// e.g. with the IP address or name of the server machine. //string serverURL = "https://192.168.0.102:4840/SampleServer"; //string serverURL = "opc.tcp://localhost:4840/SampleServer"; string serverURL = $"opc.tcp://{serverIP}:4840/SampleServer"; #region 1st Way: Use the OpcClient class. { // The OpcClient class interacts with one OPC UA server. While this class // provides session based access to the different OPC UA services of the // server, it does not implement a main loop. //var client = new OpcClient("opc.tcp://localhost:4840/SampleServer"); try { var client = new OpcClient(serverURL); Console.WriteLine($"Connected on {serverURL}"); client.Connect(); var node = client.BrowseNode(OpcObjectTypes.ObjectsFolder); Browse(node); while (true) { Program.CommunicateWithServer(client); Thread.Sleep(1000); } client.Disconnect(); } catch (Exception ex) { Console.WriteLine(ex.Message); } } #endregion #region 2nd Way: Use the OpcClientApplication class. { //// The OpcClientApplication class uses a single OpcClient instance which is //// wrapped within a main loop. //// //// Remarks //// - The app instance starts a main loop when the session to the server has //// been established. //// - Custom client/session dependent code have to be implemented within the event //// handler of the Started event. //var app = new OpcClientApplication("opc.tcp://localhost:4840/SampleServer"); //app.Started += Program.HandleAppStarted; //app.Run(); } #endregion }