Emergency Fix for AddressCombiner

This commit is contained in:
luosheng
2023-05-20 22:17:43 +08:00
parent f6da8e8b58
commit cabdc7ec81

View File

@@ -1,4 +1,5 @@
using System; using Quartz.Util;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
@@ -158,17 +159,29 @@ namespace Modbus.Net
OriginalAddresses = originalAddresses.ToList() OriginalAddresses = originalAddresses.ToList()
}); });
} }
var newAns = MaxExclude(ans);
return newAns;
}
/// <summary>
/// 将单个超长连续地址池拆分
/// </summary>
/// <param name="ans">拆分前的连续地址池</param>
/// <returns>拆分后的连续地址池</returns>
protected List<CommunicationUnit<TKey>> MaxExclude(List<CommunicationUnit<TKey>> ans)
{
var newAns = new List<CommunicationUnit<TKey>>(); var newAns = new List<CommunicationUnit<TKey>>();
foreach (var communicationUnit in ans) foreach (var communicationUnit in ans)
{ {
var oldByteCount = communicationUnit.GetCount * var oldByteCount = communicationUnit.GetCount *
BigEndianValueHelper.Instance.ByteLength[communicationUnit.DataType.FullName]; BigEndianValueHelper.Instance.ByteLength[communicationUnit.DataType.FullName];
var oldOriginalAddresses = communicationUnit.OriginalAddresses.ToList();
while (oldByteCount * BigEndianValueHelper.Instance.ByteLength[communicationUnit.DataType.FullName] > while (oldByteCount * BigEndianValueHelper.Instance.ByteLength[communicationUnit.DataType.FullName] >
MaxLength) MaxLength)
{ {
var newOriginalAddresses = new List<AddressUnit<TKey>>(); var newOriginalAddresses = new List<AddressUnit<TKey>>();
var oldOriginalAddresses = communicationUnit.OriginalAddresses.ToList();
var newByteCount = 0.0; var newByteCount = 0.0;
var newAddressUnitStart = oldOriginalAddresses.First();
do do
{ {
var currentAddressUnit = oldOriginalAddresses.First(); var currentAddressUnit = oldOriginalAddresses.First();
@@ -180,10 +193,10 @@ namespace Modbus.Net
} while (newByteCount < MaxLength); } while (newByteCount < MaxLength);
var newCommunicationUnit = new CommunicationUnit<TKey> var newCommunicationUnit = new CommunicationUnit<TKey>
{ {
Area = communicationUnit.Area, Area = newAddressUnitStart.Area,
Address = communicationUnit.Address, Address = newAddressUnitStart.Address,
SubAddress = communicationUnit.SubAddress, SubAddress = newAddressUnitStart.SubAddress,
DataType = communicationUnit.DataType, DataType = typeof(byte),
GetCount = GetCount =
(int) (int)
Math.Ceiling(newByteCount / Math.Ceiling(newByteCount /
@@ -193,10 +206,17 @@ namespace Modbus.Net
newAns.Add(newCommunicationUnit); newAns.Add(newCommunicationUnit);
} }
var addressUnitStart = oldOriginalAddresses.First();
communicationUnit.Area = addressUnitStart.Area;
communicationUnit.Address = addressUnitStart.Address;
communicationUnit.SubAddress = addressUnitStart.SubAddress;
communicationUnit.Address = addressUnitStart.Address;
communicationUnit.DataType = typeof(byte);
communicationUnit.GetCount = communicationUnit.GetCount =
(int) (int)
Math.Ceiling(oldByteCount / Math.Ceiling(oldByteCount /
BigEndianValueHelper.Instance.ByteLength[communicationUnit.DataType.FullName]); BigEndianValueHelper.Instance.ByteLength[communicationUnit.DataType.FullName]);
communicationUnit.OriginalAddresses = oldOriginalAddresses;
newAns.Add(communicationUnit); newAns.Add(communicationUnit);
} }
return newAns; return newAns;