|
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
-
- namespace ModbusDemo.Uitls
- {
- public class CheckInputUitl
- {
- /// <summary>
- /// 检测输入的数据是否合理
- /// </summary>
- /// <param name="slaveAddressStr"></param>
- /// <param name="startAddressStr"></param>
- /// <param name="numberOfPointsStr"></param>
- /// <returns></returns>
- public static (bool success, byte slaveAddress, ushort startAddress, ushort numberOfPoints) ValidateReadParameters(
- string slaveAddressStr, string startAddressStr, string numberOfPointsStr)
- {
- if (!byte.TryParse(slaveAddressStr, out byte slaveAddress))
- {
- ShowMessage("SlaveAddress 格式无效");
- return (false, 0, 0, 0);
- }
-
- if (!ushort.TryParse(startAddressStr, out ushort startAddress))
- {
- ShowMessage("StartAddress 格式无效");
- return (false, 0, 0, 0);
- }
-
- if (!ushort.TryParse(numberOfPointsStr, out ushort numberOfPoints))
- {
- ShowMessage("NumberOfPoints 格式无效");
- return (false, 0, 0, 0);
- }
-
- return (true, slaveAddress, startAddress, numberOfPoints);
- }
- /// <summary>
- /// 检测写的数据是否合理
- /// </summary>
- /// <param name="slaveAddressStr"></param>
- /// <param name="startAddressStr"></param>
- /// <param name="WriteData"></param>
- /// <returns></returns>
- public static (bool success, byte slaveAddress, ushort startAddress) ValidateWriteParameters(
- string slaveAddressStr, string startAddressStr, string WriteData)
- {
-
- if (!byte.TryParse(slaveAddressStr, out byte slaveAddress))
- {
- ShowMessage("SlaveAddress 格式无效");
- return (false, 0, 0);
- }
-
- if (!ushort.TryParse(startAddressStr, out ushort startAddress))
- {
- ShowMessage("StartAddress 格式无效");
- return (false, 0, 0);
- }
-
- if (string.IsNullOrEmpty(WriteData))
- {
- ShowMessage("WriteData 格式无效");
- return (false, 0, 0);
- }
-
- return (true, slaveAddress, startAddress);
- }
-
-
-
- private static void ShowMessage(string message, string title = "warn",
- MessageBoxImage image = MessageBoxImage.Warning)
- {
- MessageBox.Show(message, title, MessageBoxButton.OK, image);
- }
- }
- }
|