From 245569d667f6f6d308b20c4e401b17686f023243 Mon Sep 17 00:00:00 2001 From: MysticBoy Date: Sat, 3 Sep 2022 19:04:53 +0800 Subject: [PATCH] BitVector32 --- IoTSharpSdk/BitVector32Extensions.cs | 112 +++++++++++++++++++++++++++ Jobs/ModbusMasterJob.cs | 2 +- 2 files changed, 113 insertions(+), 1 deletion(-) create mode 100644 IoTSharpSdk/BitVector32Extensions.cs diff --git a/IoTSharpSdk/BitVector32Extensions.cs b/IoTSharpSdk/BitVector32Extensions.cs new file mode 100644 index 0000000..30c089b --- /dev/null +++ b/IoTSharpSdk/BitVector32Extensions.cs @@ -0,0 +1,112 @@ +using System.Reflection; + +namespace System.Collections.Specialized +{ + [System.AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, Inherited = false, AllowMultiple = true)] + public sealed class BitSectionAttribute : Attribute + { + // See the attribute guidelines at + // http://go.microsoft.com/fwlink/?LinkId=85236 + readonly short _len; + + // This is a positional argument + public BitSectionAttribute(short len) + { + _len = len; + } + + public short Len + { + get { return _len; } + } + public int Index { get; set; } + internal BitVector32.Section Section { get; set; } + } + public static class BitVector32Extensions + { + + /// + /// 取高位值 + /// + /// + /// + public static int BitHeightValue(this int value) + { + BitVector32 vector32 = new BitVector32(value); + BitVector32.Section _58_height = BitVector32.CreateSection(0xf, BitVector32.CreateSection(0xf)); + return vector32[_58_height]; + + } + /// + /// 去低位值 + /// + /// + /// + public static int BitLowValue(this int value) + { + BitVector32 vector32 = new BitVector32(value); + BitVector32.Section _04_low = BitVector32.CreateSection(0xf); + return vector32[_04_low]; + } + + public static Dictionary ToDictionary(this BitVector32 vector32,Dictionary secs) + { + Dictionary warn = new(); + List<(string pi, BitSectionAttribute bsa)> pairs = new List<(string pi, BitSectionAttribute bsa)> (); + secs.Keys.ToList().ForEach(p => + { + var bs = new BitSectionAttribute(secs[p]); + if (bs != null) + { + pairs.Add((p, bs)); + } + }); + var lst = pairs.ToList(); + for (int i = 0; i < lst.Count; i++) + { + var bsa = lst[i].bsa; + var pi = lst[i].pi; + if (i == 0) + { + bsa.Section = BitVector32.CreateSection((short)(Math.Pow(2, bsa.Len)- bsa.Len)); + + } + else + { + bsa.Section = BitVector32.CreateSection((short)(Math.Pow(2, bsa.Len )- bsa.Len), lst[i - 1].bsa.Section); + } + warn.Add(pi, vector32[bsa.Section]); + } + return warn; + } + public static T? To(this BitVector32 vector32) where T : class, new() + { + T? warn = new(); + List<(PropertyInfo pi, BitSectionAttribute bsa)> pairs = new List<(PropertyInfo pi, BitSectionAttribute bsa)>(); + warn?.GetType().GetProperties().ToList().ForEach(p => + { + var bs = p.GetCustomAttributes(typeof(BitSectionAttribute), true).ToList().FirstOrDefault() as BitSectionAttribute; + if (bs != null) + { + pairs.Add((p, bs)); + } + }); + var lst = pairs.OrderBy(k => k.bsa.Index).ToList(); + for (int i = 0; i < lst.Count; i++) + { + var bsa = lst[i].bsa; + var pi = lst[i].pi; + if (i == 0) + { + bsa.Section = BitVector32.CreateSection((short)(2 ^ bsa.Len - 1)); + } + else + { + bsa.Section = BitVector32.CreateSection((short)(2 ^ bsa.Len - 1), lst[i - 1].bsa.Section); + } + pi.SetValue(warn, vector32[bsa.Section]); + } + return warn; + } + } +} diff --git a/Jobs/ModbusMasterJob.cs b/Jobs/ModbusMasterJob.cs index e299416..9b69d08 100644 --- a/Jobs/ModbusMasterJob.cs +++ b/Jobs/ModbusMasterJob.cs @@ -277,7 +277,7 @@ namespace IoTSharp.Gateways.Jobs private async Task UploadData(ModbusSlave slave, PointMapping point, BitVector32 value) { Dictionary lst = new Dictionary(); - var _format = point.DateTimeFormat ?? $"{point.DataName}_unknow1:8;{point.DataName}_unknow2:8"; + var _format = point.DataFormat ?? $"{point.DataName}_unknow1:8;{point.DataName}_unknow2:8"; _format.Split(';').ToList().ForEach(s => { var sk = s.Split(':');