CommanderEepromTests.cs 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using IvfTl.Control.Entity;
  2. using ivf_tl_SerialHelper.Util;
  3. using Xunit;
  4. namespace ivf_tl_SerialHelper.Tests
  5. {
  6. /// <summary>
  7. /// M-01/M-02/M-03 合并遗留:control 端 Commander 缺 3 个 E方读写 builder。
  8. /// 期望字节 = 合并前 operate 端 ivf_tl_Entity/ComEntitys/Commander.cs 同名方法的黄金真值
  9. /// (临时文件/ivf_tl_operate_2.0,该实现真机已用),末位为 CreateORC 累加和校验(对前 n-1 字节求和取低字节)。
  10. /// 字节编码是纯逻辑,这里 red→green;ComBin/SerialChannelImpl 接线 + EEPROM 读写往返走真机验证。
  11. /// </summary>
  12. public class CommanderEepromTests
  13. {
  14. /// <summary>M-02 读E方→舱室排气阀打开时间。地址 00 03 08,固定 9 字节读命令。</summary>
  15. [Fact]
  16. public void CreateReadEEPROMVentNum_产出与operate一致的读排气阀时间命令字节()
  17. {
  18. var custom = new CustomProtocol();
  19. custom.CreateReadEEPROMVentNum();
  20. // 5E 11(READ_EEPROM) 00 09(LTH) 00 03 08(地址) 04 00 → ORC=前8字节和=0x87
  21. var expected = new byte[] { 0x5E, 0x11, 0x00, 0x09, 0x00, 0x03, 0x08, 0x04, 0x87 };
  22. Assert.Equal(expected, custom.sendBuffer);
  23. }
  24. /// <summary>M-01 写E方→舱室排气阀打开时间。地址 00 03 08 + 小端 4 字节值 + 校验。</summary>
  25. [Fact]
  26. public void CreateWriteEEPROOpenVentTimeCommand_产出与operate一致的写排气阀时间命令字节()
  27. {
  28. var custom = new CustomProtocol();
  29. custom.CreateWriteEEPROOpenVentTimeCommand(5);
  30. // 5E 12(WRITE_EEPROM) 00 0C(LTH) 00 03 08(地址) 05 00 00 00(小端5) 00→ORC=前11字节和=0x8C
  31. var expected = new byte[] { 0x5E, 0x12, 0x00, 0x0C, 0x00, 0x03, 0x08, 0x05, 0x00, 0x00, 0x00, 0x8C };
  32. Assert.Equal(expected, custom.sendBuffer);
  33. }
  34. /// <summary>M-03 写E方→灯光亮度。地址 00 05 34 + 小端 4 字节值 + 校验。</summary>
  35. [Fact]
  36. public void CreateWriteEEPROMLightNum_产出与operate一致的写灯光亮度命令字节()
  37. {
  38. var custom = new CustomProtocol();
  39. custom.CreateWriteEEPROMLightNum(80);
  40. // 5E 12(WRITE_EEPROM) 00 0C(LTH) 00 05 34(地址) 50 00 00 00(小端80=0x50) 00→ORC=前11字节和=0x105&0xFF=0x05
  41. var expected = new byte[] { 0x5E, 0x12, 0x00, 0x0C, 0x00, 0x05, 0x34, 0x50, 0x00, 0x00, 0x00, 0x05 };
  42. Assert.Equal(expected, custom.sendBuffer);
  43. }
  44. /// <summary>
  45. /// 交叉校验:写排气阀时间 与 control 已通过的写进气阀时间(地址 00 03 0c)仅"地址低字节"不同(0x08 vs 0x0c)。
  46. /// 锚定到既有已验证 builder,防止地址表整体写错。
  47. /// </summary>
  48. [Fact]
  49. public void 写排气阀与写进气阀命令仅地址低字节不同()
  50. {
  51. var vent = new CustomProtocol();
  52. vent.CreateWriteEEPROOpenVentTimeCommand(5);
  53. var intake = new CustomProtocol();
  54. intake.CreateWriteEEPROOpenIntakeTimeCommand(5);
  55. Assert.Equal(intake.sendBuffer.Length, vent.sendBuffer.Length);
  56. Assert.Equal((byte)0x0c, intake.sendBuffer[6]); // 进气阀地址低字节
  57. Assert.Equal((byte)0x08, vent.sendBuffer[6]); // 排气阀地址低字节
  58. // 除地址低字节(idx6)与末位校验外,其余字节应完全相同
  59. for (int i = 0; i < vent.sendBuffer.Length - 1; i++)
  60. {
  61. if (i == 6) continue;
  62. Assert.Equal(intake.sendBuffer[i], vent.sendBuffer[i]);
  63. }
  64. }
  65. }
  66. }