php opc数据,OPC连接获取数据
【實例簡介】
【實例截圖】
【核心代碼】
///摘要
///程式使用C#.NET 2005 編寫
///引用類庫OPCDAAuto.dll
///OPCServer采用KEPWare
///在windows xp sp2、sp3、windows 2003上測試通過
///完成于:2008年12月31日
///測試于:2009年01月05日
///
///作者:瀟灑草
///Email:zhkai868@163.com
///QQ:44649029
///
///如分發,請保留此摘要。
///鄙視那些拿代碼當寶貝的人,鄙視那些拿源碼換源碼的人,鄙視那些自私的人。
///別人看到你的代碼,你能死啊?對你有多大威脅啊?強烈鄙視~~~
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Collections;
using OPCAutomation;
namespace OPC測試通過
{
public partial class MainFrom : Form
{
public MainFrom()
{
InitializeComponent();
}
#region 私有變量
///
/// OPCServer Object
///
OPCServer KepServer;
///
/// OPCGroups Object
///
OPCGroups KepGroups;
///
/// OPCGroup Object
///
OPCGroup KepGroup;
///
/// OPCItems Object
///
OPCItems KepItems;
///
/// OPCItem Object
///
OPCItem KepItem;
///
/// 主機IP
///
string strHostIP = "";
///
/// 主機名稱
///
string strHostName = "";
///
/// 連接狀態
///
bool opc_connected = false;
///
/// 客戶端句柄
///
int itmHandleClient = 0;
///
/// 服務端句柄
///
int itmHandleServer = 0;
#endregion
#region 方法
///
/// 枚舉本地OPC服務器
///
private void GetLocalServer()
{
//獲取本地計算機IP,計算機名稱
IPHostEntry IPHost = Dns.Resolve(Environment.MachineName);
if (IPHost.AddressList.Length > 0)
{
strHostIP = IPHost.AddressList[0].ToString();
}
else
{
return;
}
//通過IP來獲取計算機名稱,可用在局域網內
IPHostEntry ipHostEntry = Dns.GetHostByAddress(strHostIP);
strHostName=ipHostEntry.HostName.ToString();
//獲取本地計算機上的OPCServerName
try
{
KepServer = new OPCServer();
object serverList = KepServer.GetOPCServers(strHostName);
foreach (string turn in (Array)serverList)
{
cmbServerName.Items.Add(turn);
}
cmbServerName.SelectedIndex = 0;
btnConnServer.Enabled = true;
}
catch(Exception err)
{
MessageBox.Show("枚舉本地OPC服務器出錯:" err.Message,"提示信息",MessageBoxButtons.OK,MessageBoxIcon.Warning);
}
}
///
/// 創建組
///
private bool CreateGroup()
{
try
{
KepGroups = KepServer.OPCGroups;
KepGroup = KepGroups.Add("OPCDOTNETGROUP");
SetGroupProperty();
KepGroup.DataChange = new DIOPCGroupEvent_DataChangeEventHandler(KepGroup_DataChange);
KepGroup.AsyncWriteComplete = new DIOPCGroupEvent_AsyncWriteCompleteEventHandler(KepGroup_AsyncWriteComplete);
KepItems = KepGroup.OPCItems;
}
catch (Exception err)
{
MessageBox.Show("創建組出現錯誤:" err.Message,"提示信息",MessageBoxButtons.OK,MessageBoxIcon.Warning);
return false;
}
return true;
}
///
/// 設置組屬性
///
private void SetGroupProperty()
{
KepServer.OPCGroups.DefaultGroupIsActive =Convert.ToBoolean(txtGroupIsActive.Text);
KepServer.OPCGroups.DefaultGroupDeadband = Convert.ToInt32(txtGroupDeadband.Text);
KepGroup.UpdateRate = Convert.ToInt32(txtUpdateRate.Text);
KepGroup.IsActive = Convert.ToBoolean(txtIsActive.Text);
KepGroup.IsSubscribed =Convert.ToBoolean(txtIsSubscribed.Text);
}
///
/// 列出OPC服務器中所有節點
///
///
private void RecurBrowse(OPCBrowser oPCBrowser)
{
//展開分支
oPCBrowser.ShowBranches();
//展開葉子
oPCBrowser.ShowLeafs(true);
foreach (object turn in oPCBrowser)
{
listBox1.Items.Add(turn.ToString());
}
}
///
/// 獲取服務器信息,并顯示在窗體狀態欄上
///
private void GetServerInfo()
{
tsslServerStartTime.Text ="開始時間:" KepServer.StartTime.ToString() " ";
tsslversion.Text ="版本:" KepServer.MajorVersion.ToString() "." KepServer.MinorVersion.ToString() "." KepServer.BuildNumber.ToString();
}
///
/// 連接OPC服務器
///
/// OPCServerIP
/// OPCServer名稱
private bool ConnectRemoteServer(string remoteServerIP, string remoteServerName)
{
try
{
KepServer.Connect(remoteServerName, remoteServerIP);
if (KepServer.ServerState == (int)OPCServerState.OPCRunning)
{
tsslServerState.Text = "已連接到-" KepServer.ServerName " ";
}
else
{
//這里你可以根據返回的狀態來自定義顯示信息,請查看自動化接口API文檔
tsslServerState.Text = "狀態:" KepServer.ServerState.ToString() " ";
}
}
catch (Exception err)
{
MessageBox.Show("連接遠程服務器出現錯誤:" err.Message, "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return false;
}
return true;
}
#endregion
#region 事件
///
/// 寫入TAG值時執行的事件
///
///
///
///
///
void KepGroup_AsyncWriteComplete(int TransactionID, int NumItems, ref Array ClientHandles, ref Array Errors)
{
lblState.Text = "";
for (int i = 1; i <= NumItems; i )
{
lblState.Text = "Tran:" TransactionID.ToString() " CH:" ClientHandles.GetValue(i).ToString() " Error:" Errors.GetValue(i).ToString();
}
}
///
/// 每當項數據有變化時執行的事件
///
/// 處理ID
/// 項個數
/// 項客戶端句柄
/// TAG值
/// 品質
/// 時間戳
void KepGroup_DataChange(int TransactionID, int NumItems, ref Array ClientHandles, ref Array ItemValues, ref Array Qualities, ref Array TimeStamps)
{
//為了測試,所以加了控制臺的輸出,來查看事物ID號
//Console.WriteLine("********" TransactionID.ToString() "*********");
for (int i = 1; i <= NumItems; i )
{
this.txtTagValue.Text = ItemValues.GetValue(i).ToString();
this.txtQualities.Text = Qualities.GetValue(i).ToString();
this.txtTimeStamps.Text = TimeStamps.GetValue(i).ToString();
}
}
///
/// 選擇列表項時處理的事情
///
///
///
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
if (itmHandleClient != 0)
{
this.txtTagValue.Text = "";
this.txtQualities.Text ="";
this.txtTimeStamps.Text = "";
Array Errors;
OPCItem bItem = KepItems.GetOPCItem(itmHandleServer);
//注:OPC中以1為數組的基數
int[] temp = new int[2] { 0, bItem.ServerHandle };
Array serverHandle = (Array)temp;
//移除上一次選擇的項
KepItems.Remove(KepItems.Count, ref serverHandle, out Errors);
}
itmHandleClient = 1234;
KepItem = KepItems.AddItem(listBox1.SelectedItem.ToString(), itmHandleClient);
itmHandleServer = KepItem.ServerHandle;
}
catch(Exception err)
{
//沒有任何權限的項,都是OPC服務器保留的系統項,此處可不做處理。
itmHandleClient = 0;
txtTagValue.Text = "Error ox";
txtQualities.Text = "Error ox";
txtTimeStamps.Text = "Error ox";
MessageBox.Show("此項為系統保留項:" err.Message,"提示信息");
}
}
///
/// 載入窗體時處理的事情
///
private void MainFrom_Load(object sender, EventArgs e)
{
GetLocalServer();
}
///
/// 關閉窗體時處理的事情
///
private void MainFrom_FormClosing(object sender, FormClosingEventArgs e)
{
if (!opc_connected)
{
return;
}
if (KepGroup != null)
{
KepGroup.DataChange -= new DIOPCGroupEvent_DataChangeEventHandler(KepGroup_DataChange);
}
if (KepServer != null)
{
KepServer.Disconnect();
KepServer = null;
}
opc_connected = false;
}
///
/// 【按鈕】設置
///
private void btnSetGroupPro_Click(object sender, EventArgs e)
{
SetGroupProperty();
}
///
/// 【按鈕】連接OPC服務器
///
private void btnConnLocalServer_Click(object sender, EventArgs e)
{
try
{
if (!ConnectRemoteServer(txtRemoteServerIP.Text,cmbServerName.Text))
{
return;
}
btnSetGroupPro.Enabled = true;
opc_connected = true;
GetServerInfo();
RecurBrowse(KepServer.CreateBrowser());
if (!CreateGroup())
{
return;
}
}
catch (Exception err)
{
MessageBox.Show("初始化出錯:" err.Message, "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
///
/// 【按鈕】寫入
///
private void btnWrite_Click(object sender, EventArgs e)
{
OPCItem bItem = KepItems.GetOPCItem(itmHandleServer);
int[] temp = new int[2] { 0, bItem.ServerHandle };
Array serverHandles=(Array)temp;
object[] valueTemp = new object[2] {"",txtWriteTagValue.Text };
Array values=(Array)valueTemp;
Array Errors;
int cancelID;
KepGroup.AsyncWrite(1,ref serverHandles,ref values,out Errors, 2009,out cancelID);
//KepItem.Write(txtWriteTagValue.Text);//這句也可以寫入,但并不觸發寫入事件
GC.Collect();
}
#endregion
}
}
總結
以上是生活随笔為你收集整理的php opc数据,OPC连接获取数据的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python使用APP Inventor
- 下一篇: 动态规划算法php,php算法学习之动态