選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

83 行
2.8 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. namespace ModbusDemo.Uitls
  8. {
  9. public class CheckInputUitl
  10. {
  11. /// <summary>
  12. /// 检测输入的数据是否合理
  13. /// </summary>
  14. /// <param name="slaveAddressStr"></param>
  15. /// <param name="startAddressStr"></param>
  16. /// <param name="numberOfPointsStr"></param>
  17. /// <returns></returns>
  18. public static (bool success, byte slaveAddress, ushort startAddress, ushort numberOfPoints) ValidateReadParameters(
  19. string slaveAddressStr, string startAddressStr, string numberOfPointsStr)
  20. {
  21. if (!byte.TryParse(slaveAddressStr, out byte slaveAddress))
  22. {
  23. ShowMessage("SlaveAddress 格式无效");
  24. return (false, 0, 0, 0);
  25. }
  26. if (!ushort.TryParse(startAddressStr, out ushort startAddress))
  27. {
  28. ShowMessage("StartAddress 格式无效");
  29. return (false, 0, 0, 0);
  30. }
  31. if (!ushort.TryParse(numberOfPointsStr, out ushort numberOfPoints))
  32. {
  33. ShowMessage("NumberOfPoints 格式无效");
  34. return (false, 0, 0, 0);
  35. }
  36. return (true, slaveAddress, startAddress, numberOfPoints);
  37. }
  38. /// <summary>
  39. /// 检测写的数据是否合理
  40. /// </summary>
  41. /// <param name="slaveAddressStr"></param>
  42. /// <param name="startAddressStr"></param>
  43. /// <param name="WriteData"></param>
  44. /// <returns></returns>
  45. public static (bool success, byte slaveAddress, ushort startAddress) ValidateWriteParameters(
  46. string slaveAddressStr, string startAddressStr, string WriteData)
  47. {
  48. if (!byte.TryParse(slaveAddressStr, out byte slaveAddress))
  49. {
  50. ShowMessage("SlaveAddress 格式无效");
  51. return (false, 0, 0);
  52. }
  53. if (!ushort.TryParse(startAddressStr, out ushort startAddress))
  54. {
  55. ShowMessage("StartAddress 格式无效");
  56. return (false, 0, 0);
  57. }
  58. if (string.IsNullOrEmpty(WriteData))
  59. {
  60. ShowMessage("WriteData 格式无效");
  61. return (false, 0, 0);
  62. }
  63. return (true, slaveAddress, startAddress);
  64. }
  65. private static void ShowMessage(string message, string title = "warn",
  66. MessageBoxImage image = MessageBoxImage.Warning)
  67. {
  68. MessageBox.Show(message, title, MessageBoxButton.OK, image);
  69. }
  70. }
  71. }