【问题标题】:OPC dll - How to retrieve tags value in bulkOPC dll - 如何批量检索标签值
【发布时间】:2019-09-11 02:49:56
【问题描述】:

我正在使用带有 C# .NET 的 OPCSiemensDAAutomation dll 从 OPC Server 检索标签的值。我设法使用 QueryAvailableProperties()GetItemProperties() 检索了这些值,但目标是为每个请求检索 500k 个标签值。

我已经测试了 100 个标签,代码在 45 秒内完成,多线程导致 100 个标签的小幅改进为 30 秒。以当前速度达到目标标签量需要 4 个多小时。有什么方法可以批量检索标签值并具有更好的性能?谢谢。

var opcServer = new OPCSiemensDAAutomation.OPCServer();
opcServer.Connect("PCS7.OPCDAServer.1");
ConcurrentBag<DataRow> myBag = new ConcurrentBag<DataRow>(dt.AsEnumerable().ToList());
Parallel.ForEach(myBag, data =>
{
    if (count <= num)
    {
        int cnt;
        Array propertyIds, descriptions, dataTypes, errors, vals;
        try
        {
            opcServer.QueryAvailableProperties(data[0].ToString(), out cnt, out propertyIds, out descriptions, out dataTypes);
            opcServer.GetItemProperties(data[0].ToString(), cnt, propertyIds, out vals, out errors);
            Tags tag = new Tags();
            tag.Id = data[0].ToString();
            tag.Value = vals.GetValue(2).ToString();
            tags.Add(tag);
            Interlocked.Increment(ref count);
        }
        catch
        { }
    }
});

【问题讨论】:

  • 在 OPC DA 中获取属性并不是为了快速而设计的。您实际上需要数千个项目的所有属性,这将是非常不寻常的。为了什么? OPC DA 旨在快速处理实际项目值/质量/时间戳,您应该使用属性。相反,您订阅,或者,如果它们不适合,则一次性阅读。
  • @ZbynekZ 感谢您的建议,我现在已经使用 SyncRead 进行了编码,但我面临这个“1073479672- OpcInvalidItemID:项目 ID 不符合服务器的语法”。添加 OPC 项时返回错误。我检查了标签 ID 是否与 OPC Scout 或其他工具中显示的相同。你能帮忙吗?

标签: c# .net opc siemens opc-da


【解决方案1】:

您可以创建 OPC 组:

OPCGroup myGroup = myServer.addGroup(groupName, isActive, isSubscribed, updateRate);

然后你就可以给你的组添加标签了:

myGroup.OPCItems.AddItem("FullAddress", ClientHandle) //a unique number inside the group

FullAddress由OPCChannel名称、连接名称和完整地址组成,即:S7:[MyPLCName]DB1.dbx4

当组完全填充后,您可以一次读取所有变量。

int itemCount = myGroup.OPCItems.Count;
object qualities = null;
object timeStamps = null;
object errors = null;
int serverHandles[itemCount];
Array values = Array.CreateInstance(TypeOf(object), {itemCount },{1})
for (int i = 0; i < itemCount; i++){
   serverHandles[i] = myGroup.OPCItems.Item(i + 1).ServerHandle;
   values.SetValue("", i);
}

myGroup.SyncRead(OPCSiemensDAAutomation.OPCDataSource.OPCDevice, itemCount + 1, ServerHandles, values, errors, qualities, timeStamps);

然后你将有四个与第一个 serverHandles 相关的新数组。

在使用来自values 的数据之前检查qualities 数组是明智的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-17
    • 1970-01-01
    • 1970-01-01
    • 2013-03-07
    • 2016-11-02
    • 1970-01-01
    • 2010-09-16
    • 1970-01-01
    相关资源
    最近更新 更多