using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using REF.ISS.ElectricPower.BLL;
using REF.ISS.ElectricPower.BLL.Models;
using OPCAutomation;
using System.Diagnostics;
namespace REF.ISS.ElectricPower.DataCrawl
{
class Program
{
static ElectricPowerDBEntities db = new ElectricPowerDBEntities();
static void Main(string[] args)
{
ThreadStart _readData = new ThreadStart(readData);
Thread ConstrolreadData = new Thread(_readData);
ThreadStart _CpuMonitor = new ThreadStart(CpuMonitor);
Thread ConstrolCpuMonitor = new Thread(_CpuMonitor);
//Stopwatch watch = new Stopwatch();
//watch.Start();
ConstrolreadData.Start();
ConstrolCpuMonitor.Start();
// GetCurrentProcessInfo();
}
static private void readData()
{
Console.WriteLine("测试OPC数据");
Start();
Console.Read();
}
static OPCServer KepServer;
static OPCGroups KepGroups;
static OPCGroup KepGroup;
static OPCItems KepItems;
static DateTime time;
static System.Timers.Timer myTimer;
static List<int> Items = new List<int>();
/// <summary>
/// OPC服务器
/// 开始抓取OPC数据
/// </summary>
static public void Start()
{
try
{
KepServer = new OPCServer();
string serverId = System.Configuration.ConfigurationManager.AppSettings["serverId"];
string serverName = System.Configuration.ConfigurationManager.AppSettings["serverName"];
KepServer.Connect(serverName, serverId);
//判断连接状态
if (KepServer.ServerState == (int)OPCServerState.OPCRunning)
{
SendMsg("已连接到-" + KepServer.ServerName);
}
else
{
SendMsg("状态" + KepServer.ServerState.ToString());
return;
}
KepGroups = KepServer.OPCGroups;
Task.Factory.StartNew(CreateGroup);
}
catch (Exception e)
{
System.IO.File.AppendAllText("D:\\connect.txt", "------------连接出错 " + DateTime.Now + "--------------" + "\r\n", System.Text.Encoding.Default);
System.IO.File.AppendAllText("D:\\connect.txt", e + "\r\n", System.Text.Encoding.Default);
throw e;
}
}
/// <summary>
/// 公共函数
/// </summary>
/// <param name="m"></param>
static private void SendMsg(string m)
{
Console.Write("\n");
Console.WriteLine(m);
}
/// <summary>
/// 创建组
/// </summary>
static private void CreateGroup()
{
try
{
KepGroups = KepServer.OPCGroups;
KepGroup = KepGroups.Add("OpcGroup");
SetGroupProperty();
//KepGroup.DataChange += new DIOPCGroupEvent_DataChangeEventHandler(KepGroup_DataChange);
KepGroup.AsyncReadComplete += new DIOPCGroupEvent_AsyncReadCompleteEventHandler(GroupAsyncReadComplete); //隔个几秒就读到所有的数据
KepItems = KepGroup.OPCItems;
AddOpcItem();
//异步读所有数据
myTimer = new System.Timers.Timer(3000); //周期3秒
myTimer.Elapsed += myTimer_Elapsed; //到了3秒时做的事情
myTimer.AutoReset = true; //是否不断重复定时器操作
myTimer.Enabled = true;
}
catch (Exception e)
{
System.IO.File.AppendAllText("D:\\group.txt", "------------创建组出错 " + DateTime.Now + "--------------" + "\r\n", System.Text.Encoding.Default);
System.IO.File.AppendAllText("D:\\group.txt", e + "\r\n", System.Text.Encoding.Default);
SendMsg("OPC创建组出现错误:" + e.Message);
}
}
/// <summary>
/// Timer 到了时间3秒后执行的方法
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
static private void myTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
Array serverHandles = Items.ToArray();
Array Errors;
int cancelID;
KepGroup.AsyncRead(Items.Count - 1, ref serverHandles, out Errors, 1, out cancelID);//第一参数为item数量
}
/// <summary>
/// 设置采集属性
/// 设置组属性
/// </summary>
static private void SetGroupProperty()
{
KepServer.OPCGroups.DefaultGroupIsActive = true; //激活组
KepServer.OPCGroups.DefaultGroupDeadband = 0; //死区值,设为0时,服务器端该组内任何数据变化都通知组。
KepGroup.UpdateRate = 3000; //刷新频率为3秒。
KepGroup.IsActive = true;
KepGroup.IsSubscribed = true; //使用订阅功能,即可以异步,默认false
}
/// <summary>
/// 把需要采集数据的点加进去
/// </summary>
static private void AddOpcItem()
{
try //大小写出错时
{
var tags = db.BaseInfo.Where(t => (t.FloorNum == 1)).ToList(); //1号楼要读的电表
//var tags= db.BaseInfo.Where(t => (t.FloorNum == 2)).ToList(); //2号楼要读的电表
Items.Add(0);
for (int i = 0; i < tags.Count; i++)
{
KepItems.AddItem(tags[i].Tag, tags[i].ID);
Items.Add(KepItems.AddItem(tags[i].Tag, tags[i].ID).ServerHandle);
}
}
catch (Exception e)
{
System.IO.File.AppendAllText("D:\\item.txt", "------------添加item时出错 " + DateTime.Now + "--------------" + "\r\n", System.Text.Encoding.Default);
System.IO.File.AppendAllText("D:\\item.txt", e + "\r\n", System.Text.Encoding.Default);
SendMsg("添加item时出现错误:" + e.Message);
}
}
#region 数据发生变化时 执行的方法
/// <summary>
/// 点数据发送变化的事件
/// 每当项数据有变化时执行的事件
/// </summary>
/// <param name="TransactionID">处理ID</param>
/// <param name="NumItems">项个数</param>
/// <param name="ClientHandles">项客户端句柄&l
OPC客户端抓数据
最新推荐文章于 2024-08-08 08:04:31 发布
本文档展示了如何使用C#编程语言创建一个OPC客户端,连接到OPC服务器并定期抓取数据。代码包括连接配置、创建OPC组、添加数据项、设置数据变更事件以及定时读取数据的实现。此外,还提供了处理数据变化、存储数据到数据库以及监控CPU和内存使用情况的逻辑。
摘要由CSDN通过智能技术生成