| | |
| | | using EasyModbus; |
| | | using HH.WCS.JiaTong; |
| | | using Newtonsoft.Json; |
| | | using System; |
| | | using System.Collections.Generic; |
| | | using System.Linq; |
| | |
| | | /// <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); |
| | | } |
| | | } |
| | |
| | | /// <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> |
| | |
| | | /// <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); |
| | | } |
| | | } |
| | |
| | | /// <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); |
| | | } |
| | | } |
| | |
| | | /// <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> |
| | |
| | | /// <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; |
| | | } |
| | |
| | | /// <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); |
| | | } |