OPCServer.OPCGroup.add
时间: 2024-05-31 11:01:37 浏览: 14
OPCServer.OPCGroup.add 是一个方法,用于在 OPCServer 中添加一个 OPCGroup 对象。OPCGroup 对象是 OPC 数据存取的基本单元,用于管理和控制与 OPC 数据存取服务器的通信。该方法的具体实现方式和参数设置可能会因不同的 OPC 数据存取服务器而有所不同。
相关问题
c# opcserver 例程
以下是一个简单的 C# OPC Server 例程:
```csharp
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OPCAutomation;
namespace OPCServerExample
{
class Program
{
static OPCServer opcServer;
static OPCGroup opcGroup;
static void Main(string[] args)
{
try
{
// 创建 OPC Server 对象
opcServer = new OPCServer();
// 连接 OPC Server
opcServer.Connect("Matrikon.OPC.Simulation");
// 添加 OPC Group
opcGroup = opcServer.OPCGroups.Add("Group1");
opcGroup.UpdateRate = 1000;
opcGroup.IsActive = true;
// 添加 OPC Item
OPCItem opcItem = opcGroup.OPCItems.AddItem("Random.Real8", 1);
// 循环读取 OPC Item 的值
while (true)
{
object value;
object quality;
object timestamp;
opcItem.Read(1, out value, out quality, out timestamp);
Console.WriteLine("Value: {0}, Quality: {1}, TimeStamp: {2}", value, quality, timestamp);
System.Threading.Thread.Sleep(1000);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
// 断开 OPC Server 连接
opcServer.Disconnect();
}
}
}
}
```
这个例程使用了 OPCAutomation 库,可以通过 NuGet 安装。在例程中,我们连接了一个名为 "Matrikon.OPC.Simulation" 的 OPC Server,添加了一个名为 "Group1" 的 OPC Group,然后在该 Group 中添加了一个名为 "Random.Real8" 的 OPC Item,最后循环读取该 Item 的值并输出到控制台。
GodSharp.Opc.Da.OpcNetApi库opcda源码
以下是GodSharp.Opc.Da.OpcNetApi库中opcda的源码示例:
```csharp
public class OpcDa : IDisposable
{
private const int ShutdownTimeout = 10000;
private readonly object _lockObject = new object();
private readonly OpcServer _server;
private readonly IOPCServer _opcServer;
private readonly IConnectionPoint _connectionPoint;
private readonly OPCShutdown _opcShutdown;
private readonly Dictionary<int, OpcGroup> _groups = new Dictionary<int, OpcGroup>();
private int _groupId;
private bool _disposed;
public OpcDa(string host)
{
_server = new OpcServer();
_server.Connect(host);
_opcServer = _server.GetOPCServer();
_opcShutdown = new OPCShutdown(this);
var cpContainer = (IConnectionPointContainer)_opcServer;
var cpGuid = typeof(IOPCShutdown).GUID;
cpContainer.FindConnectionPoint(ref cpGuid, out _connectionPoint);
_connectionPoint.Advise(_opcShutdown, out var cookie);
}
public void Dispose()
{
lock (_lockObject)
{
if (_disposed)
{
return;
}
_disposed = true;
_connectionPoint.Unadvise(_opcShutdown.Cookie);
foreach (var group in _groups.Values)
{
group.Dispose();
}
_server.Disconnect(ShutdownTimeout);
}
}
public OpcGroup AddGroup(string name, int updateRate)
{
lock (_lockObject)
{
var group = new OpcGroup(this, ++_groupId, name, updateRate);
_groups.Add(group.GroupId, group);
return group;
}
}
public void RemoveGroup(OpcGroup group)
{
lock (_lockObject)
{
if (_groups.TryGetValue(group.GroupId, out var target))
{
target.Dispose();
_groups.Remove(target.GroupId);
}
}
}
public IOPCItemProperties GetItemProperties()
{
_opcServer.QueryInterface(typeof(IOPCItemProperties).GUID, out var itemProperties);
return (IOPCItemProperties)itemProperties;
}
public IOPCBrowseServerAddressSpace GetBrowser()
{
_opcServer.QueryInterface(typeof(IOPCBrowseServerAddressSpace).GUID, out var browser);
return (IOPCBrowseServerAddressSpace)browser;
}
public void ShutdownRequest()
{
Dispose();
}
internal IOPCServer OpcServer => _opcServer;
internal void RemoveGroup(int groupId)
{
lock (_lockObject)
{
if (_groups.TryGetValue(groupId, out var target))
{
target.Dispose();
_groups.Remove(target.GroupId);
}
}
}
private class OPCShutdown : IOPCShutdown
{
private readonly OpcDa _parent;
public OPCShutdown(OpcDa parent)
{
_parent = parent;
}
public int Cookie { get; private set; }
public void ShutdownRequest(string reason)
{
_parent.ShutdownRequest();
}
}
}
```
该类提供了与OPC DA服务器通信的功能。它使用OpcServer对象连接到OPC服务器,并提供添加/删除组以及获取ItemProperties和Browser的功能。在Dispose方法中,它释放了与OPC服务器的连接并释放了所有组。 OPCShutdown类用于处理OPC服务器关闭请求。