HJ212 Length Limit

This commit is contained in:
parallelbgls
2025-10-30 17:19:54 +08:00
parent 7f304e826f
commit e7cad88bcf
3 changed files with 51 additions and 6 deletions

View File

@@ -1,6 +1,6 @@
namespace Modbus.Net.HJ212
{
public class HJ212Controller : FifoController
public class HJ212Controller : NoResponseController
{
public HJ212Controller(string ip, int port) : base(int.Parse(ConfigurationReader.GetValue("TCP:" + ip + ":" + port, "FetchSleepTime")))
{

View File

@@ -1,4 +1,10 @@
namespace Modbus.Net.HJ212
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.Threading.Tasks;
namespace Modbus.Net.HJ212
{
/// <summary>
/// HJ212协议连接器
@@ -9,6 +15,45 @@
{
}
public override async Task<byte[]> SendReceiveAsync(byte[] content)
{
if (content.Length <= 1000)
return await base.SendReceiveAsync(content);
else
{
string contentString = Encoding.ASCII.GetString(content);
string[] formats = { "yyyyMMddHHmmssffff", "yyyyMMddHHmmssfff", "yyyyMMddHHmmss" };
DateTime startTime = DateTime.ParseExact(contentString.Substring(3, 18), formats, CultureInfo.InvariantCulture, DateTimeStyles.None);
int dataStartIdx = contentString.IndexOf("&&") + 2;
string head = contentString.Substring(0, dataStartIdx).Substring(21);
string[] contentUnitAll = contentString.Substring(dataStartIdx).Split(';')[1].Split(',');
List<string> contentSplitAll = new List<string>();
string newContent = "DataTime=" + startTime.ToString("yyyyMMddHHmmss") + ";";
foreach (var contentUnit in contentUnitAll)
{
if (newContent.Length + contentUnit.Length > 1000 - dataStartIdx)
{
contentSplitAll.Add("QN=" + startTime.ToString("yyyyMMddHHmmssffff") + head + newContent[..^1]);
startTime = startTime.AddSeconds(1);
newContent = "DataTime=" + startTime.ToString("yyyyMMddHHmmss") + ";" + contentUnit + ",";
}
else
{
newContent += contentUnit + ",";
}
}
contentSplitAll.Add("QN=" + startTime.ToString("yyyyMMddHHmmssffff") + head + newContent[..^1]);
var receiveBytesAll = new List<byte>();
foreach (var contentSplit in contentSplitAll)
{
var extBytes = BytesExtend(Encoding.ASCII.GetBytes(contentSplit));
var receiveBytes = await SendReceiveWithoutExtAndDecAsync(extBytes);
receiveBytesAll.AddRange(receiveBytes == null ? null : receiveBytes.Length == 0 ? receiveBytes : BytesDecact(receiveBytes));
}
return receiveBytesAll.ToArray();
}
}
/// <summary>
/// 检查接收的数据是否正确
/// </summary>
@@ -19,4 +64,4 @@
return true;
}
}
}
}

View File

@@ -33,14 +33,14 @@ namespace Modbus.Net.HJ212
{
var writeRequestHJ212InputStruct =
new WriteRequestHJ212InputStruct((string)setContents[0], (string)setContents[1], (string)setContents[2], (string)setContents[3], (List<Dictionary<string, string>>)setContents[4], (DateTime)setContents[5]);
var writeRequestOpcOutputStruct =
var writeRequestHJ212OutputStruct =
await
Wrapper.SendReceiveAsync<WriteRequestHJ212OutputStruct>(Wrapper[typeof(WriteRequestHJ212Protocol)],
writeRequestHJ212InputStruct);
return new ReturnStruct<bool>
{
Datas = writeRequestOpcOutputStruct?.GetValue != null,
IsSuccess = writeRequestOpcOutputStruct?.GetValue != null,
Datas = writeRequestHJ212OutputStruct?.GetValue != null,
IsSuccess = writeRequestHJ212OutputStruct?.GetValue != null,
ErrorCode = 0,
ErrorMsg = null,
};