MainWindow.xaml.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. _scanner.Log = Log;
  33. Closing += (s, e) => Cleanup();
  34. Log("程序启动。先【扫描设备】,选舱室后【连接】。");
  35. }
  36. // ───────────────────────── 日志 ─────────────────────────
  37. private void Log(string msg)
  38. {
  39. void Append()
  40. {
  41. string line = $"{DateTime.Now:HH:mm:ss} {msg}\n";
  42. TxtLog.AppendText(line);
  43. TxtLog.ScrollToEnd();
  44. }
  45. if (Dispatcher.CheckAccess()) Append();
  46. else Dispatcher.BeginInvoke((Action)Append);
  47. }
  48. // ───────────────────────── 扫描设备 ─────────────────────────
  49. private async void BtnScan_Click(object sender, RoutedEventArgs e)
  50. {
  51. BtnScan.IsEnabled = false;
  52. Log("开始扫描设备(枚举相机 + 扫串口握手)...");
  53. try
  54. {
  55. var houses = await Task.Run(() => _scanner.ScanAll());
  56. _houses = houses;
  57. CmbHouse.Items.Clear();
  58. foreach (var h in houses)
  59. CmbHouse.Items.Add(h.ToString());
  60. if (houses.Count > 0)
  61. {
  62. CmbHouse.SelectedIndex = 0;
  63. Log($"扫描完成,发现 {houses.Count} 个舱室。");
  64. }
  65. else
  66. {
  67. Log("未发现舱室。检查串口连接/相机USB/驱动。");
  68. }
  69. }
  70. catch (Exception ex)
  71. {
  72. Log($"扫描异常: {ex.Message}");
  73. }
  74. finally
  75. {
  76. BtnScan.IsEnabled = true;
  77. }
  78. }
  79. // ───────────────────────── 连接 / 断开 ─────────────────────────
  80. private void BtnConnect_Click(object sender, RoutedEventArgs e)
  81. {
  82. int idx = CmbHouse.SelectedIndex;
  83. if (idx < 0 || idx >= _houses.Count)
  84. {
  85. Log("请先扫描并选择一个舱室。");
  86. return;
  87. }
  88. var h = _houses[idx];
  89. try
  90. {
  91. // 串口
  92. _motor = new HouseMotor(h.PortName) { Log = Log };
  93. if (!_motor.Open())
  94. {
  95. Log($"串口 {h.PortName} 打开失败。");
  96. _motor = null;
  97. return;
  98. }
  99. _motor.MotorDelayMs = ParseInt(TxtMotorDelay.Text, 1500);
  100. // 相机(若配对到)
  101. if (h.HasCamera)
  102. {
  103. _camera = new SerialCamera(h.CcdIndex, _camWidth, _camHeight, ParseInt(TxtExposure.Text, 400));
  104. int init = _camera.Init(
  105. (byte)ParseInt(TxtGainR.Text, 25),
  106. (byte)ParseInt(TxtGainG.Text, 14),
  107. (byte)ParseInt(TxtGainB.Text, 25));
  108. if (init == 0)
  109. {
  110. _camera.SetOpMode(0); // 拍照模式
  111. Log($"相机#{h.CcdIndex} 已连接。");
  112. }
  113. else
  114. {
  115. Log($"相机#{h.CcdIndex} 初始化失败(code={init}),仅串口可用。");
  116. _camera.Dispose();
  117. _camera = null;
  118. }
  119. }
  120. else
  121. {
  122. Log("该舱室未配对相机,仅串口可用。");
  123. }
  124. _current = h;
  125. TxtConnState.Text = $"已连接 舱室{h.HouseSn}";
  126. TxtConnState.Foreground = Brushes.LightGreen;
  127. ControlPanel.IsEnabled = true;
  128. BtnConnect.IsEnabled = false;
  129. BtnDisconnect.IsEnabled = true;
  130. BtnScan.IsEnabled = false;
  131. InitWellSelectors(); // 填充well下拉 + 16个勾选框
  132. RefreshPositions();
  133. }
  134. catch (Exception ex)
  135. {
  136. Log($"连接异常: {ex.Message}");
  137. }
  138. }
  139. private void BtnDisconnect_Click(object sender, RoutedEventArgs e)
  140. {
  141. Cleanup();
  142. TxtConnState.Text = "未连接";
  143. TxtConnState.Foreground = Brushes.Salmon;
  144. ControlPanel.IsEnabled = false;
  145. BtnConnect.IsEnabled = true;
  146. BtnDisconnect.IsEnabled = false;
  147. BtnScan.IsEnabled = true;
  148. Log("已断开。");
  149. }
  150. private void Cleanup()
  151. {
  152. try { _liveCts?.Cancel(); } catch { }
  153. try { _scanCts?.Cancel(); } catch { }
  154. try { if (_motor != null && _camera != null) _motor.CloseLED(); } catch { }
  155. try { _camera?.Dispose(); } catch { }
  156. try { _motor?.Dispose(); } catch { }
  157. _camera = null;
  158. _motor = null;
  159. _current = null;
  160. }
  161. private static int ParseInt(string s, int def)
  162. => int.TryParse(s?.Trim(), out int v) ? v : def;
  163. }
  164. }