From adbeeccfcddddbba49718136a1ebb3429ab3b7ae Mon Sep 17 00:00:00 2001 From: lss <Lss@HanInfo> Date: 星期三, 04 六月 2025 08:35:50 +0800 Subject: [PATCH] 电梯流程开发 --- HH.WCS.Mobox3/HH.WCS.Mobox3.JiaTong/device/PlcHelper.cs | 117 ++-------------------------------------------------------- 1 files changed, 5 insertions(+), 112 deletions(-) diff --git a/HH.WCS.Mobox3/HH.WCS.Mobox3.JiaTong/device/PlcHelper.cs b/HH.WCS.Mobox3/HH.WCS.Mobox3.JiaTong/device/PlcHelper.cs index 8b9284b..6e951f0 100644 --- a/HH.WCS.Mobox3/HH.WCS.Mobox3.JiaTong/device/PlcHelper.cs +++ b/HH.WCS.Mobox3/HH.WCS.Mobox3.JiaTong/device/PlcHelper.cs @@ -7,6 +7,7 @@ using System.Net.Sockets; using System.Text; using System.Threading.Tasks; +using System.Threading; namespace HH.WCS.JiaTong.device { @@ -36,40 +37,7 @@ } return client; } - /// <summary> - /// 璇讳繚鎸佸瘎瀛樺櫒 - /// </summary> - /// <param name="address"></param> - /// <param name="qty"></param> - /// <param name="ip"></param> - /// <param name="port"></param> - /// <returns></returns> - internal int[] ReadHoldingRegisters(int address, int qty, string ip, int port = 502) - { - int[] res = new int[0]; - // lock (ReadHoldingRegistersLock) - // { - LogHelper.Info($"ReadHoldingRegisters:SendMsg:Addr:{address},Qty:{qty},Ip:{ip},Port:{port}", "ModBus"); - - var client = GetClient(ip, port); - if (client != null && client.Connected) - { - client.ConnectionTimeout = 5000; - try - { - res = client.ReadHoldingRegisters(address, qty); - client.Disconnect(); - } - catch (Exception ex) - { - //LogHelper.Error(ex.Message, ex); - LogHelper.Info($"ReadHoldingRegisters锛欵rror:{ex.Message}"); - } - // } - LogHelper.Info($"ReadHoldingRegisters:ReqMsg:{JsonConvert.SerializeObject(res)},Addr:{address},Ip:{ip},Port:{port}", "ModBus"); - } - return res; - } + internal static void Receive(string ip, string msg) { //澶勭悊璁惧淇″彿 @@ -107,58 +75,9 @@ return Encoding.ASCII.GetString(returnBytes); } - /// <summary> - /// 璇讳繚鎸佸瘎瀛樺櫒 - /// </summary> - /// <param name="address"></param> - /// <param name="qty"></param> - /// <param name="ip"></param> - /// <param name="port"></param> - /// <returns></returns> - internal static bool[] ReadInputRegistersRtu2(int address, int qty, string ip, int port = 502) - { - //01 02 00 01 00 01 E8 0A - List<bool> res = new List<bool>(); - var hex = $"0102{address.ToString("X4")}{qty.ToString("X4")}"; - hex = hex + BitConverter.ToString(PlcHelper.CRC16LH(PlcHelper.Hex2Bytes(hex))).Replace("-", "").Replace(" ", ""); - LogHelper.Info($"鐢垫鐘舵�鍒ゆ柇锛歿hex} ip:{ip},port{port} ", "鐢垫"); - var data = SendHexOnce(ip, port, hex); - if (!string.IsNullOrEmpty(data)) - { - string fh = data.Substring(6, 2); - var xhw = Convert.ToString(Convert.ToInt32(fh, 16), 2); - if (xhw.Length == 3) - { - xhw = '0' + xhw; - } - else if (xhw.Length == 2) - { - xhw = '0' + '0' + xhw; - } - else if (xhw.Length == 1) - { - xhw = '0' + '0' + '0' + xhw; - } - foreach (var item in xhw) - { - if (true) - { - if (item == '1') - { - res.Add(true); - } - else - { - res.Add(false); - } - } - } - } - return res.ToArray(); - } - + internal static bool[] ReadInputRegistersRtu1(int address, int qty, string ip, int port = 502) { //01 02 00 01 00 01 E8 0A @@ -212,34 +131,7 @@ return res.ToArray(); } - /// <summary> - /// 鍐欏崟涓瘎瀛樺櫒 - /// </summary> - /// <param name="address"></param> - /// <param name="value"></param> - /// <param name="ip"></param> - /// <param name="port"></param> - internal static bool WriteSingleRegisterRtuT(int address, int value, string ip, int port = 502) - { - //01 05 00 01 FF 00 DD FA - var code = value == 1 ? "FF" : "00"; - var res = false; - var hex = $"0105{address.ToString("X4")}{code}00"; - - hex = hex + BitConverter.ToString(PlcHelper.CRC16LH(PlcHelper.Hex2Bytes(hex))).Replace("-", "").Replace(" ", ""); - //hex = hex + BitConverter.ToString(PlcHelper.CRC16LH(PlcHelper.Hex2Bytes(hex))).Replace("-", "").Replace(" ", ""); - var data = SendHexOnce(ip, port, hex); - LogHelper.Info($"鐢垫璇锋眰锛歿hex} ip:{ip},port{port} ", "鐢垫"); - if (!string.IsNullOrEmpty(data)) - { - if (data == hex) - { - res = true; - } - - } - return res; - } + private static string SendHexOnce(string ip, int port, string hex) { @@ -268,6 +160,7 @@ client = null; return res; } + #region 杩涘埗杞崲+CRC internal static bool CheckCRC(string hex) { -- Gitblit v1.9.1