From 1cc402450b2c632c7d21fb2ebfed0587651563b8 Mon Sep 17 00:00:00 2001 From: "GukSang.Jin" Date: Tue, 9 Jun 2026 16:19:06 +0800 Subject: [PATCH] gx --- .../HiddenSpeedSettingsViewModel.cs | 46 ++- .../HiddenSpeedSettingsWindow.xaml | 16 +- DentistryHandpieces/MainWindow.xaml | 75 +++-- DentistryHandpieces/MainWindowViewModel.cs | 56 +++- DentistryHandpieces/NumericKeypad.cs | 141 +++++++++ DentistryHandpieces/NumericKeypadWindow.xaml | 117 ++++++++ .../NumericKeypadWindow.xaml.cs | 284 ++++++++++++++++++ 7 files changed, 683 insertions(+), 52 deletions(-) create mode 100644 DentistryHandpieces/NumericKeypad.cs create mode 100644 DentistryHandpieces/NumericKeypadWindow.xaml create mode 100644 DentistryHandpieces/NumericKeypadWindow.xaml.cs diff --git a/DentistryHandpieces/HiddenSpeedSettingsViewModel.cs b/DentistryHandpieces/HiddenSpeedSettingsViewModel.cs index 77faefb..04e4ea8 100644 --- a/DentistryHandpieces/HiddenSpeedSettingsViewModel.cs +++ b/DentistryHandpieces/HiddenSpeedSettingsViewModel.cs @@ -102,9 +102,9 @@ public sealed class HiddenSpeedSettingsViewModel : ObservableObject return; } - if (!TryParseInt32(LoadSpeedSettingInput, out int loadSpeedSetting)) + if (!TryParseFloat(LoadSpeedSettingInput, out float loadSpeedSetting)) { - StatusText = "负载转速设置必须是有效的 32 位整数。"; + StatusText = "负载转速设置必须是有效数字。"; return; } @@ -112,9 +112,16 @@ public sealed class HiddenSpeedSettingsViewModel : ObservableObject StatusText = "正在保存负载转速设置..."; try { - await _plcRegisterService.WriteInt32Async(_plcConfig, LoadSpeedSettingRegister, loadSpeedSetting); - int confirmedValue = await _plcRegisterService.ReadInt32Async(_plcConfig, LoadSpeedSettingRegister); - LoadSpeedSettingInput = confirmedValue.ToString(CultureInfo.InvariantCulture); + await _plcRegisterService.WriteFloatAsync(_plcConfig, LoadSpeedSettingRegister, loadSpeedSetting); + float confirmedValue = await _plcRegisterService.ReadFloatAsync(_plcConfig, LoadSpeedSettingRegister); + if (float.IsNaN(confirmedValue) + || float.IsInfinity(confirmedValue) + || !AreEquivalentFloatRegisterValues(loadSpeedSetting, confirmedValue)) + { + throw new InvalidOperationException("PLC 写入后回读不一致。"); + } + + LoadSpeedSettingInput = FormatNumber(confirmedValue); StatusText = "负载转速设置已保存并回读确认。"; } catch (Exception ex) @@ -146,12 +153,14 @@ public sealed class HiddenSpeedSettingsViewModel : ObservableObject { await _plcRegisterService.WriteFloatAsync(_plcConfig, row.Address, value); float confirmedValue = await _plcRegisterService.ReadFloatAsync(_plcConfig, row.Address); - if (float.IsNaN(confirmedValue) || float.IsInfinity(confirmedValue)) + if (float.IsNaN(confirmedValue) + || float.IsInfinity(confirmedValue) + || !AreEquivalentFloatRegisterValues(value, confirmedValue)) { - throw new InvalidOperationException("PLC 回读值无效。"); + throw new InvalidOperationException("PLC 写入后回读不一致。"); } - row.ValueText = confirmedValue.ToString("0.########", CultureInfo.InvariantCulture); + row.ValueText = FormatNumber(confirmedValue); StatusText = $"{row.RangeText}已保存并回读确认。"; } catch (Exception ex) @@ -166,11 +175,16 @@ public sealed class HiddenSpeedSettingsViewModel : ObservableObject private async Task ReadValuesAsync() { - int loadSpeedSetting = await _plcRegisterService.ReadInt32Async(_plcConfig, LoadSpeedSettingRegister); + float loadSpeedSetting = await _plcRegisterService.ReadFloatAsync(_plcConfig, LoadSpeedSettingRegister); + if (float.IsNaN(loadSpeedSetting) || float.IsInfinity(loadSpeedSetting)) + { + throw new InvalidOperationException("负载转速设置读取无效。"); + } + ushort[] addresses = SpeedCoefficientSettings.Select(static row => row.Address).ToArray(); IReadOnlyDictionary values = await _plcRegisterService.ReadFloatValuesAsync(_plcConfig, addresses); - LoadSpeedSettingInput = loadSpeedSetting.ToString(CultureInfo.InvariantCulture); + LoadSpeedSettingInput = FormatNumber(loadSpeedSetting); foreach (SpeedCoefficientSettingRow row in SpeedCoefficientSettings) { if (!values.TryGetValue(row.Address, out float value) @@ -180,14 +194,13 @@ public sealed class HiddenSpeedSettingsViewModel : ObservableObject throw new InvalidOperationException($"{row.RangeText}读取无效。"); } - row.ValueText = value.ToString("0.########", CultureInfo.InvariantCulture); + row.ValueText = FormatNumber(value); } } - private static bool TryParseInt32(string input, out int value) + private static string FormatNumber(float value) { - return int.TryParse(input, NumberStyles.Integer, CultureInfo.CurrentCulture, out value) - || int.TryParse(input, NumberStyles.Integer, CultureInfo.InvariantCulture, out value); + return value.ToString("0.########", CultureInfo.InvariantCulture); } private static bool TryParseFloat(string input, out float value) @@ -196,4 +209,9 @@ public sealed class HiddenSpeedSettingsViewModel : ObservableObject || float.TryParse(input, NumberStyles.Float, CultureInfo.InvariantCulture, out value); return parsed && !float.IsNaN(value) && !float.IsInfinity(value); } + + private static bool AreEquivalentFloatRegisterValues(float left, float right) + { + return BitConverter.SingleToInt32Bits(left) == BitConverter.SingleToInt32Bits(right); + } } diff --git a/DentistryHandpieces/HiddenSpeedSettingsWindow.xaml b/DentistryHandpieces/HiddenSpeedSettingsWindow.xaml index 310a2b2..5420c67 100644 --- a/DentistryHandpieces/HiddenSpeedSettingsWindow.xaml +++ b/DentistryHandpieces/HiddenSpeedSettingsWindow.xaml @@ -1,6 +1,7 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +