using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OPCAutomation;
using System.Net;
using System.IO;
using System.Data;
using System.Threading;
namespace OPCPLC
{
public class OPCPLCAutomation
{
///
/// OPCServer Object
///
public OPCServer KepServer;
///
/// OPCGroups Object
///
public OPCGroups KepGroups;
///
/// OPCGroup Object
///
public OPCGroup KepGroup;
///
/// OPCItems Object
///
public OPCItems KepItems;
///
/// OPCItem Object
///
public OPCItem KepItem;
///
/// 客户端句柄
///
int itmHandleClient = 1234;
///
/// 存放服务端句柄
///
public Dictionary dirHandleServer = new Dictionary();
///
/// 枚举本地OPC服务器
///
public object GetListOPCServers()
{
//获取本地计算机上的OPCServerName
try
{
KepServer = new OPCServer();
object serverList = KepServer.GetOPCServers();
return serverList;
}
catch (Exception ex)
{
throw ex;
}
}
///
/// 枚举本地OPC服务器
///
public object GetListOPCServers(string strHostIP)
{
//获取本地计算机上的OPCServerName
try
{
KepServer = new OPCServer();
object serverList = KepServer.GetOPCServers(strHostIP);
return serverList;
}
catch (Exception ex)
{
throw ex;
}
}
///
/// 连接OPC服务器
///
/// OPCServerIP
/// OPCServer名称
public bool ConnectRemoteServer(string remoteServerName, string remoteServerIP)
{
try
{
KepServer.Connect(remoteServerName, remoteServerIP);
if (KepServer.ServerState == (int)OPCServerState.OPCRunning)
{
return true;
}
else
{
return false;
}
}
catch (Exception ex)
{
return false;
}
}
///
/// 取消连接OPC服务器
///
/// OPCServerIP
/// OPCServer名称
public bool DisconnectConnectRemoteServer()
{
try
{
if (KepServer.ServerState == (int)OPCServerState.OPCRunning && KepServer != null)
{
KepServer.Disconnect();
KepServer = null;
return true;
}
else
{
return false;
}
}
catch (Exception ex)
{
return false;
}
}
///
/// 创建组
///
/// 组的名称
///
public OPCGroup CreateGroup(string groupname)
{
try
{
KepGroups = KepServer.OPCGroups;
KepGroup = KepGroups.Add(groupname);
KepItems = KepGroup.OPCItems;
return KepGroup;
}
catch (Exception err)
{
return null;
}
}
///
/// 移除组
///
/// 组的名称
///
public void RemoveGroup(string groupname)
{
try
{
KepServer.OPCGroups.Remove(groupname);
}
catch (Exception)
{
}
}
///
/// 设置组属性
///
public void SetGroupProperty()
{
KepServer.OPCGroups.DefaultGroupIsActive = true;
KepServer.OPCGroups.DefaultGroupDeadband = 0;
KepGroup.IsActive = true;
KepGroup.IsSubscribed = true;
}
///
/// 向OPC中添加地址项
///
/// 地址项名称
///
public int AddKepItem(string itemname)
{
KepItem = KepGroup.OPCItems.AddItem(itemname, itmHandleClient);
dirHandleServer.Add(itemname, KepItem.ServerHandle);
return KepItem.ServerHandle;
}
///
/// 向OPC中添加地址项
///
/// 地址项名称
/// 客户端数据
///
public int AddKepItem(string itemname, int clientHandleValue)
{
KepItem = KepGroup.OPCItems.AddItem(itemname, clientHandleValue);
dirHandleServer.Add(itemname, KepItem.ServerHandle);
return KepItem.ServerHandle;
}
///
/// 向OPC中添加连续添加多个地址项
///
/// 添加地址的个数
/// 添加连续地址的字符数组
/// 添加连续地址的字符数组句柄
/// 返回连续地址服务句柄
/// 返回错误信息
public void AddKepItems(int NumItems, ref Array ItmeIds, ref Array ClientHandles, out Array ServerHandles, out Array Errors)
{
KepGroup.OPCItems.AddItems(NumItems, ref ItmeIds, ref ClientHandles, out ServerHandles, out Errors);
for (int i = 1; i <= ServerHandles.Length; i++)
{
dirHandleServer.Add(ItmeIds.GetValue(i).ToString(), int.Parse(ServerHandles.GetValue(i).ToString()));
}
}
///
/// 移除地址项
///
///
public void RemoveKepItems()
{
int NumItems = dirHandleServer.Count;
Array Errors;
int[] handle = new int[NumItems + 1];
int index = 0;
handle[index] = 0;
foreach (KeyValuePair kvp in dirHandleServer)
{
index++;
handle[index] = kvp.Value;
}
Array ServerHandle = handle.ToArray();
KepGroup.OPCItems.Remove(NumItems, ref ServerHandle, out Errors);
}
///
/// 读取一个地址项的值
///
/// 地方服务句柄
/// 返回读取的值
public object ReadtagValue(int p_itmHandleServer)
{
int sumValue = 0;
try
{
OPCItem bItem = KepGroup.OPCItems.GetOPCItem(p_itmHandleServer);//KepItems.GetOPCItem( itmHandleServer1 );
int[] temp = new int[2] { 0, bItem.ServerHandle };
Array serverHandles = (Array)temp;
object[] valueTemp = new object[2] { "", "0" };
Array values = (Array)valueTemp;
Array Errors;
object quer;
object tiems;
KepGroup.SyncRead(1, 1, ref serverHandles, out values, out Errors, out quer, out tiems);
GC.Collect();
sumValue = values.Length;
return values.GetValue(1);
}
catch (Exception ex)
{
throw ex;
}
finally
{
}
}
///
/// 释放资源处理的事情
///
public void ReleaseResource()
{
try
{
if (KepServer != null)
{
KepServer.Disconnect();
KepServer = null;
}
}
catch
{
}
}
///
/// 设置数据更新的频率
///
///
public void SetUpdateRate(int setupdaterate)
{
if (KepGroup != null)
{
KepGroup.UpdateRate = setupdaterate;
}
}
///
/// 获取数据更新的频率
///
///
public int GetUpdateRate()
{
return KepGroup.UpdateRate;
}
///
/// 向指定的地址项写入指定的值
///
/// 地址服务句柄
/// 要写入的值
public void WritetagValue(int p_itmHandleServer, object obj)
{
try
{
OPCItem bItem = KepGroup.OPCItems.GetOPCItem(p_itmHandleServer);
int[] temp = new int[2] { 0, bItem.ServerHandle };
Array serverHandles = (Array)temp;
object[] valueTemp = new object[2] { "", obj };
Array values = (Array)valueTemp;
Array Errors;
KepGroup.SyncWrite(1, ref serverHandles, ref values, out Errors);
GC.Collect();
}
catch (Exception ex)
{
}
finally
{
}
}
///
/// 向指定的地址项写入指定的值,不触发订阅
///
/// 地址服务句柄
/// 要写入的值
public void OPCItemWritetagValue(int p_itmHandleServer, object obj)
{
try
{
OPCItem bItem = KepGroup.OPCItems.GetOPCItem(p_itmHandleServer);
//int[] temp = new int[2] { 0, bItem.ServerHandle };
//Array serverHandles = (Array)temp;
//object[] valueTemp = new object[2] { "", obj };
//Array values = (Array)valueTemp;
//Array Errors;
//这句也可以写入,但并不触发写入事件
bItem.Write(obj);
GC.Collect();
}
catch (Exception ex)
{
}
finally
{
}
}
}
}