• 大小: 115KB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2021-05-13
  • 语言: C#
  • 标签: c#  opc  plc  

资源简介

希望对有需要的朋友带来便利

资源截图

代码片段和文件信息

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.Collections;
using OPCAutomation;

namespace OPCSet 
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        #region 私有变量
        // OPCServer object
        OPCServer KepServer;
        //OPCGroups object
        OPCGroups KepGroups;
        // OPCGroup object
        OPCGroup KepGroup;
        // OPCItems object
        OPCItems KepItems;
        // OPCItem object
        OPCItem KepItem;
        //主机IP
        string strHostIP = ““;
        // 主机名称
        string strHostName = ““;
        // 连接状态
        bool opc_connected = false;
        // 客户端句柄
        int itmHandleClient = 0;
        //服务器句柄
        int itmHandleServer = 0;
        #endregion


        #region 方法
        //枚举本地OPC服务器
        private void GetLocalServer()
        {
            //获取本地计算机IP,名称
            IPHostEntry IPHost = Dns.GetHostEntry(Environment.MachineName);     //将主机名或ip地址解析为IPhostentry实例
            if (IPHost.AddressList.Length > 0)
            {
                strHostIP = IPHost.AddressList[0].ToString();
            }
            else
            {
                return;
            }
            //通过IP来获取计算机名称
            IPHostEntry ipHostEntry = Dns.GetHostByAddress(strHostIP);
            strHostName = ipHostEntry.HostName.ToString();
            //获取本地计算机上的OPCServerName
            try
            {
                KepServer = new OPCServer();     //创建一个opcserver对象,初始化
                object serverList = KepServer.GetOPCServers(strHostName);

                foreach (string turn in (Array)serverList)
                {
                    comboBox1.Items.Add(turn);//即服务器combobox1框内显示的是opcserver的名称
                }
                comboBox1.SelectedIndex = 0;
                button1.Enabled = true;         //即连接按钮
            }
            catch (Exception err)
            {
                MessageBox.Show(“枚举本地OPC服务器出错:“ + err.Message “提示信息“ MessageBoxButtons.OK MessageBoxIcon.Warning);
                //提示框样式
            }
        }
        //创建组
        private bool CreatGroup()
        {
            try
            {
                KepGroups = KepServer.OPCGroups;
                KepGroup = KepGroups.Add(“OPCDOTNETGROUP“);
                SetGroupProperty();
                KepGroup.DataChange += new DIOPCGroupEvent_DataChangeEventHandler(KepGroup_DataChange);
                KepGroup.AsyncWriteComplete += new DIOPCGroupEvent_AsyncWriteCompleteEventHandler(KepGroup_AsyncWriteComplete);
                KepItems = KepGroup.OPCItems;
            }
            catch (Exception ex)
            {
                MessageBox.Show(“创建组出现错误:“ + ex.

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2016-10-26 20:01  OPCSet\
     文件       18441  2016-10-26 10:02  OPCSet\dictionary.docx
     目录           0  2016-10-21 10:58  OPCSet\OPCSet\
     文件         908  2016-10-21 10:58  OPCSet\OPCSet.sln
     文件       36352  2016-10-29 11:05  OPCSet\OPCSet.v11.suo
     文件         187  2016-10-21 10:58  OPCSet\OPCSet\App.config
     目录           0  2016-10-21 10:58  OPCSet\OPCSet\bin\
     目录           0  2016-10-21 17:11  OPCSet\OPCSet\bin\Debug\
     文件       24576  2016-10-27 14:12  OPCSet\OPCSet\bin\Debug\OPCSet.exe
     文件         187  2016-10-21 10:58  OPCSet\OPCSet\bin\Debug\OPCSet.exe.config
     文件       32256  2016-10-27 14:12  OPCSet\OPCSet\bin\Debug\OPCSet.pdb
     文件       22984  2016-10-29 10:27  OPCSet\OPCSet\bin\Debug\OPCSet.vshost.exe
     文件         187  2016-10-21 10:58  OPCSet\OPCSet\bin\Debug\OPCSet.vshost.exe.config
     文件         490  2013-03-18 17:00  OPCSet\OPCSet\bin\Debug\OPCSet.vshost.exe.manifest
     文件       11013  2016-10-26 09:47  OPCSet\OPCSet\Form1.cs
     文件       18463  2016-10-21 18:45  OPCSet\OPCSet\Form1.Designer.cs
     文件        6190  2016-10-21 18:45  OPCSet\OPCSet\Form1.resx
     目录           0  2016-10-21 10:58  OPCSet\OPCSet\obj\
     目录           0  2016-10-27 14:12  OPCSet\OPCSet\obj\Debug\
     文件        1308  2016-10-21 19:14  OPCSet\OPCSet\obj\Debug\DesignTimeResolveAssemblyReferences.cache
     文件        6929  2016-10-21 11:36  OPCSet\OPCSet\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache
     文件       28672  2016-10-21 11:36  OPCSet\OPCSet\obj\Debug\Interop.OPCAutomation.dll
     文件         578  2016-10-29 10:27  OPCSet\OPCSet\obj\Debug\OPCSet.csproj.FileListAbsolute.txt
     文件         975  2016-10-21 19:24  OPCSet\OPCSet\obj\Debug\OPCSet.csproj.GenerateResource.Cache
     文件         735  2016-10-21 11:36  OPCSet\OPCSet\obj\Debug\OPCSet.csproj.ResolveComReference.cache
     文件        2209  2016-10-21 17:11  OPCSet\OPCSet\obj\Debug\OPCSet.csprojResolveAssemblyReference.cache
     文件       24576  2016-10-27 14:12  OPCSet\OPCSet\obj\Debug\OPCSet.exe
     文件         180  2016-10-21 19:24  OPCSet\OPCSet\obj\Debug\OPCSet.Form1.resources
     文件       32256  2016-10-27 14:12  OPCSet\OPCSet\obj\Debug\OPCSet.pdb
     文件         180  2016-10-21 17:11  OPCSet\OPCSet\obj\Debug\OPCSet.Properties.Resources.resources
     文件           0  2016-10-21 10:58  OPCSet\OPCSet\obj\Debug\TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs
............此处省略13个文件信息

评论

共有 条评论