TSDLightProtocol.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. using System;
  2. using System.Text;
  3. namespace TeamAAS.Communication.LightSources
  4. {
  5. /// <summary>
  6. /// TSD 光源协议命令构造器
  7. /// 适用于 DPA12024V-4T-3.0 等型号
  8. /// 特点: 所有命令带 XOR 校验字
  9. /// </summary>
  10. public static class TSDLightProtocol
  11. {
  12. /// <summary>
  13. /// 读取指定通道亮度
  14. /// #4{CH}00015 + XOR校验
  15. /// </summary>
  16. public static string BuildReadCommand(int channelIndex)
  17. {
  18. string cmd = $"#4{channelIndex + 1}00015";
  19. return cmd + CalcXor(cmd);
  20. }
  21. /// <summary>
  22. /// 设置指定通道亮度
  23. /// #3{CH}0{HEX} + XOR校验
  24. /// </summary>
  25. public static string BuildSetBrightnessCommand(int channelIndex, int brightness)
  26. {
  27. int br = Math.Max(0, Math.Min(255, brightness));
  28. string cmd = $"#3{channelIndex + 1}0{br.ToString("X2")}";
  29. return cmd + CalcXor(cmd);
  30. }
  31. /// <summary>
  32. /// 打开指定通道
  33. /// #1{CH}064 + XOR校验
  34. /// </summary>
  35. public static string BuildTurnOnChannelCommand(int channelIndex)
  36. {
  37. string cmd = $"#1{channelIndex + 1}064";
  38. return cmd + CalcXor(cmd);
  39. }
  40. /// <summary>
  41. /// 关闭指定通道
  42. /// #2{CH}029 + XOR校验
  43. /// </summary>
  44. public static string BuildTurnOffChannelCommand(int channelIndex)
  45. {
  46. string cmd = $"#2{channelIndex + 1}029";
  47. return cmd + CalcXor(cmd);
  48. }
  49. /// <summary>
  50. /// 全开所有通道(逐通道发送)
  51. /// </summary>
  52. public static string[] BuildTurnOnAllCommands(int channelCount)
  53. {
  54. var cmds = new string[channelCount];
  55. for (int i = 0; i < channelCount; i++)
  56. cmds[i] = BuildTurnOnChannelCommand(i);
  57. return cmds;
  58. }
  59. /// <summary>
  60. /// 全关所有通道(逐通道发送)
  61. /// </summary>
  62. public static string[] BuildTurnOffAllCommands(int channelCount)
  63. {
  64. var cmds = new string[channelCount];
  65. for (int i = 0; i < channelCount; i++)
  66. cmds[i] = BuildTurnOffChannelCommand(i);
  67. return cmds;
  68. }
  69. /// <summary>
  70. /// 解析读取响应,返回亮度值(16进制转10进制)
  71. /// </summary>
  72. public static int ParseReadResponse(string response)
  73. {
  74. if (string.IsNullOrEmpty(response)) return 0;
  75. if (response.Contains("$")) return 0;
  76. try
  77. {
  78. string val = response.Substring(4, 2);
  79. return Convert.ToInt32(val, 16);
  80. }
  81. catch { return 0; }
  82. }
  83. /// <summary>
  84. /// 验证响应是否成功
  85. /// 成功: 响应包含 "#"
  86. /// 失败: 响应包含 "$"
  87. /// </summary>
  88. public static bool VerifyResponse(string response)
  89. {
  90. return !string.IsNullOrEmpty(response) && response.Contains("#");
  91. }
  92. /// <summary>
  93. /// XOR 校验字计算
  94. /// </summary>
  95. public static string CalcXor(string asciiData)
  96. {
  97. if (string.IsNullOrEmpty(asciiData)) return "00";
  98. byte[] data = Encoding.ASCII.GetBytes(asciiData);
  99. byte xor = data[0];
  100. for (int i = 1; i < data.Length; i++)
  101. xor ^= data[i];
  102. char high = NibbleToAscii((byte)((xor >> 4) & 0x0F));
  103. char low = NibbleToAscii((byte)(xor & 0x0F));
  104. return new string(new[] { high, low });
  105. }
  106. private static char NibbleToAscii(byte nibble)
  107. {
  108. return nibble < 10 ? (char)('0' + nibble) : (char)('A' + (nibble - 10));
  109. }
  110. }
  111. }