123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345 |
- 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<int, int> baudRateMap = new Dictionary<int, int>
- {
- { 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<int, string> parityMap = new Dictionary<int, string>
- {
- { 0, "无校验" }, { 1, "奇校验" }, { 2, "偶校验" },
- { 3, "校验位为 1" }, { 4, "校验位为 0" }
- };
- private static Dictionary<int, string> stopBitsMap = new Dictionary<int, string>
- {
- { 1, "1 个停止位" }, { 2, "2 个停止位" }
- };
- //水表类型
- public static List<KeyValuePair<byte, string>> MeterTypeList =
- new List<KeyValuePair<byte, string>>()
- {
- //new KeyValuePair<int, string>(0,"0 - 未知"),
- new KeyValuePair<byte, string>(1, "1 - 数字+指针"),
- new KeyValuePair<byte, string>(3, "3 - 全数字"),
- new KeyValuePair<byte, string>(2, "2 - 全指针")
- };
- //红色指针读数上传标志
- public static List<KeyValuePair<byte, string>> UploadRedindList =
- new List<KeyValuePair<byte, string>>()
- {
- new KeyValuePair<byte, string>(0, "不上传"),
- new KeyValuePair<byte, string>(1, "上传")
- };
- public static List<KeyValuePair<ushort, string>> FlowRateList =
- new List<KeyValuePair<ushort, string>>()
- {
- new KeyValuePair<ushort, string>(3, "DN15 - 3"),
- new KeyValuePair<ushort, string>(5, "DN20 - 5"),
- new KeyValuePair<ushort, string>(8, "DN25 - 8"),
- new KeyValuePair<ushort, string>(12, "DN32 - 12"),
- new KeyValuePair<ushort, string>(20, "DN40 - 20"),
- new KeyValuePair<ushort, string>(32, "DN50 - 32"),
- new KeyValuePair<ushort, string>(60, "DN65 - 60"),
- new KeyValuePair<ushort, string>(79, "DN80 - 79"),
- new KeyValuePair<ushort, string>(125, "DN100 - 125"),
- new KeyValuePair<ushort, string>(200, "DN125 - 200"),
- new KeyValuePair<ushort, string>(312, "DN150 - 312"),
- new KeyValuePair<ushort, string>(500, "DN200 - 500"),
- new KeyValuePair<ushort, string>(787, "DN250 - 787"),
- new KeyValuePair<ushort, string>(1250, "DN300 - 1250"),
- new KeyValuePair<ushort, string>(2000, "DN400 - 2000"),
- new KeyValuePair<ushort, string>(3125, "DN500 - 3125"),
- new KeyValuePair<ushort, string>(5000, "DN600 - 5000"),
- new KeyValuePair<ushort, string>(7875, "DN800 - 7875"),
- };
- public static List<KeyValuePair<ushort, string>> DnValueList =
- new List<KeyValuePair<ushort, string>>()
- {
- new KeyValuePair<ushort, string>(3, "DN15"),
- new KeyValuePair<ushort, string>(5, "DN20"),
- new KeyValuePair<ushort, string>(8, "DN25"),
- new KeyValuePair<ushort, string>(12, "DN32"),
- new KeyValuePair<ushort, string>(20, "DN40"),
- new KeyValuePair<ushort, string>(32, "DN50"),
- new KeyValuePair<ushort, string>(60, "DN65"),
- new KeyValuePair<ushort, string>(79, "DN80"),
- new KeyValuePair<ushort, string>(125, "DN100"),
- new KeyValuePair<ushort, string>(200, "DN125"),
- new KeyValuePair<ushort, string>(312, "DN150"),
- new KeyValuePair<ushort, string>(500, "DN200"),
- new KeyValuePair<ushort, string>(787, "DN250"),
- new KeyValuePair<ushort, string>(1250, "DN300"),
- new KeyValuePair<ushort, string>(2000, "DN400"),
- new KeyValuePair<ushort, string>(3125, "DN500"),
- new KeyValuePair<ushort, string>(5000, "DN600"),
- new KeyValuePair<ushort, string>(7875, "DN800"),
- };
- public static List<KeyValuePair<ushort, string>> SampleIntervalList =
- new List<KeyValuePair<ushort, string>>()
- {
- //new KeyValuePair<ushort, string>(1, "1分钟"),
- new KeyValuePair<ushort, string>(2, "2分钟"),
- //new KeyValuePair<ushort, string>(3, "3分钟"),
- new KeyValuePair<ushort, string>(5, "5分钟"),
- new KeyValuePair<ushort, string>(10, "10分钟"),
- new KeyValuePair<ushort, string>(30, "30分钟"),
- new KeyValuePair<ushort, string>(60, "1小时"),
- new KeyValuePair<ushort, string>(120, "2小时"),
- new KeyValuePair<ushort, string>(180, "3小时"),
- new KeyValuePair<ushort, string>(240, "4小时"),
- new KeyValuePair<ushort, string>(360, "6小时"),
- new KeyValuePair<ushort, string>(480, "8小时"),
- new KeyValuePair<ushort, string>(720, "12小时"),
- new KeyValuePair<ushort, string>(1440, "24小时"),
- new KeyValuePair<ushort, string>(9898, "一周(每周一)"),
- };
- public static List<double> UnitList = new List<double>
- {
- 1000,100,10,1,0.1,0.01,0.001,0.0001
- };
- public static Dictionary<byte, double> LastUnitMap = new Dictionary<byte, double>
- {
- { 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<byte> NumList = new List<byte>
- {
- //0,1,2,3,4,5,6,7,8,9,10
- 4,5,6,7,8,9,10
- };
- public static List<byte> IndList = new List<byte>
- {
- //0,1,2,3,4,5,6,7,8,9,10
- 1,2,3,4,5,6,7,8
- };
- public static List<float> BrightList = new List<float>
- {
- 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
- }
- /// <summary>
- /// 将 ushort 转换为 BCD 码(Binary-Coded Decimal)。
- /// </summary>
- /// <param name="value">要转换的 ushort 值 (0~9999)</param>
- /// <returns>对应的 BCD 码</returns>
- 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;
- }
- /// <summary>
- /// 将 byte 转换为 BCD 码(Binary-Coded Decimal)。
- /// </summary>
- /// <param name="value">要转换的 byte 值 (0~99)</param>
- /// <returns>对应的 BCD 码</returns>
- 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<T>(DependencyObject dependencyObject) where T : DependencyObject
- {
- var parent = VisualTreeHelper.GetParent(dependencyObject);
- if (parent == null) return null;
- var parentT = parent as T;
- return parentT ?? FindAncestor<T>(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
- //}
- ///// <summary>
- ///// 停止位设定 (40245, F4H)
- ///// 0 - 1 个停止位 (默认)
- ///// 1 - 2 个停止位
- ///// </summary>
- //public enum StopBitsEnum
- //{
- // One = 0, // 1 个停止位 (默认)
- // Two = 1 // 2 个停止位
- //}
- //-------------------------------------
- }
|