From 92800844cf98cc36b1f0322dc5802b21353bc707 Mon Sep 17 00:00:00 2001 From: "parallelbgls@outlook.com" Date: Fri, 29 Jan 2016 16:55:23 +0800 Subject: [PATCH] 2016-01-29 update 2 --- Modbus.Net/ModBus.Net/AddressCombiner.cs | 87 ++++++++++++++++++++++++ Modbus.Net/ModBus.Net/BaseProtocal.cs | 2 +- Modbus.Net/ModBus.Net/ProtocalLinker.cs | 2 +- 3 files changed, 89 insertions(+), 2 deletions(-) diff --git a/Modbus.Net/ModBus.Net/AddressCombiner.cs b/Modbus.Net/ModBus.Net/AddressCombiner.cs index 77df7da..671dc3a 100644 --- a/Modbus.Net/ModBus.Net/AddressCombiner.cs +++ b/Modbus.Net/ModBus.Net/AddressCombiner.cs @@ -25,6 +25,7 @@ namespace ModBus.Net public override IEnumerable Combine(IEnumerable addresses) { var groupedAddresses = from address in addresses + orderby address.Address group address by address.Area into grouped select grouped; @@ -93,4 +94,90 @@ namespace ModBus.Net }).ToList(); } } + + class CommunicationUnitGap + { + public CommunicationUnit EndUnit { get; set; } + public int GapNumber { get; set; } + } + + public class AddressCombinerNumericJump : AddressCombiner + { + private int JumpNumber { get; } + + public AddressCombinerNumericJump(int jumpNumber) + { + JumpNumber = jumpNumber; + } + + public override IEnumerable Combine(IEnumerable addresses) + { + var continusAddresses = new AddressCombinerContinus().Combine(addresses).ToList(); + List addressesGaps = new List(); + CommunicationUnit preCommunicationUnit = null; + foreach (var continusAddress in continusAddresses) + { + if (preCommunicationUnit == null) + { + preCommunicationUnit = continusAddress; + continue; + } + if (continusAddress.Area == preCommunicationUnit.Area) + { + var gap = new CommunicationUnitGap() + { + EndUnit = continusAddress, + GapNumber = continusAddress.Address - preCommunicationUnit.Address - (int)(preCommunicationUnit.GetCount * BigEndianValueHelper.Instance.ByteLength[preCommunicationUnit.DataType.FullName]) + }; + addressesGaps.Add(gap); + } + preCommunicationUnit = continusAddress; + } + var orderedGaps = addressesGaps.OrderBy(p => p.GapNumber); + int jumpNumberInner = JumpNumber; + foreach (var orderedGap in orderedGaps) + { + jumpNumberInner -= orderedGap.GapNumber; + if (jumpNumberInner < 0) break; + var nowAddress = orderedGap.EndUnit; + var index = continusAddresses.IndexOf(nowAddress); + index--; + var preAddress = continusAddresses[index]; + continusAddresses.RemoveAt(index); + continusAddresses.RemoveAt(index); + var newAddress = new CommunicationUnit() + { + Area = nowAddress.Area, + Address = preAddress.Address, + GetCount = + (int) + (preAddress.GetCount*BigEndianValueHelper.Instance.ByteLength[preAddress.DataType.FullName]) + + orderedGap.GapNumber + + (int) + (nowAddress.GetCount*BigEndianValueHelper.Instance.ByteLength[nowAddress.DataType.FullName]), + DataType = typeof (byte) + }; + continusAddresses.Insert(index, newAddress); + } + return continusAddresses; + } + } + + public class AddressCombinerPercentageJump : AddressCombiner + { + private double Percentage { get; } + + public AddressCombinerPercentageJump(double percentage) + { + if (percentage < 0 || percentage > 100) throw new ArgumentException(); + Percentage = percentage; + } + + public override IEnumerable Combine(IEnumerable addresses) + { + var addressUnits = addresses as IList ?? addresses.ToList(); + double count = addressUnits.Sum(address => BigEndianValueHelper.Instance.ByteLength[address.DataType.FullName]); + return new AddressCombinerNumericJump((int)(count * Percentage / 100.0)).Combine(addressUnits); + } + } } diff --git a/Modbus.Net/ModBus.Net/BaseProtocal.cs b/Modbus.Net/ModBus.Net/BaseProtocal.cs index 9001bb9..f2ad4c8 100644 --- a/Modbus.Net/ModBus.Net/BaseProtocal.cs +++ b/Modbus.Net/ModBus.Net/BaseProtocal.cs @@ -39,7 +39,7 @@ namespace ModBus.Net } //自动寻找存在的协议并将其加载 var protocalUnit = - Assembly.Load("ModBus.Net").CreateInstance(protocalName) as ProtocalUnit; + Assembly.Load(type.Assembly.FullName).CreateInstance(protocalName) as ProtocalUnit; if (protocalUnit == null) throw new InvalidCastException("没有相应的协议内容"); Register(protocalUnit); return Protocals[protocalName]; diff --git a/Modbus.Net/ModBus.Net/ProtocalLinker.cs b/Modbus.Net/ModBus.Net/ProtocalLinker.cs index 4a78e65..a0759d2 100644 --- a/Modbus.Net/ModBus.Net/ProtocalLinker.cs +++ b/Modbus.Net/ModBus.Net/ProtocalLinker.cs @@ -112,7 +112,7 @@ namespace ModBus.Net { //自动查找相应的协议放缩类,命令规则为——当前的实际类名(注意是继承后的)+"BytesExtend"。 ProtocalLinkerBytesExtend bytesExtend = - Assembly.Load(this.GetType().Assembly.GetName().Name).CreateInstance(this.GetType().FullName + "BytesExtend") as + Assembly.Load(this.GetType().Assembly.FullName).CreateInstance(this.GetType().FullName + "BytesExtend") as ProtocalLinkerBytesExtend; return bytesExtend?.BytesExtend(content); }