C#基于HSL批量读取三菱PLC地址 反射/数据写入Model实体类

using HslCommunication;
using HslCommunication.Profinet.Melsec;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace 批量读取三菱PLC数据
{
    class Program
    {
        private static MelsecMcNet melsec_net = new MelsecMcNet("127.0.0.1", 6000);
        public class MyClass
        {
            public int value1 { get; set; }
            public int value2 { get; set; }
            public int value3 { get; set; }
            public int value4 { get; set; }
            public int value5 { get; set; }
            public int value6 { get; set; }
            public int value7 { get; set; }
            public int value8 { get; set; }
            public int value9 { get; set; }
            public int value10 { get; set; }
        }
        static void Main(string[] args)
        {
            //构建一个实体类
            MyClass myClas = new MyClass();
            //构建一个字典
            Dictionary<string, string> dicPlcAddr = new Dictionary<string, string>()
            {
                ["value1"]="D100",
                ["value2"]="D101",
                ["value3"]="D102",
                ["value4"]="D103",
                ["value5"]="D104",
                ["value6"]="D105",
                ["value7"]="D106",
                ["value8"]="D107",
                ["value9"]="D108",
                ["value10"]="D109"
            };
            OperateResult connect = melsec_net.ConnectServer();
            if (connect.IsSuccess)
            {
                Console.WriteLine("连接成功!");
                //批量读取三菱D100-D109数据
                //读取字节数组
                //D100-D109读取
                byte[] read = melsec_net.Read("D100", 10).Content;
                //写入model
                if (read!=null)
                {
                    SetModelValue<MyClass>("D100", dicPlcAddr, read, ref myClas);
                }
            }
            else
            {
                Console.WriteLine("连接失败!");
            }

            ////输出json
            ///
            Console.WriteLine("----------------------------------");
            Console.WriteLine(JsonConvert.SerializeObject(myClas));
            Console.WriteLine("----------------------------------");
            Console.ReadKey();
        }
        
        public static bool SetModelValue<T>(string StartAddr, Dictionary<string,string> dicPlcAddr, byte[] buff, ref T obj)
        {
            var properties = obj.GetType().GetProperties();
            try
            {
                foreach (var item in properties)
                {
                    if (dicPlcAddr.Keys.Contains(item.Name))
                    {
                        foreach (var plcAddr in dicPlcAddr)
                        {
                            if (plcAddr.Key == item.Name)
                            {
                                string D = StartAddr.Remove(1, StartAddr.Length - 1);
                                int offest = (int.Parse(plcAddr.Value.Replace(D, "")) - int.Parse(StartAddr.Replace(D, ""))) * 2;
                                var val = melsec_net.ByteTransform.TransInt16(buff, offest);
                                Type t = obj.GetType();
                                object v = Convert.ChangeType(val, t.GetProperty(item.Name).PropertyType);
                                t.GetProperty(item.Name).SetValue(obj, v, null);
                            }
                            
                        }
                    }
                }
                
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}

 

调试输出如下

 

 

posted @ 2023-03-01 20:48  yun5  阅读(2296)  评论(1)    收藏  举报
Title