HardwareRigFixture.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO.Ports;
  4. using IvfTl.Hardware.Impl;
  5. namespace IvfTl.Hardware.HilTests.Infrastructure
  6. {
  7. /// <summary>一个有响应的真舱:串口 + 握手返回的舱号。</summary>
  8. public sealed class ChamberInfo
  9. {
  10. public string Port { get; }
  11. public int HouseSn { get; }
  12. public ChamberInfo(string port, int houseSn) { Port = port; HouseSn = houseSn; }
  13. }
  14. /// <summary>
  15. /// 全套件构造一次:扫描 COM 口、逐口 Open+握手,收集有响应的真舱。
  16. /// 探测完即 Close 释放;各测试用时各自重开端口。
  17. /// 无硬件 / control 正占用串口 → Chambers 为空 → 测试 Skip。
  18. /// </summary>
  19. public sealed class HardwareRigFixture : IDisposable
  20. {
  21. public IReadOnlyList<ChamberInfo> Chambers { get; }
  22. public HardwareRigFixture()
  23. {
  24. var found = new List<ChamberInfo>();
  25. string[] ports;
  26. try { ports = SerialPort.GetPortNames(); } catch { ports = Array.Empty<string>(); }
  27. foreach (var port in ports)
  28. {
  29. if (port == "COM1" || port == "COM2") continue; // 非舱口
  30. SerialChannelImpl ch = null;
  31. try
  32. {
  33. ch = new SerialChannelImpl(0, port);
  34. if (!ch.Open()) continue;
  35. int house = ch.ShakeHandsWait();
  36. if (house < 0) continue;
  37. found.Add(new ChamberInfo(port, house));
  38. }
  39. catch { /* 占口/无响应 → 不计入 */ }
  40. finally { try { ch?.Close(); } catch { } }
  41. }
  42. Chambers = found;
  43. }
  44. /// <summary>取第一个有舱室 E方(排气阀/灯光/焦点)的真舱,排除舱11(缓冲瓶/总控)。无则返回 null。</summary>
  45. public ChamberInfo FirstChamberWithWells()
  46. {
  47. foreach (var c in Chambers) if (c.HouseSn != 11) return c;
  48. return null;
  49. }
  50. public void Dispose() { }
  51. }
  52. }