lss
2025-06-04 adbeeccfcddddbba49718136a1ebb3429ab3b7ae
HH.WCS.Mobox3/HH.WCS.Mobox3.JiaTong/device/ModbusHelper.cs
@@ -1,5 +1,6 @@
using EasyModbus;
using HH.WCS.JiaTong;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
@@ -23,14 +24,18 @@
        /// <param name="ip"></param>
        /// <param name="port"></param>
        /// <returns></returns>
        internal static bool[] ReadCoils(int address, int qty, string ip, int port = 502) {
        internal static bool[] ReadCoils(int address, int qty, string ip, int port = 502)
        {
            bool[] res = new bool[0];
            var client = GetClient(ip, port);
            if (client != null && client.Connected) {
                try {
            if (client != null && client.Connected)
            {
                try
                {
                    res = client.ReadCoils(address, qty);
                }
                catch (Exception ex) {
                catch (Exception ex)
                {
                    //LogHelper.Error(ex.Message, ex);
                }
            }
@@ -45,51 +50,25 @@
        /// <param name="ip"></param>
        /// <param name="port"></param>
        /// <returns></returns>
        internal static bool[] ReadDiscreteInputs(int address, int qty, string ip, int port = 502) {
        internal static bool[] ReadDiscreteInputs(int address, int qty, string ip, int port = 502)
        {
            bool[] res = new bool[0];
            var client = GetClient(ip, port);
            if (client != null && client.Connected) {
                try {
            if (client != null && client.Connected)
            {
                try
                {
                    res = client.ReadDiscreteInputs(address, qty);
                }
                catch (Exception ex) {
                catch (Exception ex)
                {
                    //LogHelper.Error(ex.Message, ex);
                }
            }
            return res;
        }
        /// <summary>
        /// 读保持寄存器
        /// </summary>
        /// <param name="address"></param>
        /// <param name="qty"></param>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        /// <returns></returns>
        internal static int[] ReadHoldingRegisters(int address, int qty, string ip, int port = 502) {
            int[] res = new int[0];
            var client = GetClient(ip, port);
            if (client != null && client.Connected) {
                try {
                    //一个寄存器是16位,返回2个int类型
                    res = client.ReadHoldingRegisters(address, qty);
                    if (res != null) {
                        Console.WriteLine($"读寄存器{ip},address={address},qty={qty},length={res.Length},res={string.Join(",", res)}");
                    }
                    else {
                        Console.WriteLine($"读寄存器{ip},address={address},qty={qty},失败");
                    }
                }
                catch (Exception ex) {
                    Console.WriteLine("ReadHoldingRegisters:err=" + ex.Message);
                }
            }
            else {
                Console.WriteLine($"{ip}连接已经断开");
            }
            return res;
        }
        /// <summary>
        /// 读输入寄存器
        /// </summary>
@@ -98,14 +77,18 @@
        /// <param name="ip"></param>
        /// <param name="port"></param>
        /// <returns></returns>
        internal static int[] ReadInputRegisters(int address, int qty, string ip, int port = 502) {
        internal static int[] ReadInputRegisters(int address, int qty, string ip, int port = 502)
        {
            int[] res = new int[0];
            var client = GetClient(ip, port);
            if (client != null && client.Connected) {
                try {
            if (client != null && client.Connected)
            {
                try
                {
                    res = client.ReadInputRegisters(address, qty);
                }
                catch (Exception ex) {
                catch (Exception ex)
                {
                    //LogHelper.Error(ex.Message, ex);
                }
            }
@@ -119,15 +102,19 @@
        /// <param name="value"></param>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        internal static bool WriteSingleCoil(int address, bool value, string ip, int port = 502) {
        internal static bool WriteSingleCoil(int address, bool value, string ip, int port = 502)
        {
            var res = false;
            var client = GetClient(ip, port);
            if (client != null && client.Connected) {
                try {
            if (client != null && client.Connected)
            {
                try
                {
                    client.WriteSingleCoil(address, value);
                    res = true;
                }
                catch (Exception ex) {
                catch (Exception ex)
                {
                    //LogHelper.Error(ex.Message, ex);
                }
            }
@@ -141,21 +128,26 @@
        /// <param name="values"></param>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        internal static bool WriteMultipleCoils(int address, bool[] values, string ip, int port = 502) {
        internal static bool WriteMultipleCoils(int address, bool[] values, string ip, int port = 502)
        {
            var res = false;
            var client = GetClient(ip, port);
            if (client != null && client.Connected) {
                try {
            if (client != null && client.Connected)
            {
                try
                {
                    client.WriteMultipleCoils(address, values);
                    res = true;
                }
                catch (Exception ex) {
                catch (Exception ex)
                {
                    //LogHelper.Error(ex.Message, ex);
                }
            }
            return res;
        }
        /// <summary>
        /// 写单个寄存器
        /// </summary>
@@ -163,20 +155,63 @@
        /// <param name="value"></param>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        internal static bool WriteSingleRegister(int address, int value, string ip, int port = 502) {
        internal static bool WriteSingleRegister(int address, int value, string ip, int port = 502)
        {
            var res = false;
            var client = GetClient(ip, port);
            if (client != null && client.Connected) {
                try {
            if (client != null && client.Connected)
            {
                try
                {
                    client.WriteSingleRegister(address, value);
                    res = true;
                    Console.WriteLine($"写寄存器{ip},address={address},value={value},成功");
                }
                catch (Exception ex) {
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    Console.WriteLine($"写寄存器{ip},address={address},value={value},失败");
                }
            }
            return res;
        }
        /// <summary>
        /// 读保持寄存器
        /// </summary>
        /// <param name="address"></param>
        /// <param name="qty"></param>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        /// <returns></returns>
        internal static int[] ReadHoldingRegisters(int address, int qty, string ip, int port = 502)
        {
            int[] res = new int[0];
            var client = GetClient(ip, port);
            if (client != null && client.Connected)
            {
                try
                {
                    //一个寄存器是16位,返回2个int类型
                    res = client.ReadHoldingRegisters(address, qty);
                    if (res != null)
                    {
                        Console.WriteLine($"读寄存器{ip},address={address},qty={qty},length={res.Length},res={string.Join(",", res)}");
                    }
                    else
                    {
                        Console.WriteLine($"读寄存器{ip},address={address},qty={qty},失败");
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("ReadHoldingRegisters:err=" + ex.Message);
                }
            }
            else
            {
                Console.WriteLine($"{ip}连接已经断开");
            }
            return res;
        }
@@ -187,41 +222,53 @@
        /// <param name="values"></param>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        internal static bool WriteMultipleRegisters(int address, int[] values, string ip, int port = 502) {
        internal static bool WriteMultipleRegisters(int address, int[] values, string ip, int port = 502)
        {
            var res = false;
            var client = GetClient(ip, port);
            if (client != null && client.Connected) {
                try {
            if (client != null && client.Connected)
            {
                try
                {
                    client.WriteMultipleRegisters(address, values);
                    res = true;
                }
                catch (Exception ex) {
                catch (Exception ex)
                {
                    //LogHelper.Error(ex.Message, ex);
                }
            }
            return res;
        }
        private static ModbusClient GetClient(string ip, int port) {
        private static ModbusClient GetClient(string ip, int port)
        {
            ModbusClient client = null;
            if (!clients.ContainsKey(ip)) {
            if (!clients.ContainsKey(ip))
            {
                client = new ModbusClient(ip, port);
                try {
                try
                {
                    client.Connect();
                    clients[ip] = client;
                }
                catch (Exception ex) {
                catch (Exception ex)
                {
                    //LogHelper.Error(ex.Message, ex);
                    Console.WriteLine($"连接plc失败{ip},err=" + ex.Message);
                }
            }
            else {
            else
            {
                client = clients[ip];
                if (!clients[ip].Connected) {
                    try {
                if (!clients[ip].Connected)
                {
                    try
                    {
                        clients[ip].Connect();
                    }
                    catch (Exception ex) {
                    catch (Exception ex)
                    {
                        //LogHelper.Error(ex.Message, ex);
                        Console.WriteLine($"连接plc失败{ip},err=" + ex.Message);
                    }