其他分享
首页 > 其他分享> > OPC学习

OPC学习

作者:互联网

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//using System.Windows.Forms;
using System.Net;
using System.Threading;
using OPCAutomation;
using System.Net.Sockets;
using OPCAutomation;
using System.Collections;


namespace MyOPC
{
    class Program
    {
        static List<OpcItem2> opcList = new List<OpcItem2>();
        static void Main(string[] args)
        {


            IPHostEntry IPhost = Dns.GetHostEntry(Dns.GetHostName());

            foreach (var item in IPhost.AddressList) //IPhost.AddressList包含了主机中网卡的地址
            {
                if (item.AddressFamily == AddressFamily.InterNetwork) //判断是否为ip4
                {
                    //Console.WriteLine(Dns.GetHostEntry(item).HostName);
                }
            }


            //定义OPC服务器;
            OPCServer kepServer;
            //定义OPC服务器Groups;
            OPCGroups kepGroups;
            OPCGroup kepGroup;
            OPCItems kepItems;
            OPCBrowser kepBrowser;
            //定义OPC变量集合
            
            List<int> serverHandles = new List<int>();
            List<int> clientHandles = new List<int>();
            List<string> tempIDList = new List<string>();
            List<string> listItems = new List<string>();
            

            //定义返回的OPC标签错误码
            Array iErrors;
            //定义要添加的OPC标签的标识符
            Array strTempIDs;
            Array strClientHandles;
            Array strServerHandles;
            Array readServerHandles;
            Array writeServerHandles;
            Array writeArrayHandles;
            Array readError;
            Array writeError;
            int readTransID=1;
            int writeTransID=1;
            int readCancelID;
            int writeCancelID;
            bool connectstate;

            kepServer = new OPCServer();

            //获取kep服务器名称
            object serverList = kepServer.GetOPCServers(Dns.GetHostName());

            Array arr1 = serverList as Array;

            List<string> arr2 = new List<string>();

            ArrayList arr3 = new ArrayList();

            foreach (string item in arr1)
            {
                arr3.Add(item);
                arr2.Add(item);
                //Console.WriteLine(item);
            }

            try
            {
                kepServer.Connect(arr2[1], Dns.GetHostName());
            }
            catch (Exception ex)
            {

                Console.WriteLine(ex.Message,"连接失败");
            }

            kepGroups = kepServer.OPCGroups;
            kepGroups.DefaultGroupDeadband = 0;//Group的默认死区,变化量超过死区后将会触发DataChange事件
            kepGroups.DefaultGroupIsActive = true; //以后添加的Group是否默认激活
            kepGroup = kepGroups.Add("Group1");
            kepGroup.IsActive = true;
            kepGroup.IsSubscribed = true; //订阅模式
            kepGroup.UpdateRate = 250; //刷新率

            kepGroup.AsyncReadComplete += KepGroup_AsyncReadComplete;


            //获取服务器的所有变量
            kepBrowser = kepServer.CreateBrowser();
            kepBrowser.ShowBranches();
            kepBrowser.ShowLeafs(true);

            //添加OPC服务器中的所有变量到listItems中
            listItems.Clear();
            foreach (var item in kepBrowser)
            {
                listItems.Add(item.ToString());
            }

            tempIDList.Clear();
            clientHandles.Clear();
            tempIDList.Add("0");
            clientHandles.Add(0);

            //foreach (string item in listItems)
            //{
            //    Console.WriteLine(item);
            //}

            string strSelect = "MDbus.Modbus.K4";

            opcList.Add(new OpcItem2
            {
                Tag = strSelect
            });
            int count = opcList.Count;

            for (int i = 0; i < count; i++)
            {
                tempIDList.Add(opcList[i].Tag);
                clientHandles.Add(i + 1);
            }

            strTempIDs = (Array)tempIDList.ToArray();
            strClientHandles = (Array)clientHandles.ToArray();
            kepItems = kepGroup.OPCItems;

            //kepGroup中的KepItems添加标签
            //strTempID就是opcList[i]中的Tag属性,clientHandles从1开始,
            kepItems.AddItems(opcList.Count, ref strTempIDs, ref strClientHandles, out strServerHandles, out iErrors);
            serverHandles.Clear();
            serverHandles.Add(0);
            for (int i = 0; i < count; i++)
            {
                serverHandles.Add(Convert.ToInt32(strServerHandles.GetValue(i + 1)));//读取的strServerHandles+1
            }
            readServerHandles = (Array)serverHandles.ToArray();

            //执行后,Value才有数值
            if (opcList.Count > 0)
            {
                if (kepServer != null)
                {
                    kepGroup.AsyncRead(opcList.Count, ref readServerHandles, out readError, readTransID, out readCancelID);
                }

            }


            //判断OPC是否正在运行
            if (kepServer.ServerState==(int)OPCServerState.OPCRunning)
            {
                connectstate = true;
            }
            
            //解析读取的OPC值
            int v1 = Convert.ToInt32(opcList[0].Value);
            string v0;
            v0 = opcList[0].Value;
            Console.WriteLine(v0);
            Console.ReadKey();
            //try
            //{
            //    v0 = opcList[0].Value;
            //}
            //catch (Exception ex)
            //{
            //    Console.WriteLine("赋值失败", ex);
            //    Console.ReadKey();
            //}



            int index = 0;
            int[] serverHandle = new int[] { 0, Convert.ToInt32(strServerHandles.GetValue(index + 1)) };
            writeServerHandles = (Array)serverHandle;

            object[] values = new object[2];
            values[0] = null;
            values[1] = "1111";//1为异步
            writeArrayHandles = (Array)values;

            kepGroup.AsyncWrite(1, writeServerHandles, writeArrayHandles,out writeError, writeTransID, out writeCancelID);
            //kepGroup.SyncWrite(1, ref writeServerHandles, ref writeArrayHandles, out writeError);
        }

        //使用委托, 并向opcList中赋值
        private static void KepGroup_AsyncReadComplete(
            int TransactionID, int NumItems, ref Array ClientHandles, 
            ref Array ItemValues, ref Array Qualities, ref Array TimeStamps, ref Array Errors)
        {
            for (int i = 1; i <=NumItems; i++)
            {
                object Value = ItemValues.GetValue(i);
                if (Value!=null)
                {
                    opcList[i - 1].Value = Value.ToString();
                    opcList[i - 1].Time = Convert.ToDateTime(TimeStamps.GetValue(i));
                }
            }
        }
    }
}

标签:int,List,System,学习,OPC,using,Array,opcList
来源: https://blog.csdn.net/helldoger/article/details/121667060