侧边栏壁纸
  • 累计撰写 71 篇文章
  • 累计创建 15 个标签
  • 累计收到 3 条评论

目 录CONTENT

文章目录

c# 机床Opcua数据读取

寒江孤影
2023-09-12 / 0 评论 / 0 点赞 / 9 阅读 / 6060 字
温馨提示:
本文最后更新于 2023-09-19,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

opc-ua-sdk

官方文档:https://docs.traeger.de/en/software/sdk/opc-ua/net/client.development.guide

Github:https://github.com/Traeger-GmbH/opcuanet-samples

1、本文主要是工作需要,在查询资料时,在github找到的

2、使用说明

客户端安装Opc.UaFx.Client包

客户端:以西门子刀具数据读取为例

using Opc.UaFx;
using Opc.UaFx.Client;

namespace OpcuaDemo
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello, World!");

            var client = new OpcClient("opc.tcp://169.254.235.145:4840/");
            client.Security.UserIdentity = new OpcClientIdentity("OpcUaClient", "123456789");
            client.Connect();
            Console.WriteLine(client.State);

            //{
            //    OpcValue isRunning = client.ReadNode("ns=2;s=Machine/IsRunning");

            //    Console.WriteLine($"isRunning-test:ReadNode:{isRunning.Value}");
            //}
            int numTools;
            {
                var res = client.ReadNode("ns=2;s=" + Tool.numTools.Replace("<Area index>", 1.ToString()));

                Console.WriteLine($"numTools:ReadNode:{res.Value}");
                numTools = Convert.ToInt32(res.Value);
            }
          
            string[] points =  { Tool.toolNo, Tool.toolIdent, Tool.toolInMag, Tool.toolInPlace, Tool.numCuttEdges, Tool.nrDuplo };

            for (int i = 1; i < numTools+1; i++)
            {
                foreach (var item in points)
                {
                    {
                        var res = client.ReadNode("ns=2;s=" + item.Replace("<Area index>", i.ToString()).Replace("<Row index>", i.ToString()));

                        Console.WriteLine($"{item.Replace("<Area index>", i.ToString()).Replace("<Row index>", i.ToString())}:ReadNode:{res.Value}");
                    }
                }
            }
          
            Console.ReadLine();
        }


        private static uint currentToolArea = 1;

        //Tool configure
        public class Tool
        {
            public const string numTools = "/Tool/Catalogue/numTools[u<Area index>]";
            public const string toolNo = "/Tool/Catalogue/toolNo[u<Area index>, <Row index>]";
            public const string toolIdent = "/Tool/Catalogue/toolIdent[u<Area index>, <Row index>]";
            public const string toolInMag = "/Tool/Catalogue/toolInMag[u<Area index>, <Row index>]";
            public const string toolInPlace = "/Tool/Catalogue/toolInPlace[u<Area index>, <Row index>]";
            public const string numCuttEdges = "/Tool/Catalogue/numCuttEdges[u<Area index>, <Row index>]";
            public const string nrDuplo = "/Tool/Catalogue/nrDuplo[u<Area index>, <Row index>]";
            //Tool edge configure
            public const string edgeData = "/Tool/Compensation/edgeData[u<Area index>,c<Column index>, <Row index>]";
        }

    }
    public struct ToolInfo
    {
        public UInt16 toolNo;
        public string toolIdent;
        public UInt16 nrDuplo;
        public UInt16 numCuttEdges;
        public UInt16 toolInMag;
        public UInt16 toolInPlace;
    }
}

同时支持多个节点批量读取

for (int i = 0; i < toolcounts; i++)
                    {

                        List<OpcReadNode> cmds = new List<OpcReadNode>();
                        var istr = i.ToString();
                        foreach (var item in toolPoints)
                        {
                            cmds.Add(new OpcReadNode("ns=2;s=" + item.Replace("<Area index>", istr).Replace("<Row index>", istr), OpcAttribute.DisplayName));
                        }
                        var nodes = client.ReadNodes(cmds);
                        if (nodes != null)
                        {
                            var nodevalues = nodes.ToArray();
                            OpcuaToolInfo opcuaToolInfo = new()
                            {
                                toolNo = (ushort)(nodevalues[0].Value ?? 0),
                                toolIdent = nodevalues[1].Value?.ToString(),
                                toolInMag = (ushort)(nodevalues[2].Value ?? 0),
                                toolInPlace = (ushort)(nodevalues[3].Value ?? 0),
                                numCuttEdges = (ushort)(nodevalues[4].Value ?? 0),
                                nrDuplo = (ushort)(nodevalues[5].Value ?? 0)
                            };
                            list.Add(opcuaToolInfo);
                        }
                    }

服务端安装Opc.UaFx.Advanced包

demo:

using Opc.UaFx;
using Opc.UaFx.Server;

namespace OpcuaServerDemo
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello, World!");

            var machineNode = new OpcFolderNode("Machine");
            var machineIsRunningNode = new OpcDataVariableNode<bool>(machineNode, "IsRunning",true);

            // Note: An enumerable of nodes can be also passed.
            var server = new OpcServer("opc.tcp://localhost:4840/", machineNode);
            server.Start();
            Console.WriteLine(server.State);
            
            Console.ReadLine();
        }
    }
}
0

评论区