C# OPC 客户端示例:连接、读取、写入和异步通知
这是一个 C# OPC 客户端示例,演示如何连接 OPC 服务器、创建 OPC 组、添加 OPC 项目、读取和写入变量值,并实现异步通知功能。
基础示例
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OPCAutomation; // 添加 OPC 引用
namespace OPCClientDemo
{
class Program
{
static void Main(string[] args)
{
OPCServer server = new OPCServer(); // 创建 OPCServer 对象
server.Connect('Kepware.KEPServerEx.V4', ''); // 连接 OPC 服务器
OPCGroup group = server.OPCGroups.Add('Group1'); // 创建 OPCGroup 对象
group.IsActive = true;
group.IsSubscribed = true;
group.UpdateRate = 1000; // 刷新频率
OPCItem item = group.OPCItems.AddItem('Channel1.Device1.Tag1', 1); // 添加 OPCItem 对象
Console.WriteLine('Initial value of Tag1: {0}', item.Value);
Console.ReadLine();
server.Disconnect(); // 断开 OPC 服务器连接
}
}
}
这段代码演示了如何连接 OPC 服务器、创建 OPC 组、添加 OPC 项目以及读取变量值。
异步操作
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OPCAutomation; // 添加 OPC 引用
namespace OPCClientDemo
{
class Program
{
static void Main(string[] args)
{
OPCServer server = new OPCServer(); // 创建 OPCServer 对象
server.Connect('Kepware.KEPServerEx.V4', ''); // 连接 OPC 服务器
OPCGroup group = server.OPCGroups.Add('Group1'); // 创建 OPCGroup 对象
group.IsActive = true;
group.IsSubscribed = true;
group.UpdateRate = 1000; // 刷新频率
group.AsyncReadComplete += OnAsyncReadComplete; // 添加异步读取完成事件处理方法
group.AsyncWriteComplete += OnAsyncWriteComplete; // 添加异步写入完成事件处理方法
OPCItem item = group.OPCItems.AddItem('Channel1.Device1.Tag1', 1); // 添加 OPCItem 对象
Console.WriteLine('Initial value of Tag1: {0}', item.Value);
// 异步读取 Tag2 和 Tag3 的值
OPCItem tag2 = group.OPCItems.AddItem('Channel1.Device1.Tag2', 2);
OPCItem tag3 = group.OPCItems.AddItem('Channel1.Device1.Tag3', 3);
int transactionID = group.AsyncRead(2, new int[] { tag2.ServerHandle, tag3.ServerHandle }, out Array values, out Array errors, transactionID);
Console.WriteLine('Async read started, transaction ID: {0}', transactionID);
Console.ReadLine();
// 异步写入 Tag1 的值
object newValue = 123;
int writeTransactionID = group.AsyncWrite(new int[] { item.ServerHandle }, new object[] { newValue }, out Array writeErrors, writeTransactionID);
Console.WriteLine('Async write started, transaction ID: {0}', writeTransactionID);
Console.ReadLine();
server.Disconnect(); // 断开 OPC 服务器连接
}
static void OnAsyncReadComplete(int transactionID, int numItems, ref Array clientHandles, ref Array itemValues, ref Array qualities, ref Array timeStamps, ref Array errors)
{
Console.WriteLine('Async read completed, transaction ID: {0}', transactionID);
for (int i = 1; i <= numItems; i++)
{
Console.WriteLine('Item {0} value: {1}', i, itemValues.GetValue(i));
}
}
static void OnAsyncWriteComplete(int transactionID, int numItems, ref Array clientHandles, ref Array errors)
{
Console.WriteLine('Async write completed, transaction ID: {0}', transactionID);
}
}
}
这段代码演示了如何使用异步操作读取和写入变量值,并使用事件处理程序处理完成事件。
异步通知
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OPCAutomation; // 添加 OPC 引用
namespace OPCClientDemo
{
class Program
{
static void Main(string[] args)
{
OPCServer server = new OPCServer(); // 创建 OPCServer 对象
server.Connect('Kepware.KEPServerEx.V4', ''); // 连接 OPC 服务器
OPCGroup group = server.OPCGroups.Add('Group1'); // 创建 OPCGroup 对象
group.IsActive = true;
group.IsSubscribed = true;
group.UpdateRate = 1000; // 刷新频率
group.Asynchronous = true; // 开启异步通知
group.DataChange += OnDataChange; // 添加数据变化事件处理方法
OPCItem item = group.OPCItems.AddItem('Channel1.Device1.Tag1', 1); // 添加 OPCItem 对象
Console.WriteLine('Initial value of Tag1: {0}', item.Value);
Console.ReadLine();
server.Disconnect(); // 断开 OPC 服务器连接
}
static void OnDataChange(int transactionID, int groupID, int quality, ref Array clientHandles, ref Array itemValues, ref Array qualities, ref Array timeStamps)
{
Console.WriteLine('Tag1 value changed: {0}', itemValues.GetValue(1));
}
}
}
这段代码演示了如何使用异步通知,当变量值发生变化时,事件处理程序会被触发。
写入变量值
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OPCAutomation; // 添加 OPC 引用
namespace OPCClientDemo
{
class Program
{
static void Main(string[] args)
{
OPCServer server = new OPCServer(); // 创建 OPCServer 对象
server.Connect('Kepware.KEPServerEx.V4', ''); // 连接 OPC 服务器
OPCGroup group = server.OPCGroups.Add('Group1'); // 创建 OPCGroup 对象
group.IsActive = true;
group.IsSubscribed = true;
group.UpdateRate = 1000; // 刷新频率
OPCItem item = group.OPCItems.AddItem('Channel1.Device1.Tag1', 1); // 添加 OPCItem 对象
Console.WriteLine('Initial value of Tag1: {0}', item.Value);
// 写入新的值
object newValue = 123;
item.Write(newValue);
Console.ReadLine();
server.Disconnect(); // 断开 OPC 服务器连接
}
}
}
这段代码演示了如何使用 OPCItem 对象的 Write 方法写入变量值。
总结
本文提供了一个 C# OPC 客户端示例,演示了如何连接 OPC 服务器、创建 OPC 组、添加 OPC 项目、读取和写入变量值,并实现异步通知功能。在实际应用中,您需要根据自己的需求进行调整和扩展。
注意
- 确保您已经安装了 OPCAutomation 组件。
- 将代码中的
Kepware.KEPServerEx.V4替换为您的 OPC 服务器名称。 - 将代码中的
Channel1.Device1.Tag1替换为您要访问的变量名称。 - 为了确保代码的安全性,请添加异常处理机制。
祝您编码愉快!
原文地址: https://www.cveoy.top/t/topic/jPtw 著作权归作者所有。请勿转载和采集!