using System; using System.Collections.Generic; using System.IO; using System.IO.Ports; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; using System.Windows; using System.Windows.Media; namespace MV485.helper { public class Tools { private static Dictionary baudRateMap = new Dictionary { { 0, 110 }, { 1, 300 }, { 2, 600 }, { 3, 1200 }, { 4, 2400 }, { 5, 4800 }, { 6, 9600 }, { 7, 14400 }, { 8, 19200 }, { 9, 38400 }, { 10, 56000 }, { 11, 57600 }, { 12, 115200 } }; private static Dictionary parityMap = new Dictionary { { 0, "无校验" }, { 1, "奇校验" }, { 2, "偶校验" }, { 3, "校验位为 1" }, { 4, "校验位为 0" } }; private static Dictionary stopBitsMap = new Dictionary { { 1, "1 个停止位" }, { 2, "2 个停止位" } }; //水表类型 public static List> MeterTypeList = new List>() { new KeyValuePair(0,"0 - 非水表"), new KeyValuePair(1, "1 - 数字+指针"), new KeyValuePair(3, "3 - 全数字"), new KeyValuePair(2, "2 - 全指针") }; //红色指针读数上传标志 public static List> UploadRedindList = new List>() { new KeyValuePair(0, "不上传"), new KeyValuePair(1, "上传") }; public static List> FlowRateList = new List>() { new KeyValuePair(3, "DN15 - 3"), new KeyValuePair(5, "DN20 - 5"), new KeyValuePair(8, "DN25 - 8"), new KeyValuePair(12, "DN32 - 12"), new KeyValuePair(20, "DN40 - 20"), new KeyValuePair(32, "DN50 - 32"), new KeyValuePair(60, "DN65 - 60"), new KeyValuePair(79, "DN80 - 79"), new KeyValuePair(125, "DN100 - 125"), new KeyValuePair(200, "DN125 - 200"), new KeyValuePair(312, "DN150 - 312"), new KeyValuePair(500, "DN200 - 500"), new KeyValuePair(787, "DN250 - 787"), new KeyValuePair(1250, "DN300 - 1250"), new KeyValuePair(2000, "DN400 - 2000"), new KeyValuePair(3125, "DN500 - 3125"), new KeyValuePair(5000, "DN600 - 5000"), new KeyValuePair(7875, "DN800 - 7875"), }; public static List> DnValueList = new List>() { new KeyValuePair(3, "DN15"), new KeyValuePair(5, "DN20"), new KeyValuePair(8, "DN25"), new KeyValuePair(12, "DN32"), new KeyValuePair(20, "DN40"), new KeyValuePair(32, "DN50"), new KeyValuePair(60, "DN65"), new KeyValuePair(79, "DN80"), new KeyValuePair(125, "DN100"), new KeyValuePair(200, "DN125"), new KeyValuePair(312, "DN150"), new KeyValuePair(500, "DN200"), new KeyValuePair(787, "DN250"), new KeyValuePair(1250, "DN300"), new KeyValuePair(2000, "DN400"), new KeyValuePair(3125, "DN500"), new KeyValuePair(5000, "DN600"), new KeyValuePair(7875, "DN800"), }; public static List> SampleIntervalList = new List>() { //new KeyValuePair(1, "1分钟"), new KeyValuePair(2, "2分钟"), //new KeyValuePair(3, "3分钟"), new KeyValuePair(5, "5分钟"), new KeyValuePair(10, "10分钟"), new KeyValuePair(30, "30分钟"), new KeyValuePair(60, "1小时"), new KeyValuePair(120, "2小时"), new KeyValuePair(180, "3小时"), new KeyValuePair(240, "4小时"), new KeyValuePair(360, "6小时"), new KeyValuePair(480, "8小时"), new KeyValuePair(720, "12小时"), new KeyValuePair(1440, "24小时"), new KeyValuePair(9898, "一周(每周一)"), }; public static List UnitList = new List { 1000,100,10,1,0.1,0.01,0.001,0.0001 }; public static Dictionary LastUnitMap = new Dictionary { { 0,0.0001},{1,0.001},{2,0.01},{3,0.1},{4,1},{5,10},{6,100},{7,1000},{8,10000} }; public static List NumList = new List { //0,1,2,3,4,5,6,7,8,9,10 4,5,6,7,8,9,10 }; public static List IndList = new List { //0,1,2,3,4,5,6,7,8,9,10 1,2,3,4,5,6,7,8 }; public static List BrightList = new List { 0.8f,0.9f,1.0f,1.1f,1.2f,1.3f,1.4f,1.5f }; public string BcdToString(ushort[] registers) { StringBuilder sb = new StringBuilder(); foreach (ushort reg in registers) { sb.Append((reg >> 12) & 0xF); // 提取高位 BCD sb.Append((reg >> 8) & 0xF); sb.Append((reg >> 4) & 0xF); sb.Append(reg & 0xF); // 提取低位 BCD } return sb.ToString().TrimEnd('0'); // 去除填充的 0x00 } /// /// 将 ushort 转换为 BCD 码(Binary-Coded Decimal)。 /// /// 要转换的 ushort 值 (0~9999) /// 对应的 BCD 码 public static ushort UshortToBCD(ushort value) { if (value > 9999) throw new ArgumentOutOfRangeException(nameof(value), "BCD 范围只能是 0 ~ 9999"); ushort bcd = (ushort)( ((value / 1000) << 12) | // 千位 (((value / 100) % 10) << 8) | // 百位 (((value / 10) % 10) << 4) | // 十位 (value % 10) // 个位 ); return bcd; } /// /// 将 byte 转换为 BCD 码(Binary-Coded Decimal)。 /// /// 要转换的 byte 值 (0~99) /// 对应的 BCD 码 public static byte ByteToBCD(byte value) { if (value > 99) throw new ArgumentOutOfRangeException(nameof(value), "BCD 范围只能是 0 ~ 99"); byte bcd = (byte)(((value / 10) << 4) | (value % 10)); return bcd; } public static int GetBaudRate(int value) { return baudRateMap.TryGetValue(value, out int baudRate) ? baudRate : -1; } public static ushort GetBaudRateType(int baudrate) { return (ushort)baudRateMap.FirstOrDefault(x => x.Value == baudrate).Key; } public static string GetParityCheck(int value) { return parityMap.TryGetValue(value, out string result) ? result : "未知"; } public static string GetStopBits(int value) { return stopBitsMap.TryGetValue(value, out string result) ? result : "未知"; } public static string ByteArrayToHexString(byte[] byteArray) { return BitConverter.ToString(byteArray).Replace("-", " "); } public static string SNBcdToString(ushort[] registers) { StringBuilder sb = new StringBuilder(); foreach (ushort reg in registers) { sb.Append((reg >> 12) & 0xF); // 提取高位 BCD sb.Append((reg >> 8) & 0xF); sb.Append((reg >> 4) & 0xF); sb.Append(reg & 0xF); // 提取低位 BCD } return sb.ToString().TrimEnd('0'); // 去除填充的 0x00 } // 辅助方法:查找指定类型的祖先元素 public static T FindAncestor(DependencyObject dependencyObject) where T : DependencyObject { var parent = VisualTreeHelper.GetParent(dependencyObject); if (parent == null) return null; var parentT = parent as T; return parentT ?? FindAncestor(parent); } public static string GetResultTypeName(byte resultType) { switch (resultType) { case 0: return "0: 非水表"; case 1: return "1: 数字+指针表"; case 2: return "2: 全指针表"; case 3: return "3: 全数字表"; case 87: return "87: 没有配置基本信息"; case 88: return "88: 非水表或没有配置"; case 89: return "89: 角度变化"; case 90: return "90: 概率太低,结果不可靠"; case 91: return "91: 不符合最大水流"; case 92: return "92: 非递增或倒转"; } return $"{resultType}: 未知"; } public static bool TryParseFirmwareName(string fileName, out string versionString) { versionString = null; // 正则匹配:aimr_aa后跟6个数字,后缀是.bin var match = Regex.Match(fileName, @"^aimr_aa(\d{6})\.bin$", RegexOptions.IgnoreCase); if (!match.Success) return false; string digits = match.Groups[1].Value; // 提取出的6个数字 // 将6位数字分成三组,每组2位 string part1 = int.Parse(digits.Substring(0, 2)).ToString("D2"); string part2 = int.Parse(digits.Substring(2, 2)).ToString("D2"); string part3 = int.Parse(digits.Substring(4, 2)).ToString("D2"); versionString = $"{part1}.{part2}.{part3}"; return true; } public static bool IsFileSizeValid(string filePath, long maxSizeInBytes = 1 * 1024 * 1024) { if (!File.Exists(filePath)) return false; long length = new FileInfo(filePath).Length; return length <= maxSizeInBytes; } //---------------------------------------------------------- } //public enum BaudRate //{ // Baud_110 = 0, // 110 // Baud_300 = 1, // 300 // Baud_600 = 2, // 600 // Baud_1200 = 3, // 1200 // Baud_2400 = 4, // 2400 // Baud_4800 = 5, // 4800 // Baud_9600 = 6, // 9600 // Baud_14400 = 7, // 14400 // Baud_19200 = 8, // 19200 // Baud_38400 = 9, // 38400 // Baud_56000 = 10, // 56000 // Baud_57600 = 11, // 57600 // Baud_115200 = 12 // 115200 //} //public enum ParityCheck //{ // None = 0, // 无校验 // Odd = 1, // 奇校验 // Even = 2, // 偶校验 (默认) // Mark = 3, // 校验位为 1 // Space = 4 // 校验位为 0 //} ///// ///// 停止位设定 (40245, F4H) ///// 0 - 1 个停止位 (默认) ///// 1 - 2 个停止位 ///// //public enum StopBitsEnum //{ // One = 0, // 1 个停止位 (默认) // Two = 1 // 2 个停止位 //} //------------------------------------- }