您当前的位置: 首页 >  c#

txwtech

暂无认证

  • 5浏览

    0关注

    813博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

c#kepserver连接操作方法代码

txwtech 发布时间:2021-10-23 20:42:22 ,浏览量:5

c#kepserver连接操作方法代码

c#连接kepServer操作kepware源码.rar-C#文档类资源-CSDN下载解压密码:blog.csdn.net/txwtech更多下载资源、学习资料请访问CSDN下载频道.https://download.csdn.net/download/txwtech/33508026

解压密码:blog.csdn.net/txwtech 

Form1.cs

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;

namespace c_sharp_kepServer_demo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        List wms_s = new List();
        private void button_connect_Click(object sender, EventArgs e)
        {
            OpcUtil opcUtil = new OpcUtil();
            Wms_OpcTagListModel wms_OpcTagListModel = new Wms_OpcTagListModel();
            wms_OpcTagListModel.TagName = "Device1.Allow";
            wms_s.Add(wms_OpcTagListModel);
            OpcUtil.initOPC(wms_s);
        }

        private void button_read_Click(object sender, EventArgs e)
        {
            if(OpcUtil.InitOK==false)
            {
                MessageBox.Show("OPC kepserver连接失败,请确保kepware服务已经启动,点击连接OPC按钮");
                return;
            }
            textBox_tagContent.Text=OpcUtil.OpcRead(textBox_tagname.Text);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (OpcUtil.InitOK == false)
            {
                MessageBox.Show("OPC kepserver连接失败,请确保kepware服务已经启动,点击连接OPC按钮");
                return;
            }
            OpcUtil.OpcWrite(textBox_tagname.Text,textBox_tagContent.Text);
        }
    }
}

OpcUtil.cs 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UcAsp.Opc.Da;
using System.Threading;
using UcAsp.Opc;
using c_sharp_kepServer_demo.PO;
using System.Windows.Forms;
//using MechineOper.CommUnication.Utils;
namespace c_sharp_kepServer_demo
{
    class OpcUtil
    {
        public static DaClient OpcLink;
        public static bool InitOK = false;
       
        public static void initOPC(List taglist)
        {
            InitOK = false;
            if (OpcLink != null)
            {
                OpcLink.Dispose();
            }
            OpcLink = new DaClient(new Uri("opcda://127.0.0.1/KEPware.KEPServerEx.V6"));
          //  Opc.Server[] servers = m_discovery.GetAvailableServers(Specification.COM_DA_20,“OPC服务器IP地址”,null);
            //opcda://127.0.0.1/Matrikon.OPC.Simulation.1
            OpcLink.Options.DefaultMonitorInterval = 1000;
            OpcLink.Options.MaxPublishRequestCount = int.MaxValue;
            OpcLink.Options.MaxNotificationQueueSize = int.MaxValue;
            //OpcLink.Options.SessionTimeout = uint.MaxValue;
            OpcLink.Options.MaxMessageQueueSize = int.MaxValue;
            OpcLink.Options.MaxSubscriptionCount = int.MaxValue;
            //OpcLink.Options.SubscriptionLifetimeCount = uint.MaxValue;
            //OpcLink.Options.SubscriptionKeepAliveCount= int.MaxValue;
            try
            {
                OpcLink.Connect();
            }
            catch(Opc.ConnectFailedException e)
            {
                MessageBox.Show(e.Data.ToString()+e.Message,"连接失败,确保kepware服务已经启动哦。。。",MessageBoxButtons.OK,MessageBoxIcon.Error);
                return;
            }
            while (OpcLink.Status != OpcStatus.Connected)
            {
                Thread.Sleep(5);
                Console.WriteLine("连接失败");
            }
            if (OpcLink.Status == OpcStatus.Connected)
            {
                Thread.Sleep(5);
                Console.WriteLine("连接成功");
            }
           // OpcGroup group = OpcLink.AddGroup("opc");

           // OpcLink.AddItems("opc", new string[] {"PLC.Device1.Start", "PLC.Device1.Allow" });
          //  group.DataChange += new OpcObeser().Group_DataChange;
            InitOK = true;

            string get_data = OpcLink.Read("通道1.设备1.Tag1");
            Console.WriteLine("PLC tag1 data: "+get_data);
            OpcLink.Write("通道1.设备1.Tag1", "22");
            Console.WriteLine("PLC write tag1 data");
            Thread.Sleep(2000);
            string get_data2 = OpcLink.Read("通道1.设备1.Tag1");
            Console.WriteLine("changed PLC tag1 data: " + get_data2);


        }
        public static string OpcRead(string read_tag_name)
        {
           
            string get_data = OpcLink.Read(read_tag_name);
            Console.WriteLine("PLC tag1 data: " + get_data);
            return get_data;
        }
        public static void OpcWrite(string write_tag_name,string write_content)
        {
            OpcLink.Write(write_tag_name, write_content);
            Console.WriteLine("PLC write tag1 data");
        }
        public static void  Init()
        {
          
           
            //items[2] = new Opc.Da.Item();

            //items[2].ItemName = "PLC1.dyn_I4[1]";

        }

        public static void InitMap(string tagname)
        {
            OpcItemValue value = new OpcItemValue();
            object curValue = (short)0;
            value.Value = curValue;
            value.Quality = "bad";
            CONFConst.TAG_MAP[tagname] = value;
          //  Console.WriteLine(value);
            //侧移机对象TAG属性映射
            String[] Code = tagname.Split('.');
          
            if (Code[1] == "Allow")
            {
                CONFConst.CT_MAP.AddOrUpdate(Code[1], new PLC() { MM = new Tag() { Value = (short)curValue, TagPath = tagname, Qunity = "" == "good" ? true : false } }, (S, T) => { T.MM.Value = (short)curValue; T.MM.TagPath = tagname; T.MM.Qunity = "" == "good" ? true : false; return T; });
                Console.WriteLine("dddddddddddd");
            }
           
        }

    }
}

关注
打赏
1665060526
查看更多评论
立即登录/注册

微信扫码登录

0.0957s