private void initData()
        {
            //下一步增加数据连接,保证能够对OPC UA服务器进行修改

            Subscription in_Subscription = m_Server.AddSubscription(100);
            object       serverHandle    = null;            //初始化服务器句柄
            String       nodeIDXStr      = "ns=2;s=Channel1.Device1.Tag1";
            NodeId       nodeIdX         = new NodeId(nodeIDXStr);

            String nodeIDYStr = "ns=2;s=Channel1.Device1.Tag2";
            NodeId nodeIdY    = new NodeId(nodeIDYStr);

            String iidX  = "motorXPosition";
            object itemX = iidX;

            String iidY  = "motorYPosition";
            object itemY = iidY;

            try
            {
                in_Subscription.AddDataMonitoredItem(nodeIdX, itemX, ClientApi_ValueChanged, 100, out serverHandle);
                in_Subscription.AddDataMonitoredItem(nodeIdY, itemY, ClientApi_ValueChanged, 100, out serverHandle);

                MainWindow.myMotorPos.XValue = 0;
                MainWindow.myMotorPos.YValue = 0;
            }
            catch (Exception ex)
            {
                Console.WriteLine("添加项时出现异常,异常信息:" + ex);
            }
        }
Beispiel #2
0
        /// <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_Subscipition == null)
            {
                try
                {
                    // Handle is not stored since we delete the whole subscription
                    object monitoredItemServerHandle = null;

                    // Create subscription
                    m_Subscipition  = m_Server.AddSubscription(100);
                    btnMonitor.Text = "Stop";

                    // Create first monitored item
                    m_Subscipition.AddDataMonitoredItem(
                        new NodeId(txtIdentifier1.Text, m_NameSpaceIndex),
                        txtMonitored1,
                        ClientApi_ValueChanged,
                        100,
                        out monitoredItemServerHandle);

                    // Create second monitored item
                    m_Subscipition.AddDataMonitoredItem(
                        new NodeId(txtIdentifier2.Text, m_NameSpaceIndex),
                        txtMonitored2,
                        ClientApi_ValueChanged,
                        100,
                        out monitoredItemServerHandle);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Establishing data monitoring failed:\n\n" + ex.Message);
                }
            }
            else
            {
                try
                {
                    m_Server.RemoveSubscription(m_Subscipition);
                    m_Subscipition = null;

                    btnMonitor.Text    = "Monitor";
                    txtMonitored1.Text = "";
                    txtMonitored2.Text = "";
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Stopping data monitoring failed:\n\n" + ex.Message);
                }
            }
        }
Beispiel #3
0
        public void CreateMySubscipition(StationModel station, APPConfiguration appconfig)
        {
            object       monitoredItemServerHandle = null;
            int          interval       = Convert.ToInt32(appconfig.PublishingInterval);
            uint         rate           = Convert.ToUInt32(appconfig.SamplingRate);
            Subscription mysubscription = station.StationOPCServer.AddSubscription(interval);

            for (int i = 0; i < station.MonitorNodes.Count; i++)
            {
                mysubscription.AddDataMonitoredItem(
                    station.MonitorNodes[i],
                    station.MonitorClientHandl[i],
                    rate,
                    out monitoredItemServerHandle);
            }
        }
        private void initData(opcUAServer curServer)
        {
            Subscription in_Subscription = curServer.AddSubscription(100);
            object       serverHandle    = null;            //初始化服务器句柄
            String       nodeIDStr       = "ns=2;s=curSimulation.IOGroup.Input0";
            NodeId       nodeId          = new NodeId(nodeIDStr);

            String iid  = "data1";
            object item = iid;

            try
            {
                in_Subscription.AddDataMonitoredItem(nodeId, item, ClientApi_ValueChanged, 100, out serverHandle);

                dataGridData myData = new dataGridData();
                myData.IID      = iid;
                myData.varName  = "I0";
                myData.varValue = 0;
                beckhoffInputData.Add(myData);
            }
            catch (Exception ex)
            {
                Console.WriteLine("添加项时出现异常,异常信息:" + ex);
            }

            serverHandle = null;                     //初始化服务器句柄
            nodeIDStr    = "ns=2;s=curSimulation.IOGroup.Input1";
            nodeId       = new NodeId(nodeIDStr);

            iid  = "data2";
            item = iid;

            try
            {
                in_Subscription.AddDataMonitoredItem(nodeId, item, ClientApi_ValueChanged, 100, out serverHandle);

                dataGridData myData = new dataGridData();
                myData.IID      = iid;
                myData.varName  = "I1";
                myData.varValue = 0;
                beckhoffInputData.Add(myData);
            }
            catch (Exception ex)
            {
                Console.WriteLine("添加项时出现异常,异常信息:" + ex);
            }

            for (int i = 2; i < 16; i++)
            {
                dataGridData myData = new dataGridData();
                myData.varName  = "I" + i;
                myData.varValue = 0;
                beckhoffInputData.Add(myData);
            }
            for (int i = 0; i < 16; i++)
            {
                dataGridData myData = new dataGridData();
                myData.varName  = "O" + i;
                myData.varValue = 0;
                beckhoffOutputData.Add(myData);
            }

            dataGridData otherData = new dataGridData();

            otherData.varName  = "AI1";
            otherData.varValue = 0;
            beckhoffOtherData.Add(otherData);

            otherData          = new dataGridData();
            otherData.varName  = "AO1";
            otherData.varValue = 0;
            beckhoffOtherData.Add(otherData);

            otherData          = new dataGridData();
            otherData.varName  = "电流";
            otherData.varValue = 0;
            beckhoffOtherData.Add(otherData);

            otherData          = new dataGridData();
            otherData.varName  = "气压";
            otherData.varValue = 0;
            beckhoffOtherData.Add(otherData);
        }
Beispiel #5
0
        /// <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));
                NodeId nodeId  = new NodeId(sNodeId);

                // Create the subscription if it does not already exist.
                if (m_Subscription == null)
                {
                    m_Subscription = m_Server.AddSubscription(100);
                }

                // 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.
                    m_Subscription.AddDataMonitoredItem(nodeId, item, ClientApi_ValueChanged, 100, out serverHandle);

                    // Update status label.
                    OnUpdateStatusLabel("Adding monitored item succeeded for NodeId:" +
                                        nodeId.ToString(), 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);
            }
        }