SerialPortHelper.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. /// 获取串口实例(1串口)
  61. /// </summary>
  62. public ArrowSerialPort GetPort()
  63. {
  64. return arrowPort;
  65. }
  66. #region 弹匣变更
  67. public void OnMagazineChange(byte[] bytes)//3-弹夹状态(0x00 弹夹弹出 0x01 弹夹插入)
  68. {
  69. }
  70. #endregion
  71. #region 按键
  72. /// <summary>
  73. ///
  74. /// </summary>
  75. /// <param name="bytes">0-起始码 1-序号 2-长度 3-按键类型 4-异或校验 5-结束码</param>
  76. public void OnDataReceived(byte[] bytes)
  77. {
  78. //按键类型
  79. //0x01 开机键- 短按
  80. //0x02 开机键- 长按(1.5秒)
  81. //0x03 开机键- 双击
  82. //0x04 标定键- 短按
  83. //0x05 标定键- 长按(1.5秒)
  84. //0x06 标定键- 双击
  85. switch (bytes[3])
  86. {
  87. case 0x01:
  88. if (UserSettings.ins.selectDevicesName == "ARTEMIS Pro")
  89. AimHandler.ins.OnDataReceived(new byte[] { 0x66, 0x31 });
  90. break;
  91. case 0x03:
  92. if (UserSettings.ins.selectDevicesName == "ARTEMIS Pro")
  93. AimHandler.ins.OnDataReceived(new byte[] { 0x66, 0x32 });
  94. break;
  95. case 0x04:
  96. AimHandler.ins.OnDataReceived(new byte[] { 0x66, 0x31 });
  97. break;
  98. case 0x05:
  99. AimHandler.ins.OnDataReceived(new byte[] { 0x66, 0x32 });
  100. break;
  101. case 0x06:
  102. break;
  103. }
  104. }
  105. #endregion
  106. #region 射击
  107. bool dataGetting = false;
  108. int dataGetLen = 0;
  109. List<byte> dataBytes = new List<byte>();
  110. int TwoByteToInt(byte b1, byte b2)
  111. {
  112. ushort twoByte = (ushort)(b1 * 256 + b2);
  113. short shortNum = (short)twoByte;
  114. return (int)shortNum;
  115. }
  116. void cacheDataBytes(byte[] bytes, int startIndex)
  117. {
  118. for (int i = startIndex; i < bytes.Length; i++)
  119. {
  120. byte b = bytes[i];
  121. if (dataBytes.Count < dataGetLen)
  122. {
  123. dataBytes.Add(b);
  124. }
  125. else
  126. {
  127. if (ShootCheck.ins) ShootCheck.ins.OnDataReceived(dataBytes.ToArray());
  128. dataGetting = false;
  129. dataBytes.Clear();
  130. break;
  131. }
  132. }
  133. }
  134. //0-起始码 1-序号 2-长度值 3-时间1 4--时间2 5--异或校验 6--结束码
  135. public void handleDataBytes(byte[] bytes)
  136. {
  137. if (dataGetting)
  138. {
  139. cacheDataBytes(bytes, 0);
  140. }
  141. else
  142. {
  143. if (bytes.Length >= 5)
  144. {
  145. int byte3Value = 0; //轴数据识别,1:x轴,2:y轴,3:z轴
  146. if (ShootCheck.ins)
  147. {
  148. CMD cmd = ShootCheck.ins.cmd;
  149. if (cmd.a == "x") byte3Value = 1;
  150. if (cmd.a == "y") byte3Value = 2;
  151. if (cmd.a == "z") byte3Value = 3;
  152. }
  153. if (bytes[0] == 255 || bytes[1] == 255 && bytes[2] == byte3Value)
  154. {
  155. dataGetting = true;
  156. dataGetLen = TwoByteToInt(bytes[3], bytes[4]);
  157. cacheDataBytes(bytes, 5);
  158. }
  159. }
  160. }
  161. // 红外射击检测
  162. byte[] shootBytes = new byte[] { bytes[0], bytes[1], bytes[3], bytes[4], bytes[5] };
  163. ShootCheck.ins.ShootByInfrared(shootBytes);
  164. }
  165. #endregion
  166. }