【问题标题】:how to connect and read values from kepware using OPCAutomation.dll如何使用 OPCAutomation.dll 从 kepware 连接和读取值
【发布时间】:2015-06-04 10:07:49
【问题描述】:

我正在创建一个小型 c# 程序,以使用 OPCAutomation.dll 从 kepware 服务器连接和读取值,但无法获取其语法?

OPCAutomation.OPCServer _OPCServer = new OPCAutomation.OPCServer();
_OPCServer.connect("", ""......);

这些括号内会包含什么值?

【问题讨论】:

    标签: dll plc opc


    【解决方案1】:
    OPCAutomation.OPCServer _OPCServer = new OPCAutomation.OPCServer();
    _OPCServer.connect("Kepware.KEPServerEX.V5", "");
    

    第二个参数是OPC Server节点,可以留String.Empty。

    来自反射器:

    public virtual extern void Connect([In, MarshalAs(UnmanagedType.BStr)] string ProgID, [In, Optional, MarshalAs(UnmanagedType.Struct)] object Node);
    

    我正在添加一个示例来读取和写入值:

    // set up some variables
    OPCServer ConnectedOpc = new OPCServer();
    Array OPCItemIDs = Array.CreateInstance(typeof(string), 10);
    Array ItemServerHandles = Array.CreateInstance(typeof(Int32), 10);
    Array ItemServerErrors = Array.CreateInstance(typeof(Int32), 10);
    Array ClientHandles = Array.CreateInstance(typeof(Int32), 10);
    Array RequestedDataTypes = Array.CreateInstance(typeof(Int16), 10);
    Array AccessPaths = Array.CreateInstance(typeof(string), 10);
    OPCGroup OpcGroupNames;
    
    // connect to KepServerEX
    ConnectedOpc.Connect("Kepware.KEPServerEX.V5", "");
    
    // Add tags and OPC group.
    // set up the tags
    OPCItemIDs.SetValue("Counting.PLC.Station1.LoggedON", 1);
    OPCItemIDs.SetValue("Counting.PLC.Station2.LoggedON", 2);
    OPCItemIDs.SetValue("Counting.PLC.Station3.LoggedON", 3);
    OPCItemIDs.SetValue("Counting.PLC.Station1.Operator", 4);
    OPCItemIDs.SetValue("Counting.PLC.Station2.Operator", 5);
    OPCItemIDs.SetValue("Counting.PLC.Station3.Operator", 6);
    
    // set up the opc group
    OpcGroupNames = ConnectedOpc.OPCGroups.Add("Group01");
    OpcGroupNames.DeadBand = 0;
    OpcGroupNames.UpdateRate = 100;
    OpcGroupNames.IsSubscribed = true;
    OpcGroupNames.IsActive = true;
    OpcGroupNames.OPCItems.AddItems(6, ref OPCItemIDs, ref ClientHandles, out ItemServerHandles, out ItemServerErrors, RequestedDataTypes, AccessPaths);
    
    // Read the values from the server for those tags.
    // read
    Array ItemServerReadValues = Array.CreateInstance(typeof(string), 10);
    object a;
    object b;
    OpcGroupNames.SyncRead((short)OPCAutomation.OPCDataSource.OPCDevice, 6, ref ItemServerHandles, out ItemServerReadValues, out ItemServerErrors, out a, out b);
    Console.WriteLine((string)ItemServerReadValues.GetValue(4));
    Console.WriteLine((string)ItemServerReadValues.GetValue(5));
    Console.WriteLine((string)ItemServerReadValues.GetValue(6));
    
    // Write some values into the server for those tags.
    // write
    Array ItemServerWriteValues = Array.CreateInstance(typeof(object), 7);
    ItemServerWriteValues.SetValue(1, 1);
    ItemServerWriteValues.SetValue(1, 2);
    ItemServerWriteValues.SetValue(1, 3);
    ItemServerWriteValues.SetValue("Test Op 1", 4);
    ItemServerWriteValues.SetValue("Test Op 2", 5);
    ItemServerWriteValues.SetValue("Test Op 3", 6);
    OpcGroupNames.SyncWrite(6, ref ItemServerHandles, ref ItemServerReadValues, out ItemServerErrors);
    

    此示例改编自:http://lifeisunderconstruction.blogspot.mx/2011/03/opc-client-in-asp-net-c.html,我添加了它以防链接损坏。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-07
    • 1970-01-01
    • 1970-01-01
    • 2018-01-07
    • 2017-06-22
    • 2016-10-20
    • 2020-11-11
    相关资源
    最近更新 更多