MainWindow.Motor.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using System;
  2. using System.Threading.Tasks;
  3. using System.Windows;
  4. using System.Windows.Media;
  5. namespace AutoFocusTool
  6. {
  7. // 电机控制:Z对焦轴 / 水平轴 / 位置刷新
  8. public partial class MainWindow
  9. {
  10. /// <summary>串口操作统一包装:置忙、后台执行、完成后刷新位置。</summary>
  11. private void MotorAction(string name, Func<bool> action)
  12. {
  13. if (_motor == null) return;
  14. if (_busy) { Log("串口忙,请稍候。"); return; }
  15. int motorDelay = ParseInt2(TxtMotorDelay.Text, 1500); // UI线程先读
  16. Task.Run(() =>
  17. {
  18. _busy = true;
  19. try
  20. {
  21. if (_motor != null) _motor.MotorDelayMs = motorDelay;
  22. bool ok = action();
  23. Log($"{name} {(ok ? "完成" : "失败")}");
  24. }
  25. catch (Exception ex) { Log($"{name} 异常: {ex.Message}"); }
  26. finally { _busy = false; }
  27. RefreshPositions();
  28. });
  29. }
  30. // ── Z 对焦轴 ──
  31. private void BtnZUp_Click(object sender, RoutedEventArgs e)
  32. {
  33. int step = ParseInt2(TxtZStep.Text, 128);
  34. LogAction($"点击 Z正转, 步距={step}");
  35. MotorAction($"Z正转 {step}步", () => _motor.VerticalForward(step));
  36. }
  37. private void BtnZDown_Click(object sender, RoutedEventArgs e)
  38. {
  39. int step = ParseInt2(TxtZStep.Text, 128);
  40. LogAction($"点击 Z反转, 步距={step}");
  41. MotorAction($"Z反转 {step}步", () => _motor.VerticalBackward(step));
  42. }
  43. private void BtnZReset_Click(object sender, RoutedEventArgs e)
  44. {
  45. LogAction("点击 Z复位");
  46. MotorAction("Z复位", () => _motor.VerticalReset());
  47. }
  48. private void BtnZMoveTo_Click(object sender, RoutedEventArgs e)
  49. {
  50. int abs = ParseInt2(TxtZAbs.Text, 0);
  51. LogAction($"点击 Z绝对移动, 目标={abs}");
  52. MotorAction($"Z绝对移动→{abs}", () => _motor.VerticalMoveTo(abs));
  53. }
  54. // ── 水平轴 ──
  55. private void BtnHFwd_Click(object sender, RoutedEventArgs e)
  56. {
  57. int step = ParseInt2(TxtHStep.Text, 100);
  58. LogAction($"点击 水平正转, 步距={step}");
  59. MotorAction($"水平正转 {step}步", () => _motor.HorizontalForward(step));
  60. }
  61. private void BtnHBwd_Click(object sender, RoutedEventArgs e)
  62. {
  63. int step = ParseInt2(TxtHStep.Text, 100);
  64. LogAction($"点击 水平反转, 步距={step}");
  65. MotorAction($"水平反转 {step}步", () => _motor.HorizontalBackward(step));
  66. }
  67. private void BtnHReset_Click(object sender, RoutedEventArgs e)
  68. {
  69. LogAction("点击 水平复位");
  70. MotorAction("水平复位", () => _motor.HorizontalReset());
  71. }
  72. // ── 读位置并显示 ──
  73. private void RefreshPositions()
  74. {
  75. if (_motor == null) return;
  76. Task.Run(() =>
  77. {
  78. if (_busy) return;
  79. _busy = true;
  80. int z = -1, hpos = -1;
  81. try
  82. {
  83. z = _motor.ReadVerticalPosition();
  84. hpos = _motor.ReadHorizontalPosition();
  85. }
  86. catch (Exception ex) { Log($"读位置异常: {ex.Message}"); }
  87. finally { _busy = false; }
  88. _lastZ = z; _lastH = hpos;
  89. Dispatcher.Invoke(() => { TxtZ.Text = $"Z: {z} 水平: {hpos}"; UpdateParamDisplay(); });
  90. });
  91. }
  92. private static int ParseInt2(string s, int def)
  93. => int.TryParse(s?.Trim(), out int v) ? v : def;
  94. }
  95. }