1.界面
image.png
2.源碼
using OPCAutomation;
using System;
using System.Drawing;
using System.Windows.Forms;
namespace KepserverTest
{
/*
1.連接KepServer服務(wù)器:OPCServer.Connect(string ProgID, object Node);
2.配置通道:OPCGroup OPCServer.OPCGroups.Add(chanel);
3.配置item:OPCItem OPCItems.AddItem(string ItemID, int ClientHandle);
(1)讀:OPCItem.Read(short Source, out object Value, out object Quality, out object TimeStamp);
(2)寫:OPCItem.Write(object Value);
*/
public partial class Form1 : Form
{
string def_chanel = "通道 3";
string def_item = "通道 3.設(shè)備 1.M1";
string remoteServerName, remoteServerIP;//服務(wù)器明和服務(wù)器IP地址
OPCServer KepServer;
OPCGroup KepGroup;
OPCItem KepItem1;//動(dòng)態(tài)數(shù)據(jù)
OPCItem KepItem;//讀取寫入
int[] ServerHandle = new int[2];
bool isStart = false;
/*初始化*/
public Form1()
{
InitializeComponent();
KepServer = new OPCServer();
}
/*連接按鈕*/
private void button1_Click(object sender, EventArgs e)
{
//獲取服務(wù)器IP地址和服務(wù)器名
remoteServerIP = this.tb_server_ip.Text;
remoteServerName = this.tb_server_name.Text;
//連接服務(wù)器
ConnectRemoteServer(remoteServerIP, remoteServerName);
}
/*斷開按鈕*/
private void button2_Click(object sender, EventArgs e)
{
//斷開連接
DisConnectRemoteServer();
}
/*讀取按鈕*/
private void btn_read_syn_Click(object sender, EventArgs e)
{
object ItemValues;
object Qualities;
object TimeStamps;
try
{
//準(zhǔn)備ID信息
string chanel = this.tb_chanel.Text;
string device = this.tb_device.Text;
string key = this.tb_mark.Text;
//SetGroup(chanel);
KepItem = KepGroup.OPCItems.AddItem(chanel+"."+device + "." +key, 1);//創(chuàng)建Item
//KepItem = KepGroup.OPCItems.GetOPCItem(ServerHandle[0]);
//ServerHandle[1] = KepItem.ServerHandle;//???
System.Threading.Thread.Sleep(500);//服務(wù)器掃描速率為100ms
//讀取數(shù)據(jù),并輸出
KepItem.Read(1, out ItemValues, out Qualities, out TimeStamps);
//顯示數(shù)據(jù)
this.listBox2.Items.Insert(0,
"[Value] " + string.Format("{0}", KepItem.Value) + " " +
"[Quality] " + string.Format("{0}", KepItem.Quality) + " " +
"[TimeStamp] " + string.Format("{0}", KepItem.TimeStamp)
);
//輸出日志
Log("【信息】讀取"+KepItem.ItemID);
}
catch (System.Exception ex)
{
Log("【異常】" + ex.Message);
}
}
/*寫入按鈕*/
private void btn_write_Click_1(object sender, EventArgs e)
{
try
{
string chanel = this.tb_chanel.Text;
string device = this.tb_device.Text;
string mark = this.tb_mark.Text;
int value = int.Parse(this.tb_value_write.Text);
KepItem = KepGroup.OPCItems.AddItem(chanel + "." + device + "." + mark, 1);
//ServerHandle[1] = KepItem.ServerHandle;
System.Threading.Thread.Sleep(200);
KepItem.Write(value);
Log("【信息】寫入"+KepItem.ItemID);
}
catch (System.Exception error)
{
Log("【異常】" + error.Message);
}
}
/*啟動(dòng)/關(guān)閉*/
private void btn_power_Click(object sender, EventArgs e)
{
try
{
string chanel = this.tb_chanel.Text;
string device = this.tb_device.Text;
string mark = this.tb_mark.Text;
bool value = !isStart;
KepItem = KepGroup.OPCItems.AddItem(chanel + "." + device + "." + mark, 1);
//ServerHandle[1] = KepItem.ServerHandle;
System.Threading.Thread.Sleep(200);
KepItem.Write(value);
this.btn_power.Text = isStart ? "啟動(dòng)" : "關(guān)閉";
Log("【信息】寫入" + KepItem.ItemID+" : "+isStart);
}
catch (System.Exception error)
{
Log("【異常】" + error.Message);
}
}
/*動(dòng)態(tài)讀取按鈕*/
private void btn_read_dyn_Click(object sender, EventArgs e)
{
KepItem1 = KepGroup.OPCItems.AddItem(def_item, 1);
//綁定委托:數(shù)據(jù)改變
KepGroup.DataChange += new DIOPCGroupEvent_DataChangeEventHandler(MyOpcGroup_DataChange);
}
/*連接服務(wù)器*/
private bool ConnectRemoteServer(string remoteServerIP, string remoteServerName)
{
string status;
try
{
this.KepServer.Connect(remoteServerName, remoteServerIP);
if (KepServer.ServerState == (int)OPCServerState.OPCRunning)
{
status = "【連接】" + KepServer.ServerName + " ";
}
else
{
//這里你可以根據(jù)返回的狀態(tài)來自定義顯示信息,請(qǐng)查看自動(dòng)化接口API文檔
status = "【狀態(tài)】" + KepServer.ServerState.ToString() + " ";
}
}
catch (Exception ex)
{
Log("【異常】" + "KepServer"+ex.Message);
return false;
}
//設(shè)置組
try
{
//添加通道
KepGroup = KepServer.OPCGroups.Add(def_chanel);//通道標(biāo)識(shí)
//設(shè)置通道
KepGroup.IsActive = true;
KepGroup.IsSubscribed = true;
KepGroup.DeadBand = 0;
KepGroup.UpdateRate = 1000;//更新速率
//初始化item1:ModbusSlave測(cè)試(通道)>ModbusSlave(從機(jī))>K1(標(biāo)記)
//KepItem1 = KepGroup.OPCItems.AddItem(def_item, 1);
//ServerHandle[0] = KepItem1.ServerHandle;//???
//綁定委托:完成異步寫入
KepGroup.AsyncWriteComplete += new DIOPCGroupEvent_AsyncWriteCompleteEventHandler(MyOpcGroup_AsyncWriteComplete);
//綁定委托:完成異步讀取
KepGroup.AsyncReadComplete += new DIOPCGroupEvent_AsyncReadCompleteEventHandler(MyOpcGroup_AsyncReadComplete);
//綁定委托:???
KepGroup.AsyncCancelComplete += new DIOPCGroupEvent_AsyncCancelCompleteEventHandler(MyOpcGroup_AsyncCancelComplete);
//綁定委托:數(shù)據(jù)改變
//KepGroup.DataChange += new DIOPCGroupEvent_DataChangeEventHandler(MyOpcGroup_DataChange);
}
catch (System.Exception ex)
{
Log("【異常】" + "KepGroup," + ex.Message);
return false;
}
Log(status);
return true;
}
/*斷開連接*/
private void DisConnectRemoteServer()
{
try
{
this.KepServer.Disconnect();
Log( "【斷開】" + remoteServerIP);
}
catch(Exception ex)
{
Log("【異常】" + ex.Message);
}
}
void MyOpcGroup_AsyncCancelComplete(int CancelID)
{
throw new NotImplementedException();
}
void MyOpcGroup_AsyncReadComplete(int TransactionID, int NumItems, ref Array ClientHandles, ref Array ItemValues, ref Array Qualities, ref Array TimeStamps, ref Array Errors)
{
throw new NotImplementedException();
}
void MyOpcGroup_AsyncWriteComplete(int TransactionID, int NumItems, ref Array ClientHandles, ref Array Errors)
{
throw new NotImplementedException();
}
/*DataChange事件*/
void MyOpcGroup_DataChange(int TransactionID, int NumItems, ref Array ClientHandles, ref Array ItemValues, ref Array Qualities, ref Array TimeStamps)
{
try
{
this.listBox2.Items.Clear();
this.listBox2.Items.Insert(0,
"[Value] " + string.Format("{0}", KepItem1.Value) + " " +
"[Quality] " + string.Format("{0}", KepItem1.Quality) + " " +
"[TimeStamp] " + string.Format("{0}", KepItem1.TimeStamp)
);
}
catch (System.Exception ex)
{
Log("【異常】" + ex.Message);
}
}
#region 管理數(shù)據(jù)顯示
private void lb3_clear_all_Click(object sender, EventArgs e)
{
listBox3.Items.Clear();
}
private void listBox3_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
ms_lb3.Show(listBox3, new Point(e.X, e.Y));
}
}
private void listBox2_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
ms_lb2.Show(listBox2, new Point(e.X, e.Y));
}
}
private void lb2_clear_all_Click(object sender, EventArgs e)
{
listBox2.Items.Clear();
}
void Log(string info)
{
this.listBox3.Items.Insert(0, "[" + DateTime.Now.ToString() + "] " + info);
}
#endregion
}
}