C#l连接OPC进行数据交互

步骤 :引用 OPCNETAPI.DLL&&OPCNETAPI.COM.DLL

          1。查询服务器      2. 连接服务器  3. 读取数据     4.写入数据

1.查询服务器 :根据IP地址进行查询

代码如下 :

          //查询服务器
            try
            {
                Opc.Server[] servers = m_discovery.GetAvailableServers(Specification.COM_DA_20, “OPC服务器IP地址”, null);
                if (servers != null)
                {   //遍历所查询到的所有OPC服务器,将其新增到 comboBox1 下拉列表框中
                    foreach (Opc.Da.Server server in servers)
                    {
                        comboBox1.Items.Add(server.Name);
                    }
                }
                comboBox1.SelectedIndex = 0;
                listBox1.Items.Add("查询服务器成功.请选择OPC进行连接");
            }
            catch (Exception ex)
            {

                listBox1.Items.Add(ex.Message);
            }

2. 连接服务器

   

                try
                {
                    Opc.Server[] servers = m_discovery.GetAvailableServers(Specification.COM_DA_20, “OPCIP地址”, null);
                    if (servers != null)
                    {
                        foreach (Opc.Da.Server server in servers)
                        {
                            if (String.Compare(server.Name, comboBox1.Text, true) == 0)//为true忽略大小写
                            {
                                m_server = server;//建立连接。
                                break;
                            }
                        }
                    }
                }
                catch (Exception ex)
                {

                    listBox1.Items.Add(ex.Message);
                    return;
                }
                if (m_server != null)
                {
                    try
                    {
                        m_server.Connect();
                        listBox1.Items.Add("OPC服务器连接成功,请填写变量名称进行读取数据");

                    }
                    catch (Exception ex)
                    {
                        listBox1.Items.Add(ex.Message);

                    }

                }
                else
                {
                    listBox1.Items.Add("连接失败,请检查IP以及服务器对象");
                }
            }

3. 读取

         try
            {

                state = new Opc.Da.SubscriptionState();//组(订阅者)状态,相当于OPC规范中组的参数
                state.Name ="";//组名 可为空
                state.ServerHandle = null;//服务器给该组分配的句柄。
                state.ClientHandle = Guid.NewGuid().ToString();//客户端给该组分配的句柄。
                state.Active = true;//激活该组。
                state.UpdateRate = 1000;//刷新频率为1秒。
                state.Deadband = 0;// 死区值,设为0时,服务器端该组内任何数据变化都通知组。
                state.Locale = null;//不设置地区值。
                 subscription = (Opc.Da.Subscription)m_server.CreateSubscription(state);//创建组
                string[] itemName = new string[1];
                itemName[0] = textBox2.Text; //OPC变量名称(项目代码)
                Item[] items = new Item[1];//定义数据项,即item
                items[0] = new Item();
                items[0].ClientHandle = Guid.NewGuid().ToString();//客户端给该数据项分配的句柄。
                items[0].ItemPath = null; //该数据项在服务器中的路径。
                items[0].ItemName = itemName[0]; //该数据项在服务器中的名字。
                 subscription.AddItems(items);

                 ItemValueResult[] values = subscription.Read(subscription.Items);
                 for (int i = 0; i < values.Length; i++)
                {
                    if (values[i].ToString() != "")
                     {
                        MessageBox.Show("4.1" + values[i].Value.ToString());
                    }
                }
 
                if (values[0].Quality.Equals(Opc.Da.Quality.Good))
                {
                     textBox3.Text = values[0].Value.ToString();//变量接收取到的值
                    listBox1.Items.Add("成功读取变量为<" + textBox2.Text + ">的数据.值为<" + textBox3.Text + ">");
                 }
               
            }
            catch (Exception ex)
            {
                 listBox1.Items.Add(ex.Message);
            }

4. 写入

            state = new Opc.Da.SubscriptionState();//组(订阅者)状态,相当于OPC规范中组的参数
            state.Name = "";// textBox4.Text;//组名
            state.ServerHandle = null;//服务器给该组分配的句柄。
            state.ClientHandle = Guid.NewGuid().ToString();//客户端给该组分配的句柄。
            state.Active = true;//激活该组。
            state.UpdateRate = 1000;//刷新频率为1秒。
            state.Deadband = 0;// 死区值,设为0时,服务器端该组内任何数据变化都通知组。
            state.Locale = null;//不设置地区值。 
            subscription = (Opc.Da.Subscription)m_server.CreateSubscription(state);//创建组
            string[] itemName = new string[1];
            itemName[0] =textBox7.Text;  //OPC变量名称  (项目代码)
            Item[] items = new Item[1];//定义数据项,即item
            items[0] = new Item();
            items[0].ClientHandle = Guid.NewGuid().ToString();//客户端给该数据项分配的句柄。
            items[0].ItemPath = null; //该数据项在服务器中的路径。
            items[0].ItemName = itemName[0]; //该数据项在服务器中的名字。
            subscription.AddItems(items);       
            ItemValue[] itemvalues2 = new ItemValue[3];
            listBox1.Items.Add(string.Format("subscription Items count:{0}", subscription.Items.Length));
            listBox1.Items.Add(string.Format("subscription Items[0]:{0}", subscription.Items[0].ItemName));
            ItemValue iv = new ItemValue((ItemIdentifier)subscription.Items[0]);
            iv.Value = textBox8.Text;//需要写入OPC中的值
            subscription.Write(new ItemValue[] { iv });
            subscription.RemoveItems(subscription.Items);
            m_server.CancelSubscription(subscription);//m_server前文已说明,通知服务器要求删除组。

原文地址:https://www.cnblogs.com/-lxl/p/5817686.html