天津投入产出系统后端
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.
 
 
 
 
 
 

127 lines
3.6 KiB

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OPCPLC;
using System.Configuration;
using OPCAutomation;
namespace EQUIPINTERFACETEST
{
public class LoadOpcLeafTest
{
OPCServer KepServer;
/// <summary>
/// OPCGroups Object
/// </summary>
OPCGroups KepGroups;
/// <summary>
/// OPCGroup Object
/// </summary>
OPCGroup KepGroup;
/// <summary>
/// OPCItems Object
/// </summary>
OPCItems KepItems;
/// <summary>
/// OPCItem Object
/// </summary>
OPCItem KepItem;
public void LoadLeafTest()
{
KepServer = new OPCServer();
//opcServer服务器IP
string opcServerIP = ConfigurationManager.AppSettings.Get("OPCServerIP");
//opcServer服务名称
string opcServerName = ConfigurationManager.AppSettings.Get("OPCServerName");
KepServer.Connect(opcServerName, opcServerIP);
WriteLog.Write("Connect Server");
OPCBrowser oPCBrowse = KepServer.CreateBrowser();
oPCBrowse.ShowBranches();
//展开叶子
oPCBrowse.ShowLeafs(true);
WriteLog.Write("ShowLeafs");
KepGroups = KepServer.OPCGroups;
KepGroup = KepGroups.Add("OPCDOTNETGROUP");
KepServer.OPCGroups.DefaultGroupIsActive = true;
KepServer.OPCGroups.DefaultGroupDeadband = 0;
//当没有配置配置文件时,我们设置默认更新频率为1秒
KepGroup.UpdateRate = 1000;
KepGroup.IsActive = true;
KepGroup.IsSubscribed = true;
// KepGroup.DataChange += new DIOPCGroupEvent_DataChangeEventHandler(KepGroup_DataChange);
KepItems = KepGroup.OPCItems;
string channel = ConfigurationManager.AppSettings.Get("Channel.Machine");
WriteLog.Write("Finish Set");
foreach (object turn in oPCBrowse)
{
string strturn = turn.ToString();
List<string> list=strturn.Split('.').ToList<string>();
if (list.Count(o => o.IndexOf('_') == 0) > 0)
{
continue;
}
if (strturn.Contains(channel) == false)
{
continue;
}
WriteLog.Write(strturn);
Console.WriteLine(strturn);
KepItem = KepItems.AddItem(strturn, 1234);
int i = KepItem.ServerHandle;
object a = ReadtagValue(i);
Console.WriteLine(a.ToString());
}
}
private 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
{
}
}
}
}