using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Reflection; using System.Threading.Tasks; using System.Windows.Forms; using OpcLabs.EasyOpc.UA; using OpcLabs.EasyOpc.UA.OperationModel; using OpcLabs.BaseLib.DataTypeModel; using OpcLabs.EasyOpc.UA.ComplexData; using OpcLabs.BaseLib.ComponentModel; using OpcLabs.EasyOpc.DataAccess; using OpcLabs.EasyOpc.OperationModel; namespace TestOPCUAClient { public partial class Form1 : Form { EasyUAClient mClient; EasyDAClient mDAClient; public Form1() { InitializeComponent(); RegisterManagedResource(); // Set the EasyUA client to not care about certificates EasyUAClient.SharedParameters.EngineParameters.CertificateAcceptancePolicy.AcceptAnyCertificate = true; // Instantiate the client objects mClient = new EasyUAClient(); mDAClient = new EasyDAClient(); // Check license btnGetLicense_Click(null, null); //initialize default end point txtEndpoint.Text = "opc.tcp://10.97.219.120:4840/"; } private void btnGetAlarms_Click(object sender, EventArgs e) { if (txtEndpoint.TextLength > 5) { try { // Create the Endpoint // Call the Method // Call the Krones UA Method "GetActiveMessages" on object "MethodSet" // Parameters are Endpoint = URL for server, ObjectNodeDescriptor = Node on Server, MethodNodeDescriptor = Method Descreptor (not just name). UAEndpointDescriptor endpointDescriptor = new UAEndpointDescriptor("opc.tcp://10.97.219.120:4840"); object[] lOutput = mClient.CallMethod(endpointDescriptor, "ns=2;s=MethodSet", "ns=9;s=DMM.Messages.GetActiveMessages"); // This is for stopping in debug and looking at what the object returned is like. UAGenericObject go = lOutput[0] as UAGenericObject; var firstObject = lOutput[0]; if (go != null) { var sd = (StructuredData)go.GenericData; lData = sd.ToString(); // first get the ID var lPd = (PrimitiveData)sd.FieldData["Id"]; int lId = (int)lPd.Value; // Second get the text. Since it is in a Subtype, need to get the subtype first. Then the text is in an array, need to first cast it as an array. var lMessageConfiguration = (StructuredData)sd.FieldData["MessageConfiguration"]; var lTextArray = (SequenceData)lMessageConfiguration.FieldData["Text"]; var lLocalzedText = (StructuredData)lTextArray.Elements[0]; var lTextPd = (PrimitiveData)lLocalzedText.FieldData["Text"]; string lText = (string)lTextPd.Value; txtAlarm.Text = String.Format("Id={0}, Msg={1}", lId, lText); // Output the whole returned object and then the verbose formatted generic data and then the first value in the list as structured data. txtOutput.Text = "/n/nGenericData to String/n" + lData; } object[] lChildObject = (object[])lOutput[0]; for (int i = 0; i < lChildObject.Length; i++) { UAGenericObject go4 = lChildObject[i] as UAGenericObject; txtOutput.Text += "\n" + lChildObject[i].ToString(); } } catch (UAException uaException) { txtOutput.Text = String.Format("*** Failure:\n {0}", uaException.ToString()); } } else txtOutput.Text = "Please enter a valid endpoint URI of the form opc.tcp://nnn.nnn.nnn.nnn:4840"; } private void RegisterManagedResource() { //LicensingManagement.Instance.RegisterManagedResource("QuickOPC", "Multipurpose", // Assembly.GetExecutingAssembly(), "TestOpcUAClientNewSDK.EasyOpcLicense.Key-Evaluation-WebForm-1999005037-20210209.bin"); //Assembly.GetExecutingAssembly(), "TestOPCUAClient.EasyOpcLicense.Key-Permanent-WebForm-1999356803-20190329.bin"); } private void btnGetLicense_Click(object sender, EventArgs e) { // Check license txtOutput.Text = String.Format("SerialNumber: {0} at {1}", (uint)mClient.LicenseInfo["Multipurpose.SerialNumber"], DateTime.Now); mClient.InstanceParameters.PluginSetups.FindName("UAComplexData").Enabled = true; string lIsEnabled = mClient.InstanceParameters.PluginSetups.FindName("UAComplexData").Enabled.ToString(); txtOutput.Text += "; UAComplexData Is Enabled=" + lIsEnabled; } } }