/// <summary> /// Called to connect to a server. /// </summary> public void OnConnect() { Cursor = Cursors.WaitCursor; try { OpcUserIdentity credentials = null; do { try { m_server.Connect(new OpcConnectData(credentials, m_proxy)); break; } catch (Exception e) { MessageBox.Show(e.Message); } credentials = new NetworkCredentialsDlg().ShowDialog(credentials); }while (credentials != null); // initialize controls. SelectServerCTRL.OnConnect(m_server); StatusCTRL.Start(m_server); SubscriptionsCTRL.ShowSubscriptions(m_server); // register for shutdown events. m_server.ServerShutdownEvent += new OpcServerShutdownEventHandler(Server_ServerShutdown); // save settings. SaveSettings(); } catch (Exception e) { MessageBox.Show(e.Message, "On Connect"); m_server = null; } Cursor = Cursors.Default; }
public void Run() { try { const string serverUrl = "opcae://localhost/Technosoftware.AeSample"; Console.WriteLine(); Console.WriteLine("Simple OPC AE Client based on the OPC AE Client SDK .NET Standard"); Console.WriteLine("-----------------------------------------------------------------"); Console.Write(" Press <Enter> to connect to "); Console.WriteLine(serverUrl); Console.ReadLine(); Console.WriteLine(" Please wait..."); TsCAeServer myAeServer = new TsCAeServer(); // Connect to the server myAeServer.Connect(serverUrl); Console.WriteLine(" Connected, press <Enter> to create an active subscription and press <Enter>"); Console.WriteLine(" again to deactivate the subscription. This stops the reception of new events."); Console.ReadLine(); TsCAeSubscriptionState state = new TsCAeSubscriptionState { Active = true, BufferTime = 0, MaxSize = 0, ClientHandle = 100, Name = "Simple Event Subscription" }; TsCAeCategory[] categories = myAeServer.QueryEventCategories((int)TsCAeEventType.Condition); TsCAeAttribute[] attributes = null; attributes = myAeServer.QueryEventAttributes(categories[0].ID); int[] attributeIDs = new int[2]; attributeIDs[0] = attributes[0].ID; attributeIDs[1] = attributes[1].ID; TsCAeSubscription subscription; subscription = (TsCAeSubscription)myAeServer.CreateSubscription(state); subscription.DataChangedEvent += OnDataChangedEvent; subscription.SelectReturnedAttributes(categories[0].ID, attributeIDs); Console.ReadLine(); subscription.DataChangedEvent -= OnDataChangedEvent; Console.WriteLine(" Subscription deactivated, press <Enter> to remove the subscription and disconnect from the server."); subscription.Dispose(); // Remove subscription myAeServer.Disconnect(); // Disconnect from server myAeServer.Dispose(); // Dispose server object Console.ReadLine(); Console.WriteLine(" Disconnected from the server."); Console.WriteLine(); } catch (OpcResultException e) { Console.WriteLine(String.Format(" {0}", e.Message)); Console.ReadLine(); return; } catch (Exception e) { Console.WriteLine(String.Format(" {0}", e.Message)); Console.ReadLine(); return; } }