• 大小: 257KB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2021-06-17
  • 语言: C#
  • 标签: C#  OPC  automation  

资源简介

c#编写的 OPC 客户端 读取程序,将读取值存入SQLserver。

资源截图

代码片段和文件信息

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 OPC_Client.Methods;//添加引用
using OPCAutomation;


namespace OPC_Client
{
    public partial class Frm_Main : Form
    {
        public Frm_Main()
        {
            InitializeComponent();
        }

        #region 声明变量与定义
        DataTable dt = new DataTable();
        IpRead ipread = new IpRead();//IP 操作类
        SqlHelper sqlhp = new SqlHelper();//数据库操作
        string LocalIP = ““;//IP地址
        OPCServer KepServer;
        OPCGroups KepGroups;
        OPCGroup KepGroup;
        OPCItems KepItems;
        OPCItem KepItem;
        int itmHandleClient = 0;//客户端句柄
        int itmHandleServer = 0;//服务端句柄
        bool opc_connected = false;//opc连接状态
        #endregion

        private void Frm_Main_Load(object sender EventArgs e)
        {
            LocalIP = txt_LocalIP.Text = ipread.RetrunIP();//获取本机IP地址
            GetLocalServer();//枚举本地OPC服务器
            dt.Columns.Add(“标签“ System.Type.GetType(“System.String“));
            dt.Columns.Add(“测量值“ System.Type.GetType(“System.String“));
            dt.Columns.Add(“质量“ System.Type.GetType(“System.String“));
            dt.Columns.Add(“时间戳“ System.Type.GetType(“System.String“));
            dgv_dataShow.DataSource = dt;
        }

        private void GetLocalServer()
        {
            //检查IP地址是否合法
            if (System.Text.Regularexpressions.Regex.IsMatch(LocalIP “[0-9]{13}\\.[0-9]{13}\\.[0-9]{13}\\.[0-9]{13}“))
            {
                string CoputerName = Dns.GetHostByAddress(LocalIP).HostName.ToString();
                try
                {
                    KepServer = new OPCServer();
                    object serverList = KepServer.GetOPCServers(CoputerName);
                    foreach (string turn in (Array)serverList)
                    {
                        cmb_OpcServer.Items.Add(turn);
                    }
                    cmb_OpcServer.SelectedIndex = 0;
                    Btn_ServerConnect.Enabled = true;
                }
                catch (Exception err)
                {
                    MessageBox.Show(“枚举本地OPC服务器出错:“ + err.Message “提示信息“ MessageBoxButtons.OK MessageBoxIcon.Warning);
                }
            }
            else { MessageBox.Show(“IP地址不合法,请检查!!!“ “友情提示“); }
        }

        /// 
        /// 【按钮】连接服务器
        /// 

        /// 
        /// 
        private void Btn_ServerConnect_Click(object sender EventArgs e)
        {
            try
            {
                KepServer.Connect(cmb_OpcServer.Text txt_LocalIP.Text);
                if (KepServer.ServerState == (int)OPCServerState.OPCRunning)
                {
       

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     文件        3021  2016-07-25 10:58  51Aspx源码必读.txt
     文件         187  2016-07-20 09:01  App.config
     目录           0  2016-07-25 10:51  DB_51aspx\
     文件     3145728  2016-07-25 10:58  DB_51aspx\OPC_Test.mdf
     文件     1048576  2016-07-25 10:58  DB_51aspx\OPC_Test_log.ldf
     文件       22960  2016-07-20 14:49  Frm_Main.Designer.cs
     文件       11138  2016-07-20 14:51  Frm_Main.cs
     文件       12549  2016-07-20 14:49  Frm_Main.resx
     目录           0  2016-07-25 10:51  Methods\
     文件        1031  2016-07-20 09:16  Methods\IpRead.cs
     文件        2051  2016-07-25 10:54  Methods\SqlHelper.cs
     文件        4288  2016-07-20 14:21  OPC Client.csproj
     文件         909  2016-07-25 10:53  OPC Client.sln
     文件       26112  2016-07-25 10:53  OPC Client.v11.suo
     文件         525  2016-07-20 09:01  Program.cs
     目录           0  2016-07-25 10:51  Properties\
     文件        1352  2016-07-20 09:00  Properties\AssemblyInfo.cs
     文件        2872  2016-07-20 09:00  Properties\Resources.Designer.cs
     文件        5612  2016-07-20 09:00  Properties\Resources.resx
     文件        1097  2016-07-20 09:00  Properties\Settings.Designer.cs
     文件         249  2016-07-20 09:00  Properties\Settings.settings
     目录           0  2016-07-25 10:51  bin\
     目录           0  2016-07-25 10:54  bin\Debug\
     文件       40960  2009-01-08 16:11  bin\Debug\Interop.OPCAutomation.dll
     文件       33792  2016-07-25 10:54  bin\Debug\OPC Client.exe
     文件         187  2016-07-20 09:01  bin\Debug\OPC Client.exe.config
     文件       38400  2016-07-25 10:54  bin\Debug\OPC Client.pdb
     文件       22984  2016-07-25 10:56  bin\Debug\OPC Client.vshost.exe
     文件         187  2016-07-20 09:01  bin\Debug\OPC Client.vshost.exe.config
     文件         490  2012-06-02 22:34  bin\Debug\OPC Client.vshost.exe.manifest
     目录           0  2016-07-25 10:58  bin\Release\
............此处省略16个文件信息

评论

共有 条评论