MainWindow.xaml.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. BtnScan.IsEnabled = false;
  67. Log("开始扫描设备(枚举相机 + 扫串口握手)...");
  68. try
  69. {
  70. var houses = await Task.Run(() => _scanner.ScanAll());
  71. _houses = houses;
  72. CmbHouse.Items.Clear();
  73. foreach (var h in houses)
  74. CmbHouse.Items.Add(h.ToString());
  75. if (houses.Count > 0)
  76. {
  77. CmbHouse.SelectedIndex = 0;
  78. Log($"扫描完成,发现 {houses.Count} 个舱室。");
  79. }
  80. else
  81. {
  82. Log("未发现舱室。检查串口连接/相机USB/驱动。");
  83. }
  84. }
  85. catch (Exception ex)
  86. {
  87. Log($"扫描异常: {ex.Message}");
  88. }
  89. finally
  90. {
  91. BtnScan.IsEnabled = true;
  92. }
  93. }
  94. // ───────────────────────── 连接 / 断开 ─────────────────────────
  95. private void BtnConnect_Click(object sender, RoutedEventArgs e)
  96. {
  97. int idx = CmbHouse.SelectedIndex;
  98. if (idx < 0 || idx >= _houses.Count)
  99. {
  100. Log("请先扫描并选择一个舱室。");
  101. return;
  102. }
  103. var h = _houses[idx];
  104. try
  105. {
  106. // 串口
  107. _motor = new HouseMotor(h.PortName) { Log = Log };
  108. if (!_motor.Open())
  109. {
  110. Log($"串口 {h.PortName} 打开失败。");
  111. _motor = null;
  112. return;
  113. }
  114. _motor.MotorDelayMs = ParseInt(TxtMotorDelay.Text, 1500);
  115. // 相机(若配对到)
  116. if (h.HasCamera)
  117. {
  118. _camera = new SerialCamera(h.CcdIndex, _camWidth, _camHeight, ParseInt(TxtExposure.Text, 400));
  119. int init = _camera.Init(
  120. (byte)ParseInt(TxtGainR.Text, 25),
  121. (byte)ParseInt(TxtGainG.Text, 14),
  122. (byte)ParseInt(TxtGainB.Text, 25));
  123. if (init == 0)
  124. {
  125. _camera.SetOpMode(0); // 拍照模式
  126. Log($"相机#{h.CcdIndex} 已连接。");
  127. }
  128. else
  129. {
  130. Log($"相机#{h.CcdIndex} 初始化失败(code={init}),仅串口可用。");
  131. _camera.Dispose();
  132. _camera = null;
  133. }
  134. }
  135. else
  136. {
  137. Log("该舱室未配对相机,仅串口可用。");
  138. }
  139. _current = h;
  140. TxtConnState.Text = $"已连接 舱室{h.HouseSn}";
  141. TxtConnState.Foreground = Brushes.LightGreen;
  142. ControlPanel.IsEnabled = true;
  143. BtnConnect.IsEnabled = false;
  144. BtnDisconnect.IsEnabled = true;
  145. BtnScan.IsEnabled = false;
  146. InitWellSelectors(); // 填充well下拉 + 16个勾选框
  147. RefreshPositions();
  148. }
  149. catch (Exception ex)
  150. {
  151. Log($"连接异常: {ex.Message}");
  152. }
  153. }
  154. private void BtnDisconnect_Click(object sender, RoutedEventArgs e)
  155. {
  156. Cleanup();
  157. TxtConnState.Text = "未连接";
  158. TxtConnState.Foreground = Brushes.Salmon;
  159. ControlPanel.IsEnabled = false;
  160. BtnConnect.IsEnabled = true;
  161. BtnDisconnect.IsEnabled = false;
  162. BtnScan.IsEnabled = true;
  163. Log("已断开。");
  164. }
  165. private void Cleanup()
  166. {
  167. try { _liveCts?.Cancel(); } catch { }
  168. try { _scanCts?.Cancel(); } catch { }
  169. try { if (_motor != null && _camera != null) _motor.CloseLED(); } catch { }
  170. try { _camera?.Dispose(); } catch { }
  171. try { _motor?.Dispose(); } catch { }
  172. _camera = null;
  173. _motor = null;
  174. _current = null;
  175. }
  176. private static int ParseInt(string s, int def)
  177. => int.TryParse(s?.Trim(), out int v) ? v : def;
  178. }
  179. }