private void connectBtn_Click(object sender, EventArgs e) { myHelperApi.ItemChangedNotification -= new MonitoredItemNotificationEventHandler(Notification_MonitoredItem); myHelperApi.ItemEventNotification -= new MonitoredItemNotificationEventHandler(Notification_EventItem); myHelperApi.KeepAliveNotification -= new KeepAliveEventHandler(Notification_KeepAlive); myHelperApi.CertificateValidationNotification += new CertificateValidationEventHandler(Notification_ServerCertificate); myHelperApi.KeepAliveNotification += new KeepAliveEventHandler(Notification_KeepAlive); string url = "opc.tcp://" + txtIpPort.Text + "/"; EndpointDescription eds = new EndpointDescription(url); try { myHelperApi.Connect(eds, true, false, "", "").Wait(); txtIpPort.BackColor = Color.Green; MessageBox.Show("Connection Success", "Inform", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch { MessageBox.Show("Connection Error", "Inform", MessageBoxButtons.OK, MessageBoxIcon.Error); txtIpPort.BackColor = Color.Red; } mySession = myHelperApi.Session; if (mySession != null && mySession.Connected) { Debug.WriteLine("Session success connected"); try { mySubscription = myHelperApi.Subscribe(1000); myEventSubscription = myHelperApi.Subscribe(1000); } catch { Debug.WriteLine("Error subscribe connection"); } myHelperApi.CertificateValidationNotification -= new CertificateValidationEventHandler(Notification_ServerCertificate); myHelperApi.KeepAliveNotification -= new KeepAliveEventHandler(Notification_KeepAlive); myHelperApi.ItemChangedNotification += new MonitoredItemNotificationEventHandler(Notification_MonitoredItem); myHelperApi.ItemEventNotification += new MonitoredItemNotificationEventHandler(Notification_EventItem); myHelperApi.KeepAliveNotification += new KeepAliveEventHandler(Notification_KeepAlive); myRfidNamespaceIndex = GetRfidNamespaceIndex(); myRfidMethodIdentifiers = new UARfidMethodIdentifiers(myHelperApi); } }
// Подключение к считывателю по OPC UA public void connectOPC() { // Точка подключения EndpointDescription eds = new EndpointDescription("opc.tcp://" + url + "/"); myHelperApi.CertificateValidationNotification += new CertificateValidationEventHandler(Notification_ServerCertificate); myHelperApi.KeepAliveNotification += new KeepAliveEventHandler(Notification_KeepAlive); try { myHelperApi.Connect(eds, true, false, "", "").Wait(); Debug.WriteLine("___Success connection___"); } catch { Debug.WriteLine("___Error connection___"); } // Проверка результатов подключения if (myHelperApi.Session != null && myHelperApi.Session.Connected) { mySubscribe = myHelperApi.Subscribe(1000); myHelperApi.CertificateValidationNotification += new CertificateValidationEventHandler(Notification_ServerCertificate); myHelperApi.KeepAliveNotification += new KeepAliveEventHandler(Notification_KeepAlive); // Запрос методов и пространства имен myRfidMethodIdentifiers = new UARfidMethodIdentifiers(myHelperApi); rfidNamespaceIdx = GetRfidNamespaceIndex(); } this.connected = (myHelperApi.Session != null && myHelperApi.Session.Connected); }
private void Subscribe(string nodeId, string motorType) { referenceDescriptionCollection = myClientHelperAPI.ReadStructProperties(nodeId); Subscription subscription = myClientHelperAPI.Subscribe(1000); mySubscriptions.Add(subscription); try { foreach (ReferenceDescription tempRefDesc in referenceDescriptionCollection) { //use different item names for correct assignment at the notificatino event if (tempRefDesc.DisplayName.ToString().Contains("startAnyway")) { if (motorType == "cooling") { coolingStartAnywayNodeId = tempRefDesc.NodeId.ToString(); } else if (motorType == "heating") { heatingStartAnywayNodeId = tempRefDesc.NodeId.ToString(); } } if (tempRefDesc.DisplayName.ToString().Contains("speed")) { if (motorType == "cooling") { coolingSpeedNodeId = tempRefDesc.NodeId.ToString(); } else if (motorType == "heating") { heatingSpeedNodeId = tempRefDesc.NodeId.ToString(); } } string monitoredItemName = motorType + "-" + tempRefDesc.DisplayName.ToString(); myMonitoredItem = myClientHelperAPI.AddMonitoredItem(mySubscriptions[mySubscriptions.Count() - 1], tempRefDesc.NodeId.ToString(), monitoredItemName, 1); myClientHelperAPI.ItemChangedNotification = new MonitoredItemNotificationEventHandler(Notification_MonitoredItem); } } catch (Exception ex) { MessageBox.Show(ex.Message, "Error"); } }
/// <summary> /// Starts the monitoring of the values of the two variables entered in the From. /// The NodeIds used for the monitoring are constructed from the identifier entered /// in the Form and the namespace index detected in the connect method /// </summary> private void btnMonitor_Click(object sender, EventArgs e) { // Check if we have a subscription // - No -> Create a new subscription and create monitored items // - Yes -> Delete Subcription if (m_Subscription == null) { try { // Create subscription m_Subscription = m_Server.Subscribe(1000); m_Server.ItemChangedNotification += new MonitoredItemNotificationEventHandler(ClientApi_ValueChanged); btnMonitor.Text = "Stop"; // Create first monitored item m_Server.AddMonitoredItem(m_Subscription, new NodeId(txtIdentifier1.Text, m_NameSpaceIndex).ToString(), "item1", 100); // Create second monitored item m_Server.AddMonitoredItem(m_Subscription, new NodeId(txtIdentifier2.Text, m_NameSpaceIndex).ToString(), "item2", 100); } catch (Exception ex) { MessageBox.Show("Establishing data monitoring failed:\n\n" + ex.Message); } } else { try { m_Server.RemoveSubscription(m_Subscription); m_Subscription = null; btnMonitor.Text = "Monitor"; txtMonitored1.Text = ""; txtMonitored2.Text = ""; } catch (Exception ex) { MessageBox.Show("Stopping data monitoring failed:\n\n" + ex.Message); } } }
/// <summary> /// 连接服务器 /// </summary> /// <param name="server"></param> /// <returns></returns> public bool Connect(OpcServer server) { var uaServer = server as OpcUaServer; if (uaServer == null) { return(false); } uAClient.Connect((EndpointDescription)uaServer.EndpointDesCription.OpcEndpointDescription, uaServer.UserAuth, uaServer.UserName, uaServer.Password); _Subscription = uAClient.Subscribe(uaServer.SubscriptionPublishingInterval); return(true); }
/// <summary> /// Finishes a drag and drop action whereas this control is used as target. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param> private void MonitoredItems_DragDrop(object sender, DragEventArgs e) { try { // Retrieve the event data and create the according nodeid. String sNodeId = (String)e.Data.GetData(typeof(System.String)); // Create the subscription if it does not already exist. if (m_Subscription == null) { m_Subscription = m_Server.Subscribe(200); m_Server.ItemChangedNotification += new MonitoredItemNotificationEventHandler(ClientApi_ValueChanged); } // Add the attribute name/value to the list view. ListViewItem item = new ListViewItem(sNodeId); object serverHandle = null; // Prepare further columns. item.SubItems.Add("100"); // Sampling interval by default. item.SubItems.Add(String.Empty); item.SubItems.Add(String.Empty); item.SubItems.Add(String.Empty); item.SubItems.Add(String.Empty); item.SubItems.Add(String.Empty); try { // Add the item and apply any changes to it. MonitoredItem tempMonitoredItem = m_Server.AddMonitoredItem(m_Subscription, sNodeId, "item1", 100); tempMonitoredItem.Handle = item; serverHandle = tempMonitoredItem; // Update status label. OnUpdateStatusLabel("Adding monitored item succeeded for NodeId:" + sNodeId, true); } catch (ServiceResultException monitoredItemResult) { item.SubItems[5].Text = monitoredItemResult.StatusCode.ToString(); // Update status label. OnUpdateStatusLabel("An exception occured while adding an item: " + monitoredItemResult.Message, false); } item.Tag = serverHandle; MonitoredItemsLV.Items.Add(item); // Fit column width to the longest item and add a few pixel: MonitoredItemsLV.Columns[0].Width = -1; MonitoredItemsLV.Columns[0].Width += 15; // Fit column width to the column content: MonitoredItemsLV.Columns[1].Width = -2; MonitoredItemsLV.Columns[5].Width = -2; // Fix settings: MonitoredItemsLV.Columns[2].Width = 95; MonitoredItemsLV.Columns[3].Width = 75; MonitoredItemsLV.Columns[4].Width = 75; } catch (Exception exception) { // Update status label. OnUpdateStatusLabel("An exception occured while creating a subscription: " + exception.Message, false); } }