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

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

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

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using OPCAutomation;//引用opc.dll

namespace OpcClient
{
    //C# 2.0 引入了局部类型的概念。局部类型允许我们将一个类、结构或接口分成几个部分,
    //分别实现在几个不同的.cs文件中。
    public partial class Form1 : Form
    {
        private OPCServer KepServer;
        private OPCGroups KepGroups;
        public OPCGroup KepGroup;
        private OPCItems KepItems;
        //private OPCItem KepItem;
        public object readValue;
        public List<string> serverNames = new List<string>();
        public List<string> Tags = new List<string>();

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.Text = "opc客户端";
            //获取服务器列表 默认本地
            string strHostIP = "localhost";//Console.ReadLine();
            IPHostEntry ipHostEntry = Dns.GetHostEntry(strHostIP);
            try
            {
                KepServer = new OPCServer();
                object serverList = KepServer.GetOPCServers(ipHostEntry.HostName.ToString());
                foreach (string serverName in (Array)serverList)
                {
                    pcIpName.Items.Add(serverName);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Connect Error:"   ex.Message);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                KepServer.Connect(pcIpName.SelectedItem.ToString(), "127.0.0.1");
                if (KepServer.ServerState == (int)OPCServerState.OPCRunning)
                {
                    this.Text = "客户端-->"   "    已连接到-"   KepServer.ServerName;
                    //MessageBox.Show("已连接到-"   KepServer.ServerName);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Connect Error:"   ex.Message);
                //Console.WriteLine("Connect Error:"   ex.Message);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                //组的初始化
                KepGroups = KepServer.OPCGroups;
                KepGroup = KepGroups.Add("");
                KepServer.OPCGroups.DefaultGroupIsActive = true;
                KepServer.OPCGroups.DefaultGroupDeadband = 0;
                KepGroup.UpdateRate = 100;
                KepGroup.IsActive = true;
                KepGroup.IsSubscribed = true;
                //异步读取添加事件处理
                KepGroup.DataChange  = this.UpdateItmes;
                //标记添加
                KepItems = KepGroup.OPCItems;
                //AddAllItems();
                
                KepItems.AddItem("Channel_0_User_Defined.Ramp.Ramp_Float", 1);
                KepItems.AddItem("Channel_0_User_Defined.Ramp.Ramp1", 2);
                KepItems.AddItem("Channel_0_User_Defined.Ramp.Ramp2", 3);
                KepItems.AddItem("Channel_0_User_Defined.Ramp.Ramp3", 4);
                KepItems.AddItem("Channel_0_User_Defined.Ramp.Ramp4", 5);
                KepItems.AddItem("Channel_0_User_Defined.Ramp.RampXL1", 6);
                KepItems.AddItem("Channel_0_User_Defined.Ramp.RampXL2", 7);
                KepItems.AddItem("Channel_0_User_Defined.Ramp.RampXL3", 8);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Create group error:"   ex.Message);
                //Console.WriteLine("Create group error:"   ex.Message);
            }
        }

        void KepGroup_AsyncWriteComplete(int TransactionID, int NumItems, ref Array ClientHandles, ref Array Errors)
        {
            throw new NotImplementedException();
        }



        

        private void UpdateItmes(int TransactionID, int NumItems, ref Array ClientHandles, ref Array ItemValues, ref Array Qualities, ref Array TimeStamps)
        {
            for (int i = 1; i <= NumItems; i  )
            {
                if (ClientHandles.GetValue(i).ToString() == "1") 
                    textBox1.Text = ItemValues.GetValue(i).ToString();
                if (ClientHandles.GetValue(i).ToString() == "2")
                    textBox2.Text = ItemValues.GetValue(i).ToString();
                if (ClientHandles.GetValue(i).ToString() == "3")
                    textBox3.Text = ItemValues.GetValue(i).ToString();
                if (ClientHandles.GetValue(i).ToString() == "4")
                    textBox4.Text = ItemValues.GetValue(i).ToString();
                if (ClientHandles.GetValue(i).ToString() == "5")
                    textBox5.Text = ItemValues.GetValue(i).ToString();
                if (ClientHandles.GetValue(i).ToString() == "6")
                    textBox6.Text = ItemValues.GetValue(i).ToString();
                if (ClientHandles.GetValue(i).ToString() == "7")
                    textBox7.Text = ItemValues.GetValue(i).ToString();
                if (ClientHandles.GetValue(i).ToString() == "8")
                    textBox8.Text = ItemValues.GetValue(i).ToString();  
            }
        }      
    }
}