MainWindow.xaml.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Media;
  9. using System.Windows.Shapes;
  10. using AutoFocusTool.Devices;
  11. using AutoFocusTool.Imaging;
  12. using AutoFocusTool.Serial;
  13. using SerialCamera = AutoFocusTool.Camera.Camera;
  14. namespace AutoFocusTool
  15. {
  16. public partial class MainWindow : Window
  17. {
  18. private readonly DeviceScanner _scanner = new DeviceScanner();
  19. private List<HouseDevice> _houses = new List<HouseDevice>();
  20. private SerialCamera _camera; // 当前连接的相机
  21. private HouseMotor _motor; // 当前连接的串口马达+光源
  22. private HouseDevice _current; // 当前舱室
  23. private CancellationTokenSource _liveCts; // 实时预览
  24. private CancellationTokenSource _scanCts; // Z扫描
  25. private volatile bool _busy; // 串口忙(避免并发命令)
  26. private int _lastZ = -1, _lastH = -1, _curWell = -1; // 最近回读的Z/水平位置、当前well(供参数显示)
  27. private int _camWidth = 2592, _camHeight = 1944; // MVC2000 原生分辨率(参考图实测),勿用1600x1200否则画面被裁中央
  28. private List<(int z, double score)> _curve = new List<(int, double)>();
  29. public MainWindow()
  30. {
  31. InitializeComponent();
  32. AutoFocusTool.Logging.FileLogger.Init();
  33. _scanner.Log = Log;
  34. Closing += (s, e) => { AutoFocusTool.Logging.FileLogger.Info("APP", "程序关闭"); Cleanup(); };
  35. Log("程序启动。先【扫描设备】,选舱室后【连接】。");
  36. }
  37. // ───────────────────────── 日志 ─────────────────────────
  38. private void Log(string msg)
  39. {
  40. AutoFocusTool.Logging.FileLogger.Info("RUN", msg);
  41. void Append()
  42. {
  43. string line = $"{DateTime.Now:HH:mm:ss} {msg}\n";
  44. TxtLog.AppendText(line);
  45. TxtLog.ScrollToEnd();
  46. }
  47. if (Dispatcher.CheckAccess()) Append();
  48. else Dispatcher.BeginInvoke((Action)Append);
  49. }
  50. /// <summary>记录界面操作(ACTION级落盘 + 界面日志)。</summary>
  51. private void LogAction(string msg)
  52. {
  53. AutoFocusTool.Logging.FileLogger.Action("UI", msg);
  54. void Append()
  55. {
  56. string line = $"{DateTime.Now:HH:mm:ss} ▶ {msg}\n";
  57. TxtLog.AppendText(line);
  58. TxtLog.ScrollToEnd();
  59. }
  60. if (Dispatcher.CheckAccess()) Append();
  61. else Dispatcher.BeginInvoke((Action)Append);
  62. }
  63. // ───────────────────────── 扫描设备 ─────────────────────────
  64. private async void BtnScan_Click(object sender, RoutedEventArgs e)
  65. {
  66. LogAction("点击 扫描设备");
  67. BtnScan.IsEnabled = false;
  68. Log("开始扫描设备(枚举相机 + 扫串口握手)...");
  69. try
  70. {
  71. var houses = await Task.Run(() => _scanner.ScanAll());
  72. _houses = houses;
  73. CmbHouse.Items.Clear();
  74. foreach (var h in houses)
  75. CmbHouse.Items.Add(h.ToString());
  76. if (houses.Count > 0)
  77. {
  78. CmbHouse.SelectedIndex = 0;
  79. Log($"扫描完成,发现 {houses.Count} 个舱室。");
  80. }
  81. else
  82. {
  83. Log("未发现舱室。检查串口连接/相机USB/驱动。");
  84. }
  85. }
  86. catch (Exception ex)
  87. {
  88. Log($"扫描异常: {ex.Message}");
  89. }
  90. finally
  91. {
  92. BtnScan.IsEnabled = true;
  93. }
  94. }
  95. // ───────────────────────── 连接 / 断开 ─────────────────────────
  96. private void BtnConnect_Click(object sender, RoutedEventArgs e)
  97. {
  98. LogAction($"点击 连接 舱室索引={CmbHouse.SelectedIndex}");
  99. int idx = CmbHouse.SelectedIndex;
  100. if (idx < 0 || idx >= _houses.Count)
  101. {
  102. Log("请先扫描并选择一个舱室。");
  103. return;
  104. }
  105. var h = _houses[idx];
  106. try
  107. {
  108. // 串口
  109. _motor = new HouseMotor(h.PortName) { Log = Log };
  110. if (!_motor.Open())
  111. {
  112. Log($"串口 {h.PortName} 打开失败。");
  113. _motor = null;
  114. return;
  115. }
  116. _motor.MotorDelayMs = ParseInt(TxtMotorDelay.Text, 1500);
  117. // 相机(若配对到)
  118. if (h.HasCamera)
  119. {
  120. _camera = new SerialCamera(h.CcdIndex, _camWidth, _camHeight, ParseInt(TxtExposure.Text, 400));
  121. int init = _camera.Init(
  122. (byte)ParseInt(TxtGainR.Text, 25),
  123. (byte)ParseInt(TxtGainG.Text, 14),
  124. (byte)ParseInt(TxtGainB.Text, 25));
  125. if (init == 0)
  126. {
  127. _camera.SetOpMode(0); // 拍照模式
  128. Log($"相机#{h.CcdIndex} 已连接。");
  129. }
  130. else
  131. {
  132. Log($"相机#{h.CcdIndex} 初始化失败(code={init}),仅串口可用。");
  133. _camera.Dispose();
  134. _camera = null;
  135. }
  136. }
  137. else
  138. {
  139. Log("该舱室未配对相机,仅串口可用。");
  140. }
  141. _current = h;
  142. TxtConnState.Text = $"已连接 舱室{h.HouseSn}";
  143. TxtConnState.Foreground = Brushes.LightGreen;
  144. ControlPanel.IsEnabled = true;
  145. BtnConnect.IsEnabled = false;
  146. BtnDisconnect.IsEnabled = true;
  147. BtnScan.IsEnabled = false;
  148. InitWellSelectors(); // 填充well下拉 + 16个勾选框
  149. RefreshPositions();
  150. }
  151. catch (Exception ex)
  152. {
  153. Log($"连接异常: {ex.Message}");
  154. }
  155. }
  156. private void BtnDisconnect_Click(object sender, RoutedEventArgs e)
  157. {
  158. LogAction("点击 断开");
  159. Cleanup();
  160. TxtConnState.Text = "未连接";
  161. TxtConnState.Foreground = Brushes.Salmon;
  162. ControlPanel.IsEnabled = false;
  163. BtnConnect.IsEnabled = true;
  164. BtnDisconnect.IsEnabled = false;
  165. BtnScan.IsEnabled = true;
  166. Log("已断开。");
  167. }
  168. private void Cleanup()
  169. {
  170. try { _liveCts?.Cancel(); } catch { }
  171. try { _scanCts?.Cancel(); } catch { }
  172. try { if (_motor != null && _camera != null) _motor.CloseLED(); } catch { }
  173. try { _camera?.Dispose(); } catch { }
  174. try { _motor?.Dispose(); } catch { }
  175. _camera = null;
  176. _motor = null;
  177. _current = null;
  178. }
  179. private static int ParseInt(string s, int def)
  180. => int.TryParse(s?.Trim(), out int v) ? v : def;
  181. }
  182. }