SerialPortHelper.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. public class SerialPortHelper : MonoBehaviour
  4. {
  5. public static SerialPortExample[] SerialPortExampleGroup;
  6. private static ArrowSerialPort arrowPort;
  7. private static SerialPortHelper _ins;
  8. public static SerialPortHelper ins
  9. {
  10. get
  11. {
  12. if (_ins == null)
  13. {
  14. var go = new GameObject("SerialPortHelper");
  15. _ins = go.AddComponent<SerialPortHelper>();
  16. DontDestroyOnLoad(go);
  17. SerialPortExampleGroup = FindObjectsOfType<SerialPortExample>();
  18. arrowPort = FindObjectOfType<ArrowSerialPort>();
  19. }
  20. return _ins;
  21. }
  22. }
  23. public static System.Action<byte[]> aim;
  24. public static System.Action<byte[]> shoot;
  25. void Start()
  26. {
  27. shoot = handleDataBytes;
  28. aim = OnDataReceived;
  29. }
  30. public void OnConnect(string deviceName)
  31. {
  32. }
  33. public void OnDisConnect(string deviceName)
  34. {
  35. }
  36. bool newPort = false;//true 用0 8串口 false 4替代0 6替代8
  37. /// <summary>
  38. /// 获取串口实例(0串口和8串口)
  39. /// </summary>
  40. public SerialPortExample GetPort(int port)
  41. {
  42. if (port == 0 || port == 8)
  43. {
  44. if (!newPort)
  45. {
  46. if (port == 0) port = 4;
  47. else if (port == 8) port = 6;
  48. }
  49. foreach (var item in SerialPortExampleGroup)
  50. {
  51. if (item.name.Contains($"com{port}"))
  52. return item;
  53. }
  54. }
  55. return null;
  56. }
  57. /// <summary>
  58. /// 获取串口实例(1串口)
  59. /// </summary>
  60. public ArrowSerialPort GetPort()
  61. {
  62. return arrowPort;
  63. }
  64. #region 按键
  65. /// <summary>
  66. ///
  67. /// </summary>
  68. /// <param name="bytes">0-起始码 1-序号 2-长度 3-按键类型 4-异或校验 5-结束码</param>
  69. public void OnDataReceived(byte[] bytes)
  70. {
  71. //按键类型
  72. //0x01 开机键- 短按
  73. //0x02 开机键- 长按(1.5秒)
  74. //0x03 开机键- 双击
  75. //0x04 标定键- 短按
  76. //0x05 标定键- 长按(1.5秒)
  77. //0x06 标定键- 双击
  78. switch (bytes[3])
  79. {
  80. case 0x01:
  81. if (UserSettings.ins.selectDevicesName == "ARTEMIS Pro")
  82. AimHandler.ins.OnDataReceived(new byte[] { 0x66, 0x31 });
  83. break;
  84. case 0x03:
  85. if (UserSettings.ins.selectDevicesName == "ARTEMIS Pro")
  86. AimHandler.ins.OnDataReceived(new byte[] { 0x66, 0x32 });
  87. break;
  88. case 0x04:
  89. AimHandler.ins.OnDataReceived(new byte[] { 0x66, 0x31 });
  90. break;
  91. case 0x05:
  92. AimHandler.ins.OnDataReceived(new byte[] { 0x66, 0x32 });
  93. break;
  94. case 0x06:
  95. break;
  96. }
  97. }
  98. #endregion
  99. #region 射击
  100. bool dataGetting = false;
  101. int dataGetLen = 0;
  102. List<byte> dataBytes = new List<byte>();
  103. int TwoByteToInt(byte b1, byte b2)
  104. {
  105. ushort twoByte = (ushort)(b1 * 256 + b2);
  106. short shortNum = (short)twoByte;
  107. return (int)shortNum;
  108. }
  109. void cacheDataBytes(byte[] bytes, int startIndex)
  110. {
  111. for (int i = startIndex; i < bytes.Length; i++)
  112. {
  113. byte b = bytes[i];
  114. if (dataBytes.Count < dataGetLen)
  115. {
  116. dataBytes.Add(b);
  117. }
  118. else
  119. {
  120. if (ShootCheck.ins) ShootCheck.ins.OnDataReceived(dataBytes.ToArray());
  121. dataGetting = false;
  122. dataBytes.Clear();
  123. break;
  124. }
  125. }
  126. }
  127. //0-起始码 1-序号 2-长度值 3-时间1 4--时间2 5--异或校验 6--结束码
  128. public void handleDataBytes(byte[] bytes)
  129. {
  130. if (dataGetting)
  131. {
  132. cacheDataBytes(bytes, 0);
  133. }
  134. else
  135. {
  136. if (bytes.Length >= 5)
  137. {
  138. int byte3Value = 0; //轴数据识别,1:x轴,2:y轴,3:z轴
  139. if (ShootCheck.ins)
  140. {
  141. CMD cmd = ShootCheck.ins.cmd;
  142. if (cmd.a == "x") byte3Value = 1;
  143. if (cmd.a == "y") byte3Value = 2;
  144. if (cmd.a == "z") byte3Value = 3;
  145. }
  146. if (bytes[0] == 255 || bytes[1] == 255 && bytes[2] == byte3Value)
  147. {
  148. dataGetting = true;
  149. dataGetLen = TwoByteToInt(bytes[3], bytes[4]);
  150. cacheDataBytes(bytes, 5);
  151. }
  152. }
  153. }
  154. // 红外射击检测
  155. byte[] shootBytes = new byte[] { bytes[0], bytes[1], bytes[3], bytes[4], bytes[5] };
  156. ShootCheck.ins.ShootByInfrared(shootBytes);
  157. }
  158. #endregion
  159. }