OPCServer  Modbus使用和配置
                        
                            时间:2021-07-01 10:21:17
                            帮助过:2人阅读
							                        
                     
                    
                    
                     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();
        }
    }
}
 
OPCServer  Modbus使用和配置
标签: