SerialPortHelper.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. }
  85. #endregion
  86. #region 按键
  87. /// <summary>
  88. ///
  89. /// </summary>
  90. /// <param name="bytes">0-起始码 1-序号 2-长度 3-按键类型 4-异或校验 5-结束码</param>
  91. public void OnDataReceived(byte[] bytes)
  92. {
  93. //按键类型
  94. //0x01 开机键- 短按
  95. //0x02 开机键- 长按(1.5秒)
  96. //0x03 开机键- 双击
  97. //0x04 标定键- 短按
  98. //0x05 标定键- 长按(1.5秒)
  99. //0x06 标定键- 双击
  100. switch (bytes[3])
  101. {
  102. case 0x01:
  103. if (UserSettings.ins.selectDevicesName == "ARTEMIS Pro")
  104. AimHandler.ins.OnDataReceived(new byte[] { 0x66, 0x31 });
  105. break;
  106. case 0x03:
  107. if (UserSettings.ins.selectDevicesName == "ARTEMIS Pro")
  108. AimHandler.ins.OnDataReceived(new byte[] { 0x66, 0x32 });
  109. break;
  110. case 0x04:
  111. AimHandler.ins.OnDataReceived(new byte[] { 0x66, 0x31 });
  112. break;
  113. case 0x05:
  114. AimHandler.ins.OnDataReceived(new byte[] { 0x66, 0x32 });
  115. break;
  116. case 0x06:
  117. break;
  118. }
  119. }
  120. #endregion
  121. #region 射击
  122. bool dataGetting = false;
  123. int dataGetLen = 0;
  124. List<byte> dataBytes = new List<byte>();
  125. int TwoByteToInt(byte b1, byte b2)
  126. {
  127. ushort twoByte = (ushort)(b1 * 256 + b2);
  128. short shortNum = (short)twoByte;
  129. return (int)shortNum;
  130. }
  131. void cacheDataBytes(byte[] bytes, int startIndex)
  132. {
  133. for (int i = startIndex; i < bytes.Length; i++)
  134. {
  135. byte b = bytes[i];
  136. if (dataBytes.Count < dataGetLen)
  137. {
  138. dataBytes.Add(b);
  139. }
  140. else
  141. {
  142. if (ShootCheck.ins) ShootCheck.ins.OnDataReceived(dataBytes.ToArray());
  143. dataGetting = false;
  144. dataBytes.Clear();
  145. break;
  146. }
  147. }
  148. }
  149. //0-起始码 1-序号 2-长度值 3-时间1 4--时间2 5--异或校验 6--结束码
  150. public void handleDataBytes(byte[] bytes)
  151. {
  152. if (dataGetting)
  153. {
  154. cacheDataBytes(bytes, 0);
  155. }
  156. else
  157. {
  158. if (bytes.Length >= 5)
  159. {
  160. int byte3Value = 0; //轴数据识别,1:x轴,2:y轴,3:z轴
  161. if (ShootCheck.ins)
  162. {
  163. CMD cmd = ShootCheck.ins.cmd;
  164. if (cmd.a == "x") byte3Value = 1;
  165. if (cmd.a == "y") byte3Value = 2;
  166. if (cmd.a == "z") byte3Value = 3;
  167. }
  168. if (bytes[0] == 255 || bytes[1] == 255 && bytes[2] == byte3Value)
  169. {
  170. dataGetting = true;
  171. dataGetLen = TwoByteToInt(bytes[3], bytes[4]);
  172. cacheDataBytes(bytes, 5);
  173. }
  174. }
  175. }
  176. // 红外射击检测
  177. byte[] shootBytes = new byte[] { bytes[0], bytes[1], bytes[3], bytes[4], bytes[5] };
  178. ShootCheck.ins.ShootByInfrared(shootBytes);
  179. }
  180. #endregion
  181. }