MainWindow.Motor.cs 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. MotorAction($"Z正转 {step}步", () => _motor.VerticalForward(step));
  35. }
  36. private void BtnZDown_Click(object sender, RoutedEventArgs e)
  37. {
  38. int step = ParseInt2(TxtZStep.Text, 128);
  39. MotorAction($"Z反转 {step}步", () => _motor.VerticalBackward(step));
  40. }
  41. private void BtnZReset_Click(object sender, RoutedEventArgs e)
  42. => MotorAction("Z复位", () => _motor.VerticalReset());
  43. private void BtnZMoveTo_Click(object sender, RoutedEventArgs e)
  44. {
  45. int abs = ParseInt2(TxtZAbs.Text, 0);
  46. MotorAction($"Z绝对移动→{abs}", () => _motor.VerticalMoveTo(abs));
  47. }
  48. // ── 水平轴 ──
  49. private void BtnHFwd_Click(object sender, RoutedEventArgs e)
  50. {
  51. int step = ParseInt2(TxtHStep.Text, 100);
  52. MotorAction($"水平正转 {step}步", () => _motor.HorizontalForward(step));
  53. }
  54. private void BtnHBwd_Click(object sender, RoutedEventArgs e)
  55. {
  56. int step = ParseInt2(TxtHStep.Text, 100);
  57. MotorAction($"水平反转 {step}步", () => _motor.HorizontalBackward(step));
  58. }
  59. private void BtnHReset_Click(object sender, RoutedEventArgs e)
  60. => MotorAction("水平复位", () => _motor.HorizontalReset());
  61. // ── 读位置并显示 ──
  62. private void RefreshPositions()
  63. {
  64. if (_motor == null) return;
  65. Task.Run(() =>
  66. {
  67. if (_busy) return;
  68. _busy = true;
  69. int z = -1, hpos = -1;
  70. try
  71. {
  72. z = _motor.ReadVerticalPosition();
  73. hpos = _motor.ReadHorizontalPosition();
  74. }
  75. catch (Exception ex) { Log($"读位置异常: {ex.Message}"); }
  76. finally { _busy = false; }
  77. _lastZ = z; _lastH = hpos;
  78. Dispatcher.Invoke(() => { TxtZ.Text = $"Z: {z} 水平: {hpos}"; UpdateParamDisplay(); });
  79. });
  80. }
  81. private static int ParseInt2(string s, int def)
  82. => int.TryParse(s?.Trim(), out int v) ? v : def;
  83. }
  84. }