• OPCServer Modbus使用和配置


    一,安装KEPware.Enhanced.OPC.DDE.KEPServer。(PLC数据传送给KEPServer,开发的程序用OPCServer读KEPServer)

         

       

        设置ip地址后面是指的plc站号,此处必须和plc上站号对应。否则无法接收数据。

       

          打开quick client 查看传值情况

         

    二.C#程序代码

       引用opcdaauto.dll

       在win7 64位下KepServer = new OPCServer()会出错的问题,

       检索COM 类工厂中 CLSID 为 <28e68f9a-8d75-11d1-8dc3-3c302a000000> 的组件时失败,原因是出现以下错误: 80040154

      1.一定要到C:WindowsSysWOW64路径下打开cmd.exe运行命令文件(而不是在运行打开),注册“OPCDAAuto.dll” 输入命令: REGSVR32 OPCDAAuto.dll;即可

      2.编译工具目标要选择x86

       KepServer.Connect("KEPware.KEPServerEx.V4", "127.0.0.1");  此处ip地址是安装KEPware.Enhanced.OPC.DDE.KEPServer电脑的ip地址.

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using OPCAutomation;
    using System.Threading;
    
    
    namespace SPCTest
    {
        public partial class Form2 : Form
        {
    
    
            private OPCServer KepServer;
            private OPCGroups KepGroups;
            private OPCGroup KepGroup;
    
            private OPCItems KepItems;
            //轴承净重,注脂量,防尘盖压入深度,防尘盖平整度   压入OK/NG
            private OPCItem item1, item2, item3;
    
            public Form2()
            {
                InitializeComponent();
            }
    
    
            #region OPC Server
            private void KepServerLoad()
            {
                try
                {
                    KepServer = new OPCServer();
                    KepServer.Connect("KEPware.KEPServerEx.V4", "127.0.0.1");
                    if (KepServer.ServerState == (int)OPCServerState.OPCRunning)
                    {
                       // statusLabelConnectInfo.Text = "OPC Server连接成功";
                    }
                    else
                    {
                       // statusLabelConnectInfo.Text = "OPC Server连接失败";
                        return;
                    }
                }
                catch (Exception ex)
                {
    
                   // statusLabelConnectInfo.Text = "OPC Server连接失败," + ex.Message;
                    return;
                }
    
                KepGroups = KepServer.OPCGroups;
                Thread t1; // 开1个线程用于读取数据                 
                t1 = new Thread(new ThreadStart(KepProcess));
                t1.Start();
    
            }
    
            public void KepProcess()
            {
                //KepGroup = KepGroups.Add("Channel.Device.Group");
                KepGroup = KepGroups.Add("Channel1.Device1.Group");
                KepGroup.UpdateRate = 250;
                KepGroup.IsActive = true;
                KepGroup.IsSubscribed = true;
                //当KepGroup中数据发生改变的触发事件    
                KepGroup.DataChange += new DIOPCGroupEvent_DataChangeEventHandler(KepGroup_DataChange);
                KepItems = KepGroup.OPCItems;
                item1 = KepItems.AddItem("Channel1.Device1.A01", 1);
                item2 = KepItems.AddItem("Channel1.Device1.A02", 2);
                item3 = KepItems.AddItem("Channel1.Device1.A03", 3);
    
    
            }
          
            //当数据改变时触发的事件 
            public delegate void DelegateShowMessage(string str);
            
            public void KepGroup_DataChange(int TransactionID, int NumItems, ref Array ClientHandles, ref Array ItemValues, ref Array Qualities, ref Array TimeStamps)
            {
                string str = "";
                DelegateShowMessage show1 = new DelegateShowMessage(ShowMessage);
                for (int i = 1; i <= NumItems; i++)
                {
                    if (ClientHandles.GetValue(i).Equals(1))
                    {
                        str = "A01:" + ItemValues.GetValue(i).ToString();
                    }
    
                    if (ClientHandles.GetValue(i).Equals(2))
                    {
                        str = "A02:" + ItemValues.GetValue(i).ToString();
                    }
    
                    if (ClientHandles.GetValue(i).Equals(3))
                    {
                        str = "A03:" + ItemValues.GetValue(i).ToString();
    
                    }
                 
                    BeginInvoke(show1, new string[] { str });
                }
    
             
              
    
            }
    
        
         
            public void ShowMessage(string str)
            {
                
                richTextBox1.AppendText(str+",");
              
            }
    
        
            #endregion
    
            private void Form2_Load(object sender, EventArgs e)
            {
                KepServerLoad();
            }
    
            private void Form2_FormClosing(object sender, FormClosingEventArgs e)
            {
                KepServer.Disconnect();
            }
    
        }
    }
  • 相关阅读:
    [Re] SpringMVC-5(Converter+拦截器+国际化+异常处理)
    [Re] SpringMVC-4(数据绑定+数据格式化+数据校验)
    [Re] SpringMVC-3(视图解析+RESTful CRUD)
    [Re] SpringMVC-2(数据输出+源码流程)
    常用SQL优化
    JAVA 中的标准注解
    linux第一发
    easyui js取消选中 Tree 指定节点
    axis1.4 服务端获取请求报文报文和客户端获取请求报文和响应报文
    对于两个实体类属性值的合并,java实现
  • 原文地址:https://www.cnblogs.com/ike_li/p/5468658.html
Copyright © 2020-2023  润新知