SerialPortHelper.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. public static System.Action<byte[]> magazineChange;
  26. void Start()
  27. {
  28. shoot = handleDataBytes;
  29. aim = OnDataReceived;
  30. magazineChange = OnMagazineChange;
  31. }
  32. public void OnConnect(string deviceName)
  33. {
  34. }
  35. public void OnDisConnect(string deviceName)
  36. {
  37. }
  38. //bool newPort = false;//true 用0 8串口 false 4替代0 6替代8
  39. ///// <summary>
  40. ///// 获取串口实例(0串口和8串口 4替代0 6替代8)
  41. ///// </summary>
  42. //public SerialPortExample GetPort(int port)
  43. //{
  44. // if (port == 0 || port == 8)
  45. // {
  46. // if (!newPort)
  47. // {
  48. // if (port == 0) port = 4;
  49. // else if (port == 8) port = 6;
  50. // }
  51. // foreach (var item in SerialPortExampleGroup)
  52. // {
  53. // if (item.name.Contains($"com{port}"))
  54. // return item;
  55. // }
  56. // }
  57. // return null;
  58. //}
  59. /// <summary>
  60. /// 获取串口实例(4,6)
  61. /// </summary>
  62. public SerialPortExample GetPort(int port)
  63. {
  64. if (port == 4 || port == 6)
  65. {
  66. foreach (var item in SerialPortExampleGroup)
  67. {
  68. if (item.name.Contains($"com{port}"))
  69. return item;
  70. }
  71. }
  72. return null;
  73. }
  74. /// <summary>
  75. /// 获取串口实例(1串口)
  76. /// </summary>
  77. public ArrowSerialPort GetPort()
  78. {
  79. return arrowPort;
  80. }
  81. #region 弹匣变更
  82. public void OnMagazineChange(byte[] bytes)//3-弹夹状态(0x00 弹夹弹出 0x01 弹夹插入)
  83. {
  84. //00 弹夹分离,01 上弹夹
  85. if (ShootCheck.ins) ShootCheck.ins.UpdateTheMagazine(bytes);
  86. }
  87. #endregion
  88. #region 按键
  89. /// <summary>
  90. ///
  91. /// </summary>
  92. /// <param name="bytes">0-起始码 1-序号 2-长度 3-按键类型 4-异或校验 5-结束码</param>
  93. public void OnDataReceived(byte[] bytes)
  94. {
  95. //按键类型
  96. //0x01 开机键- 短按
  97. //0x02 开机键- 长按(1.5秒)
  98. //0x03 开机键- 双击
  99. //0x04 标定键- 短按
  100. //0x05 标定键- 长按(1.5秒)
  101. //0x06 标定键- 双击
  102. switch (bytes[3])
  103. {
  104. case 0x01:
  105. if (UserSettings.ins.selectDevicesName == "ARTEMIS Pro")
  106. AimHandler.ins.OnDataReceived(new byte[] { 0x66, 0x31 });
  107. break;
  108. case 0x03:
  109. if (UserSettings.ins.selectDevicesName == "ARTEMIS Pro")
  110. AimHandler.ins.OnDataReceived(new byte[] { 0x66, 0x32 });
  111. break;
  112. case 0x04:
  113. AimHandler.ins.OnDataReceived(new byte[] { 0x66, 0x31 });
  114. break;
  115. case 0x05:
  116. AimHandler.ins.OnDataReceived(new byte[] { 0x66, 0x32 });
  117. break;
  118. case 0x06:
  119. break;
  120. }
  121. }
  122. #endregion
  123. #region 射击
  124. bool dataGetting = false;
  125. int dataGetLen = 0;
  126. List<byte> dataBytes = new List<byte>();
  127. int TwoByteToInt(byte b1, byte b2)
  128. {
  129. ushort twoByte = (ushort)(b1 * 256 + b2);
  130. short shortNum = (short)twoByte;
  131. return (int)shortNum;
  132. }
  133. void cacheDataBytes(byte[] bytes, int startIndex)
  134. {
  135. for (int i = startIndex; i < bytes.Length; i++)
  136. {
  137. byte b = bytes[i];
  138. if (dataBytes.Count < dataGetLen)
  139. {
  140. dataBytes.Add(b);
  141. }
  142. else
  143. {
  144. if (ShootCheck.ins) ShootCheck.ins.OnDataReceived(dataBytes.ToArray());
  145. dataGetting = false;
  146. dataBytes.Clear();
  147. break;
  148. }
  149. }
  150. }
  151. //0-起始码 1-序号 2-长度值 3-时间1 4--时间2 5--异或校验 6--结束码
  152. public void handleDataBytes(byte[] bytes)
  153. {
  154. if (dataGetting)
  155. {
  156. cacheDataBytes(bytes, 0);
  157. }
  158. else
  159. {
  160. if (bytes.Length >= 5)
  161. {
  162. int byte3Value = 0; //轴数据识别,1:x轴,2:y轴,3:z轴
  163. if (ShootCheck.ins)
  164. {
  165. CMD cmd = ShootCheck.ins.cmd;
  166. if (cmd.a == "x") byte3Value = 1;
  167. if (cmd.a == "y") byte3Value = 2;
  168. if (cmd.a == "z") byte3Value = 3;
  169. }
  170. if (bytes[0] == 255 || bytes[1] == 255 && bytes[2] == byte3Value)
  171. {
  172. dataGetting = true;
  173. dataGetLen = TwoByteToInt(bytes[3], bytes[4]);
  174. cacheDataBytes(bytes, 5);
  175. }
  176. }
  177. }
  178. // 红外射击检测
  179. byte[] shootBytes = new byte[] { bytes[0], bytes[1], bytes[3], bytes[4], bytes[5] };
  180. ShootCheck.ins.ShootByInfrared(shootBytes);
  181. }
  182. #endregion
  183. }