基本信息
源码名称:OPC DA(客户端)源码
源码大小:0.45M
文件格式:.zip
开发语言:C#
更新时间:2019-07-29
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559

本次赞助数额为: 2 元 
   源码介绍

using System;
using System.Windows.Forms;
using Opc;
using Opc.Da;
using OpcCom;
namespace opcTest
{
    public partial class Form1 : Form
    {
        private Opc.Da.Server m_server = null;//定义数据存取服务器
        private Opc.Da.Subscription subscription = null;//定义组对象(订阅者)
        private Opc.Da.SubscriptionState state = null;//定义组(订阅者)状态,相当于OPC规范中组的参数
        private Opc.IDiscovery m_discovery = new ServerEnumerator();//定义枚举基于COM服务器的接口,用来搜索所有的此类服务器。

        public Form1()
        {
            InitializeComponent();
            textBox1.Text = "192.168.1.5";
            button2.Enabled = false;
            button3.Enabled = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //查询服务器
            try
            {
                Opc.Server[] servers = m_discovery.GetAvailableServers(Specification.COM_DA_20, textBox1.Text, null);
                if (servers != null)
                {
                    foreach (Opc.Da.Server server in servers)
                    {
                        comboBox1.Items.Add(server.Name);
                    }
                }
                comboBox1.SelectedIndex = 0;
                listBox1.Items.Add("查询服务器成功.请选择OPC进行连接");

                button1.Enabled = false;
                button2.Enabled = true;
            }
            catch (Exception ex)
            {

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


        }


        private void button2_Click(object sender, EventArgs e)
        {
            //连接
            if (button2.Text=="释放")
            {
                //结束:释放各资源
                button2.Text = "连接";
                listBox1.Items.Add("释放成功.不能进行读取数据,请重新连接");
                button3.Enabled = false;
                try
                {
                    subscription.Dispose();//强制.NET资源回收站回收该subscription的所有资源。         
                    m_server.Disconnect();//断开服务器连接
                }
                catch (Exception ex)
                {
                    listBox1.Items.Add(ex.Message);
                }
            }
            else
            {
                try
                {
                    Opc.Server[] servers = m_discovery.GetAvailableServers(Specification.COM_DA_20, textBox1.Text, 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服务器连接成功,请填写变量名称进行读取数据");
                        button2.Text = "释放";
                        button3.Enabled = true;
                        
                    }
                    catch (Exception ex)
                    {
                        listBox1.Items.Add(ex.Message);

                    }

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

        private void button3_Click(object sender, EventArgs e)
        {


            try
            {
                state = new Opc.Da.SubscriptionState();//组(订阅者)状态,相当于OPC规范中组的参数
            state.Name = "newGroup";//组名
            state.ServerHandle = null;//服务器给该组分配的句柄。
            state.ClientHandle = Guid.NewGuid().ToString();//客户端给该组分配的句柄。
            state.Active = true;//激活该组。
            state.UpdateRate = 100;//刷新频率为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;
            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);
            if (values[0].Quality.Equals(Opc.Da.Quality.Good))
            {
                textBox3.Text = values[0].Value.ToString();
                listBox1.Items.Add("成功读取变量为<" textBox2.Text ">的数据.值为<" textBox3.Text ">");
            }
            subscription.RemoveItems(subscription.Items);
            m_server.CancelSubscription(subscription);//m_server前文已说明,通知服务器要求删除组。 
            }
            catch (Exception ex)
            {

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

        }

       
        

        private void alert(string msg)
        {
            MessageBox.Show(msg, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
         
        }

    }
}