Tools.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.IO.Ports;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Text.RegularExpressions;
  8. using System.Threading.Tasks;
  9. using System.Windows;
  10. using System.Windows.Media;
  11. namespace MV485.helper
  12. {
  13. public class Tools
  14. {
  15. private static Dictionary<int, int> baudRateMap = new Dictionary<int, int>
  16. {
  17. { 0, 110 }, { 1, 300 }, { 2, 600 }, { 3, 1200 },
  18. { 4, 2400 }, { 5, 4800 }, { 6, 9600 }, { 7, 14400 },
  19. { 8, 19200 }, { 9, 38400 }, { 10, 56000 }, { 11, 57600 },
  20. { 12, 115200 }
  21. };
  22. private static Dictionary<int, string> parityMap = new Dictionary<int, string>
  23. {
  24. { 0, "无校验" }, { 1, "奇校验" }, { 2, "偶校验" },
  25. { 3, "校验位为 1" }, { 4, "校验位为 0" }
  26. };
  27. private static Dictionary<int, string> stopBitsMap = new Dictionary<int, string>
  28. {
  29. { 1, "1 个停止位" }, { 2, "2 个停止位" }
  30. };
  31. //水表类型
  32. public static List<KeyValuePair<byte, string>> MeterTypeList =
  33. new List<KeyValuePair<byte, string>>()
  34. {
  35. new KeyValuePair<byte, string>(0,"0 - 非水表"),
  36. new KeyValuePair<byte, string>(1, "1 - 数字+指针"),
  37. new KeyValuePair<byte, string>(3, "3 - 全数字"),
  38. new KeyValuePair<byte, string>(2, "2 - 全指针")
  39. };
  40. //红色指针读数上传标志
  41. public static List<KeyValuePair<byte, string>> UploadRedindList =
  42. new List<KeyValuePair<byte, string>>()
  43. {
  44. new KeyValuePair<byte, string>(0, "不上传"),
  45. new KeyValuePair<byte, string>(1, "上传")
  46. };
  47. public static List<KeyValuePair<ushort, string>> FlowRateList =
  48. new List<KeyValuePair<ushort, string>>()
  49. {
  50. new KeyValuePair<ushort, string>(3, "DN15 - 3"),
  51. new KeyValuePair<ushort, string>(5, "DN20 - 5"),
  52. new KeyValuePair<ushort, string>(8, "DN25 - 8"),
  53. new KeyValuePair<ushort, string>(12, "DN32 - 12"),
  54. new KeyValuePair<ushort, string>(20, "DN40 - 20"),
  55. new KeyValuePair<ushort, string>(32, "DN50 - 32"),
  56. new KeyValuePair<ushort, string>(60, "DN65 - 60"),
  57. new KeyValuePair<ushort, string>(79, "DN80 - 79"),
  58. new KeyValuePair<ushort, string>(125, "DN100 - 125"),
  59. new KeyValuePair<ushort, string>(200, "DN125 - 200"),
  60. new KeyValuePair<ushort, string>(312, "DN150 - 312"),
  61. new KeyValuePair<ushort, string>(500, "DN200 - 500"),
  62. new KeyValuePair<ushort, string>(787, "DN250 - 787"),
  63. new KeyValuePair<ushort, string>(1250, "DN300 - 1250"),
  64. new KeyValuePair<ushort, string>(2000, "DN400 - 2000"),
  65. new KeyValuePair<ushort, string>(3125, "DN500 - 3125"),
  66. new KeyValuePair<ushort, string>(5000, "DN600 - 5000"),
  67. new KeyValuePair<ushort, string>(7875, "DN800 - 7875"),
  68. };
  69. public static List<KeyValuePair<ushort, string>> DnValueList =
  70. new List<KeyValuePair<ushort, string>>()
  71. {
  72. new KeyValuePair<ushort, string>(3, "DN15"),
  73. new KeyValuePair<ushort, string>(5, "DN20"),
  74. new KeyValuePair<ushort, string>(8, "DN25"),
  75. new KeyValuePair<ushort, string>(12, "DN32"),
  76. new KeyValuePair<ushort, string>(20, "DN40"),
  77. new KeyValuePair<ushort, string>(32, "DN50"),
  78. new KeyValuePair<ushort, string>(60, "DN65"),
  79. new KeyValuePair<ushort, string>(79, "DN80"),
  80. new KeyValuePair<ushort, string>(125, "DN100"),
  81. new KeyValuePair<ushort, string>(200, "DN125"),
  82. new KeyValuePair<ushort, string>(312, "DN150"),
  83. new KeyValuePair<ushort, string>(500, "DN200"),
  84. new KeyValuePair<ushort, string>(787, "DN250"),
  85. new KeyValuePair<ushort, string>(1250, "DN300"),
  86. new KeyValuePair<ushort, string>(2000, "DN400"),
  87. new KeyValuePair<ushort, string>(3125, "DN500"),
  88. new KeyValuePair<ushort, string>(5000, "DN600"),
  89. new KeyValuePair<ushort, string>(7875, "DN800"),
  90. };
  91. public static List<KeyValuePair<ushort, string>> SampleIntervalList =
  92. new List<KeyValuePair<ushort, string>>()
  93. {
  94. //new KeyValuePair<ushort, string>(1, "1分钟"),
  95. new KeyValuePair<ushort, string>(2, "2分钟"),
  96. //new KeyValuePair<ushort, string>(3, "3分钟"),
  97. new KeyValuePair<ushort, string>(5, "5分钟"),
  98. new KeyValuePair<ushort, string>(10, "10分钟"),
  99. new KeyValuePair<ushort, string>(30, "30分钟"),
  100. new KeyValuePair<ushort, string>(60, "1小时"),
  101. new KeyValuePair<ushort, string>(120, "2小时"),
  102. new KeyValuePair<ushort, string>(180, "3小时"),
  103. new KeyValuePair<ushort, string>(240, "4小时"),
  104. new KeyValuePair<ushort, string>(360, "6小时"),
  105. new KeyValuePair<ushort, string>(480, "8小时"),
  106. new KeyValuePair<ushort, string>(720, "12小时"),
  107. new KeyValuePair<ushort, string>(1440, "24小时"),
  108. new KeyValuePair<ushort, string>(9898, "一周(每周一)"),
  109. };
  110. public static List<double> UnitList = new List<double>
  111. {
  112. 1000,100,10,1,0.1,0.01,0.001,0.0001
  113. };
  114. public static Dictionary<byte, double> LastUnitMap = new Dictionary<byte, double>
  115. {
  116. { 0,0.0001},{1,0.001},{2,0.01},{3,0.1},{4,1},{5,10},{6,100},{7,1000},{8,10000}
  117. };
  118. public static List<byte> NumList = new List<byte>
  119. {
  120. //0,1,2,3,4,5,6,7,8,9,10
  121. 4,5,6,7,8,9,10
  122. };
  123. public static List<byte> IndList = new List<byte>
  124. {
  125. //0,1,2,3,4,5,6,7,8,9,10
  126. 1,2,3,4,5,6,7,8
  127. };
  128. public static List<float> BrightList = new List<float>
  129. {
  130. 0.8f,0.9f,1.0f,1.1f,1.2f,1.3f,1.4f,1.5f
  131. };
  132. public string BcdToString(ushort[] registers)
  133. {
  134. StringBuilder sb = new StringBuilder();
  135. foreach (ushort reg in registers)
  136. {
  137. sb.Append((reg >> 12) & 0xF); // 提取高位 BCD
  138. sb.Append((reg >> 8) & 0xF);
  139. sb.Append((reg >> 4) & 0xF);
  140. sb.Append(reg & 0xF); // 提取低位 BCD
  141. }
  142. return sb.ToString().TrimEnd('0'); // 去除填充的 0x00
  143. }
  144. /// <summary>
  145. /// 将 ushort 转换为 BCD 码(Binary-Coded Decimal)。
  146. /// </summary>
  147. /// <param name="value">要转换的 ushort 值 (0~9999)</param>
  148. /// <returns>对应的 BCD 码</returns>
  149. public static ushort UshortToBCD(ushort value)
  150. {
  151. if (value > 9999)
  152. throw new ArgumentOutOfRangeException(nameof(value), "BCD 范围只能是 0 ~ 9999");
  153. ushort bcd = (ushort)(
  154. ((value / 1000) << 12) | // 千位
  155. (((value / 100) % 10) << 8) | // 百位
  156. (((value / 10) % 10) << 4) | // 十位
  157. (value % 10) // 个位
  158. );
  159. return bcd;
  160. }
  161. /// <summary>
  162. /// 将 byte 转换为 BCD 码(Binary-Coded Decimal)。
  163. /// </summary>
  164. /// <param name="value">要转换的 byte 值 (0~99)</param>
  165. /// <returns>对应的 BCD 码</returns>
  166. public static byte ByteToBCD(byte value)
  167. {
  168. if (value > 99)
  169. throw new ArgumentOutOfRangeException(nameof(value), "BCD 范围只能是 0 ~ 99");
  170. byte bcd = (byte)(((value / 10) << 4) | (value % 10));
  171. return bcd;
  172. }
  173. public static int GetBaudRate(int value)
  174. {
  175. return baudRateMap.TryGetValue(value, out int baudRate) ? baudRate : -1;
  176. }
  177. public static ushort GetBaudRateType(int baudrate)
  178. {
  179. return (ushort)baudRateMap.FirstOrDefault(x => x.Value == baudrate).Key;
  180. }
  181. public static string GetParityCheck(int value)
  182. {
  183. return parityMap.TryGetValue(value, out string result) ? result : "未知";
  184. }
  185. public static string GetStopBits(int value)
  186. {
  187. return stopBitsMap.TryGetValue(value, out string result) ? result : "未知";
  188. }
  189. public static string ByteArrayToHexString(byte[] byteArray)
  190. {
  191. return BitConverter.ToString(byteArray).Replace("-", " ");
  192. }
  193. public static string SNBcdToString(ushort[] registers)
  194. {
  195. StringBuilder sb = new StringBuilder();
  196. foreach (ushort reg in registers)
  197. {
  198. sb.Append((reg >> 12) & 0xF); // 提取高位 BCD
  199. sb.Append((reg >> 8) & 0xF);
  200. sb.Append((reg >> 4) & 0xF);
  201. sb.Append(reg & 0xF); // 提取低位 BCD
  202. }
  203. return sb.ToString().TrimEnd('0'); // 去除填充的 0x00
  204. }
  205. // 辅助方法:查找指定类型的祖先元素
  206. public static T FindAncestor<T>(DependencyObject dependencyObject) where T : DependencyObject
  207. {
  208. var parent = VisualTreeHelper.GetParent(dependencyObject);
  209. if (parent == null) return null;
  210. var parentT = parent as T;
  211. return parentT ?? FindAncestor<T>(parent);
  212. }
  213. public static string GetResultTypeName(byte resultType)
  214. {
  215. switch (resultType)
  216. {
  217. case 0:
  218. return "0: 非水表";
  219. case 1:
  220. return "1: 数字+指针表";
  221. case 2:
  222. return "2: 全指针表";
  223. case 3:
  224. return "3: 全数字表";
  225. case 87:
  226. return "87: 没有配置基本信息";
  227. case 88:
  228. return "88: 非水表或没有配置";
  229. case 89:
  230. return "89: 角度变化";
  231. case 90:
  232. return "90: 概率太低,结果不可靠";
  233. case 91:
  234. return "91: 不符合最大水流";
  235. case 92:
  236. return "92: 非递增或倒转";
  237. }
  238. return $"{resultType}: 未知";
  239. }
  240. public static bool TryParseFirmwareName(string fileName, out string versionString)
  241. {
  242. versionString = null;
  243. // 正则匹配:aimr_aa后跟6个数字,后缀是.bin
  244. var match = Regex.Match(fileName, @"^aimr_aa(\d{6})\.bin$", RegexOptions.IgnoreCase);
  245. if (!match.Success)
  246. return false;
  247. string digits = match.Groups[1].Value; // 提取出的6个数字
  248. // 将6位数字分成三组,每组2位
  249. string part1 = int.Parse(digits.Substring(0, 2)).ToString("D2");
  250. string part2 = int.Parse(digits.Substring(2, 2)).ToString("D2");
  251. string part3 = int.Parse(digits.Substring(4, 2)).ToString("D2");
  252. versionString = $"{part1}.{part2}.{part3}";
  253. return true;
  254. }
  255. public static bool IsFileSizeValid(string filePath, long maxSizeInBytes = 1 * 1024 * 1024)
  256. {
  257. if (!File.Exists(filePath))
  258. return false;
  259. long length = new FileInfo(filePath).Length;
  260. return length <= maxSizeInBytes;
  261. }
  262. //----------------------------------------------------------
  263. }
  264. //public enum BaudRate
  265. //{
  266. // Baud_110 = 0, // 110
  267. // Baud_300 = 1, // 300
  268. // Baud_600 = 2, // 600
  269. // Baud_1200 = 3, // 1200
  270. // Baud_2400 = 4, // 2400
  271. // Baud_4800 = 5, // 4800
  272. // Baud_9600 = 6, // 9600
  273. // Baud_14400 = 7, // 14400
  274. // Baud_19200 = 8, // 19200
  275. // Baud_38400 = 9, // 38400
  276. // Baud_56000 = 10, // 56000
  277. // Baud_57600 = 11, // 57600
  278. // Baud_115200 = 12 // 115200
  279. //}
  280. //public enum ParityCheck
  281. //{
  282. // None = 0, // 无校验
  283. // Odd = 1, // 奇校验
  284. // Even = 2, // 偶校验 (默认)
  285. // Mark = 3, // 校验位为 1
  286. // Space = 4 // 校验位为 0
  287. //}
  288. ///// <summary>
  289. ///// 停止位设定 (40245, F4H)
  290. ///// 0 - 1 个停止位 (默认)
  291. ///// 1 - 2 个停止位
  292. ///// </summary>
  293. //public enum StopBitsEnum
  294. //{
  295. // One = 0, // 1 个停止位 (默认)
  296. // Two = 1 // 2 个停止位
  297. //}
  298. //-------------------------------------
  299. }