You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
363 lines
11 KiB
363 lines
11 KiB
4 years ago
|
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
|
||
|
{
|
||
|
/// <summary>
|
||
|
/// OPCServer Object
|
||
|
/// </summary>
|
||
|
public OPCServer KepServer;
|
||
|
/// <summary>
|
||
|
/// OPCGroups Object
|
||
|
/// </summary>
|
||
|
public OPCGroups KepGroups;
|
||
|
/// <summary>
|
||
|
/// OPCGroup Object
|
||
|
/// </summary>
|
||
|
public OPCGroup KepGroup;
|
||
|
/// <summary>
|
||
|
/// OPCItems Object
|
||
|
/// </summary>
|
||
|
public OPCItems KepItems;
|
||
|
/// <summary>
|
||
|
/// OPCItem Object
|
||
|
/// </summary>
|
||
|
public OPCItem KepItem;
|
||
|
/// <summary>
|
||
|
/// 客户端句柄
|
||
|
/// </summary>
|
||
|
int itmHandleClient = 1234;
|
||
|
/// <summary>
|
||
|
/// 存放服务端句柄
|
||
|
/// </summary>
|
||
|
public Dictionary<string, int> dirHandleServer = new Dictionary<string, int>();
|
||
|
|
||
|
/// <summary>
|
||
|
/// 枚举本地OPC服务器
|
||
|
/// </summary>
|
||
|
public object GetListOPCServers()
|
||
|
{
|
||
|
//获取本地计算机上的OPCServerName
|
||
|
try
|
||
|
{
|
||
|
KepServer = new OPCServer();
|
||
|
object serverList = KepServer.GetOPCServers();
|
||
|
return serverList;
|
||
|
}
|
||
|
catch (Exception ex)
|
||
|
{
|
||
|
throw ex;
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 枚举本地OPC服务器
|
||
|
/// </summary>
|
||
|
public object GetListOPCServers(string strHostIP)
|
||
|
{
|
||
|
//获取本地计算机上的OPCServerName
|
||
|
try
|
||
|
{
|
||
|
KepServer = new OPCServer();
|
||
|
object serverList = KepServer.GetOPCServers(strHostIP);
|
||
|
return serverList;
|
||
|
}
|
||
|
catch (Exception ex)
|
||
|
{
|
||
|
throw ex;
|
||
|
}
|
||
|
|
||
|
}
|
||
|
/// <summary>
|
||
|
/// 连接OPC服务器
|
||
|
/// </summary>
|
||
|
/// <param name="remoteServerIP">OPCServerIP</param>
|
||
|
/// <param name="remoteServerName">OPCServer名称</param>
|
||
|
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;
|
||
|
}
|
||
|
}
|
||
|
/// <summary>
|
||
|
/// 取消连接OPC服务器
|
||
|
/// </summary>
|
||
|
/// <param name="remoteServerIP">OPCServerIP</param>
|
||
|
/// <param name="remoteServerName">OPCServer名称</param>
|
||
|
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;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 创建组
|
||
|
/// </summary>
|
||
|
/// <param name="groupname">组的名称</param>
|
||
|
/// <returns></returns>
|
||
|
public OPCGroup CreateGroup(string groupname)
|
||
|
{
|
||
|
try
|
||
|
{
|
||
|
KepGroups = KepServer.OPCGroups;
|
||
|
KepGroup = KepGroups.Add(groupname);
|
||
|
KepItems = KepGroup.OPCItems;
|
||
|
return KepGroup;
|
||
|
}
|
||
|
catch (Exception err)
|
||
|
{
|
||
|
return null;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 移除组
|
||
|
/// </summary>
|
||
|
/// <param name="groupname">组的名称</param>
|
||
|
/// <returns></returns>
|
||
|
public void RemoveGroup(string groupname)
|
||
|
{
|
||
|
try
|
||
|
{
|
||
|
KepServer.OPCGroups.Remove(groupname);
|
||
|
}
|
||
|
catch (Exception)
|
||
|
{
|
||
|
}
|
||
|
}
|
||
|
/// <summary>
|
||
|
/// 设置组属性
|
||
|
/// </summary>
|
||
|
public void SetGroupProperty()
|
||
|
{
|
||
|
KepServer.OPCGroups.DefaultGroupIsActive = true;
|
||
|
KepServer.OPCGroups.DefaultGroupDeadband = 0;
|
||
|
KepGroup.IsActive = true;
|
||
|
KepGroup.IsSubscribed = true;
|
||
|
}
|
||
|
/// <summary>
|
||
|
/// 向OPC中添加地址项
|
||
|
/// </summary>
|
||
|
/// <param name="itemname">地址项名称</param>
|
||
|
/// <returns></returns>
|
||
|
public int AddKepItem(string itemname)
|
||
|
{
|
||
|
KepItem = KepGroup.OPCItems.AddItem(itemname, itmHandleClient);
|
||
|
dirHandleServer.Add(itemname, KepItem.ServerHandle);
|
||
|
return KepItem.ServerHandle;
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 向OPC中添加地址项
|
||
|
/// </summary>
|
||
|
/// <param name="itemname">地址项名称</param>
|
||
|
/// <param name="clientHandleValue">客户端数据</param>
|
||
|
/// <returns></returns>
|
||
|
public int AddKepItem(string itemname, int clientHandleValue)
|
||
|
{
|
||
|
KepItem = KepGroup.OPCItems.AddItem(itemname, clientHandleValue);
|
||
|
dirHandleServer.Add(itemname, KepItem.ServerHandle);
|
||
|
return KepItem.ServerHandle;
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 向OPC中添加连续添加多个地址项
|
||
|
/// </summary>
|
||
|
/// <param name="NumItems">添加地址的个数</param>
|
||
|
/// <param name="ItmeIds">添加连续地址的字符数组</param>
|
||
|
/// <param name="ClientHandles">添加连续地址的字符数组句柄</param>
|
||
|
/// <param name="ServerHandles">返回连续地址服务句柄</param>
|
||
|
/// <param name="Errors">返回错误信息</param>
|
||
|
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()));
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 移除地址项
|
||
|
/// </summary>
|
||
|
/// <returns></returns>
|
||
|
public void RemoveKepItems()
|
||
|
{
|
||
|
int NumItems = dirHandleServer.Count;
|
||
|
Array Errors;
|
||
|
int[] handle = new int[NumItems + 1];
|
||
|
int index = 0;
|
||
|
handle[index] = 0;
|
||
|
foreach (KeyValuePair<string, int> kvp in dirHandleServer)
|
||
|
{
|
||
|
index++;
|
||
|
handle[index] = kvp.Value;
|
||
|
}
|
||
|
Array ServerHandle = handle.ToArray();
|
||
|
KepGroup.OPCItems.Remove(NumItems, ref ServerHandle, out Errors);
|
||
|
}
|
||
|
/// <summary>
|
||
|
/// 读取一个地址项的值
|
||
|
/// </summary>
|
||
|
/// <param name="p_itmHandleServer">地方服务句柄</param>
|
||
|
/// <returns>返回读取的值</returns>
|
||
|
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
|
||
|
{
|
||
|
|
||
|
}
|
||
|
}
|
||
|
/// <summary>
|
||
|
/// 释放资源处理的事情
|
||
|
/// </summary>
|
||
|
public void ReleaseResource()
|
||
|
{
|
||
|
try
|
||
|
{
|
||
|
if (KepServer != null)
|
||
|
{
|
||
|
KepServer.Disconnect();
|
||
|
KepServer = null;
|
||
|
}
|
||
|
}
|
||
|
catch
|
||
|
{
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 设置数据更新的频率
|
||
|
/// </summary>
|
||
|
/// <param name="setupdaterate"></param>
|
||
|
public void SetUpdateRate(int setupdaterate)
|
||
|
{
|
||
|
if (KepGroup != null)
|
||
|
{
|
||
|
KepGroup.UpdateRate = setupdaterate;
|
||
|
}
|
||
|
}
|
||
|
/// <summary>
|
||
|
/// 获取数据更新的频率
|
||
|
/// </summary>
|
||
|
/// <returns></returns>
|
||
|
public int GetUpdateRate()
|
||
|
{
|
||
|
return KepGroup.UpdateRate;
|
||
|
}
|
||
|
/// <summary>
|
||
|
/// 向指定的地址项写入指定的值
|
||
|
/// </summary>
|
||
|
/// <param name="p_itmHandleServer">地址服务句柄</param>
|
||
|
/// <param name="obj">要写入的值</param>
|
||
|
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
|
||
|
{
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 向指定的地址项写入指定的值,不触发订阅
|
||
|
/// </summary>
|
||
|
/// <param name="p_itmHandleServer">地址服务句柄</param>
|
||
|
/// <param name="obj">要写入的值</param>
|
||
|
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
|
||
|
{
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
}
|
||
|
}
|