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 { /// /// 检测输入的数据是否合理 /// /// /// /// /// 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); } /// /// 检测写的数据是否合理 /// /// /// /// /// 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); } } }