From d0707a867ad0a8150c0e4c70fad1a3fea2307cb1 Mon Sep 17 00:00:00 2001 From: luosheng Date: Mon, 20 May 2024 16:25:21 +0800 Subject: [PATCH] hj212 --- Modbus.Net/Modbus.Net.HJ212/HJ212Protocol.cs | 10 ++++++---- .../Modbus.Net.HJ212/HJ212ProtocolLinkerBytesExtend.cs | 8 ++++---- Modbus.Net/Modbus.Net/Connector/TcpConnector.cs | 3 ++- Modbus.Net/Modbus.Net/Connector/UdpConnector.cs | 3 ++- 4 files changed, 14 insertions(+), 10 deletions(-) diff --git a/Modbus.Net/Modbus.Net.HJ212/HJ212Protocol.cs b/Modbus.Net/Modbus.Net.HJ212/HJ212Protocol.cs index bc01a23..8edf400 100644 --- a/Modbus.Net/Modbus.Net.HJ212/HJ212Protocol.cs +++ b/Modbus.Net/Modbus.Net.HJ212/HJ212Protocol.cs @@ -59,8 +59,9 @@ namespace Modbus.Net.HJ212 formatMessage += "CN=" + r_message.CN + ";"; formatMessage += "PW=" + r_message.PW + ";"; formatMessage += "MN=" + r_message.MN + ";"; + formatMessage += "Flag=5;"; formatMessage += "CP=&&"; - formatMessage += "DateTime=" + r_message.Datetime.ToString("yyyyMMddHHmmss") + ";"; + formatMessage += "DataTime=" + r_message.Datatime + ";"; foreach (var record in r_message.CP) { foreach (var data in record) @@ -93,15 +94,16 @@ namespace Modbus.Net.HJ212 { public WriteRequestHJ212InputStruct(string st, string cn, string pw, string mn, List> cp, DateTime datetime) { + QN = datetime.ToString("yyyyMMddHHmmssffff"); ST = st; CN = cn; PW = pw; MN = mn; CP = cp; - Datetime = datetime; + Datatime = datetime.ToString("yyyyMMddHHmmss"); } - public string QN => "20170101000926706"; + public string QN { get; } public string ST { get; } @@ -113,7 +115,7 @@ namespace Modbus.Net.HJ212 public List> CP { get; } - public DateTime Datetime { get; } + public string Datatime { get; } } /// diff --git a/Modbus.Net/Modbus.Net.HJ212/HJ212ProtocolLinkerBytesExtend.cs b/Modbus.Net/Modbus.Net.HJ212/HJ212ProtocolLinkerBytesExtend.cs index 26a09c0..09778c3 100644 --- a/Modbus.Net/Modbus.Net.HJ212/HJ212ProtocolLinkerBytesExtend.cs +++ b/Modbus.Net/Modbus.Net.HJ212/HJ212ProtocolLinkerBytesExtend.cs @@ -15,10 +15,10 @@ namespace Modbus.Net.HJ212 /// 扩展后的协议内容 public byte[] BytesExtend(byte[] content) { - var newFormat = new byte[content.Length + 12]; + var newFormat = new byte[content.Length + 14]; Array.Copy(content, 0, newFormat, 6, content.Length); //表头长度扩张 - var length = content.Length; + var length = content.Length + 2; string lengthString = length.ToString("0000"); lengthString = "##" + lengthString; var lengthCalc = Encoding.ASCII.GetBytes(lengthString); @@ -27,9 +27,9 @@ namespace Modbus.Net.HJ212 var crc = new byte[2]; Crc16.GetInstance().GetCRC(content, ref crc); string crcString = BitConverter.ToString(crc).Replace("-", string.Empty); - crcString = "&&" + crcString; + crcString = "&&" + crcString + "\r\n"; var crcCalc = Encoding.ASCII.GetBytes(crcString); - Array.Copy(crcCalc, 0, newFormat, newFormat.Length - 6, 6); + Array.Copy(crcCalc, 0, newFormat, newFormat.Length - 8, 8); return newFormat; } diff --git a/Modbus.Net/Modbus.Net/Connector/TcpConnector.cs b/Modbus.Net/Modbus.Net/Connector/TcpConnector.cs index b3946b8..88923eb 100644 --- a/Modbus.Net/Modbus.Net/Connector/TcpConnector.cs +++ b/Modbus.Net/Modbus.Net/Connector/TcpConnector.cs @@ -118,7 +118,8 @@ namespace Modbus.Net pipeline.AddLast("handler", this); })); - Channel = await bootstrap.ConnectAsync(new IPEndPoint(IPAddress.Parse(_host), _port)); + var isIp = IPAddress.TryParse(_host, out _); + Channel = await bootstrap.ConnectAsync(isIp ? new IPEndPoint(IPAddress.Parse(_host), _port) : new DnsEndPoint(_host, _port)); if (Channel.Open) { diff --git a/Modbus.Net/Modbus.Net/Connector/UdpConnector.cs b/Modbus.Net/Modbus.Net/Connector/UdpConnector.cs index 7a58059..f341e46 100644 --- a/Modbus.Net/Modbus.Net/Connector/UdpConnector.cs +++ b/Modbus.Net/Modbus.Net/Connector/UdpConnector.cs @@ -180,7 +180,8 @@ namespace Modbus.Net logger.LogDebug($"Udp client {ConnectionToken} send: {string.Concat(datagram.Select(p => " " + p.ToString("X2")))}"); IByteBuffer buffer = Unpooled.Buffer(); buffer.WriteBytes(datagram); - var packet = new DatagramPacket((IByteBuffer)buffer.Retain(), new IPEndPoint(IPAddress.Parse(_host), _port)); + var isIp = IPAddress.TryParse(_host, out _); + var packet = new DatagramPacket((IByteBuffer)buffer.Retain(), isIp ? new IPEndPoint(IPAddress.Parse(_host), _port) : new DnsEndPoint(_host, _port)); await Channel.WriteAndFlushAsync(packet); } catch (Exception err)