ArrowSerialPort.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. using SerialPortUtility;
  2. using System;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. public class ArrowSerialPort : MonoBehaviour
  6. {
  7. private SerialPortUtilityPro serialPortUtility;
  8. private SerialPortUtilityPro.OpenSystem openMode = SerialPortUtilityPro.OpenSystem.PCI;
  9. private int baudrate = 115200;
  10. private string PortName = "/dev/ttyS1";
  11. private static Dictionary<string, int> _isInit = new();
  12. public bool testMode = false;
  13. private void Awake()
  14. {
  15. if (!_isInit.TryGetValue(gameObject.name, out int instanceID) && instanceID == 0)
  16. {
  17. DontDestroyOnLoad(gameObject);
  18. Init();
  19. }
  20. }
  21. private void LOG(string msg, bool warning = false)
  22. {
  23. if (!warning)
  24. Debug.Log($"<color=#00FF00>{msg}</color>");
  25. else
  26. Debug.LogWarning($"<color=#00FF00>{msg}</color>");
  27. }
  28. private void Init()
  29. {
  30. _isInit[gameObject.name] = GetInstanceID();
  31. #if UNITY_ANDROID && !UNITY_EDITOR
  32. serialPortUtility = gameObject.GetComponent<SerialPortUtilityPro>();
  33. serialPortUtility.OpenMethod = openMode;
  34. serialPortUtility.DeviceName = PortName;
  35. serialPortUtility.BaudRate = baudrate;
  36. serialPortUtility.StopBit = SerialPortUtilityPro.StopBitEnum.OneBit;
  37. serialPortUtility.DataBit = SerialPortUtilityPro.DataBitEnum.EightBit;
  38. serialPortUtility.SystemEventObject.AddListener(SystemEventObject);
  39. TrySerialOpenPort();
  40. #endif
  41. #if UNITY_EDITOR
  42. if (testMode)
  43. {
  44. var testBoard = Resources.Load<GameObject>("SerialPortTest");
  45. GameObject.Instantiate(testBoard);
  46. }
  47. #endif
  48. }
  49. private void TrySerialOpenPort()
  50. {
  51. serialPortUtility?.Open();
  52. }
  53. private void SystemEventObject(SerialPortUtilityPro arg0, string msg)
  54. {
  55. if (msg.Equals("OPEN_ERROR") || msg.Equals("PERMISSION_ERROR"))//串口开启失败 定时重试
  56. {
  57. #if UNITY_ANDROID && !UNITY_EDITOR
  58. Invoke("TrySerialOpenPort", 1f);
  59. #endif
  60. LOG($"{PortName} 串口打开失败 重试中", true);
  61. }
  62. else if (msg.Equals("OPENED"))
  63. {
  64. SerialPortHelper.ins.OnConnect(arg0.DeviceName);
  65. RequestDeviceIno();//应用启动 请求一次设备信息
  66. LOG($"{PortName} 串口打开成功!");
  67. }
  68. else if (msg.Equals("CLOSED"))//串口断开
  69. {
  70. SerialPortHelper.ins.OnDisConnect(arg0.DeviceName);
  71. LOG($"{PortName} 串口关闭!");
  72. }
  73. }
  74. private void OnDestroy()
  75. {
  76. if (_isInit.TryGetValue(gameObject.name, out var instanceID) && instanceID == GetInstanceID())
  77. {
  78. LOG($"{PortName} 串口关闭");
  79. SerialPortHelper.ins.OnDisConnect(serialPortUtility?.DeviceName);
  80. serialPortUtility?.Close();
  81. _isInit.Remove(gameObject.name);
  82. }
  83. }
  84. /// <summary>
  85. /// 串口读取二进制流数据(界面挂载调用)
  86. /// </summary>
  87. /// <param name="data"></param>
  88. public void ReadStreamingBinary(object data)
  89. {
  90. PhraseData(data as byte[]);
  91. }
  92. public void TestRead(byte[] bytes)
  93. {
  94. PhraseData(bytes);
  95. }
  96. /// <summary>
  97. /// 解析串口数据
  98. /// </summary>
  99. /// <param name="bytes"></param>
  100. private void PhraseData(byte[] bytes)
  101. {
  102. LOG($"{PortName} 收到串口信息!");
  103. if (bytes[0] == 0xAA)
  104. {
  105. var cmdID = bytes[1];
  106. switch (cmdID)
  107. {
  108. case 0x80://设备信息响应
  109. OnDeviceInfoBack(bytes);
  110. break;
  111. case 0x81://射击消息
  112. OnDeviceShoot(bytes);
  113. break;
  114. case 0x82://按键消息
  115. OnDeviceButton(bytes);
  116. break;
  117. }
  118. }
  119. }
  120. #region APP请求
  121. // 异或校验内容:命令+长度+数据内容
  122. /// <summary>
  123. /// app请求设备信息
  124. /// </summary>
  125. public void RequestDeviceIno()
  126. {
  127. List<byte> data = new List<byte>();
  128. data[0] = 0xAA;//起始码
  129. data[1] = 0x80;//命令号
  130. data[2] = 0x05;//长度
  131. data[3] = 0x85;//异或校验
  132. data[4] = 0x55;//结束码
  133. serialPortUtility?.Write(data.ToArray());
  134. }
  135. #endregion
  136. #region 返回消息处理
  137. /// <summary>
  138. /// 设备信息响应
  139. /// </summary>
  140. private void OnDeviceInfoBack(byte[] bytes)// 0xAA 0x80 0x07 0x01 0x01 0x89 0x5D
  141. {
  142. Debug.Log($"{PortName} 收到设备信息响应!");
  143. var check = bytes[1] + bytes[2] + bytes[3] + bytes[4];//校验:命令+长度+数据内容
  144. if (check != bytes[5])
  145. LOG("OnDeviceInfoBack 数据校验错误!");
  146. else
  147. {
  148. //0x01 HOUYI Pro
  149. //0x02 ARTEMIS Pro
  150. //0x03 Pistol 1
  151. var deviceType = bytes[3];//设备类型
  152. switch (deviceType)
  153. {
  154. case 0x01:
  155. UserSettings.ins.selectDevicesName = "HOUYI Pro";
  156. DevicesHolder.ins.SwitchDeviceByType(AimDeviceType.HOUYIPRO);
  157. break;
  158. case 0x02:
  159. UserSettings.ins.selectDevicesName = "ARTEMIS Pro";
  160. DevicesHolder.ins.SwitchDeviceByType(AimDeviceType.ARTEMISPRO);
  161. break;
  162. case 0x03:
  163. UserSettings.ins.selectDevicesName = "Pistol M9";
  164. DevicesHolder.ins.SwitchDeviceByType(AimDeviceType.Gun);
  165. break;
  166. }
  167. //刷新界面
  168. var setting = FindAnyObjectByType<CustomUIView.BoxUserSettings>();
  169. setting?.FlushDeviceSelect();
  170. }
  171. }
  172. /// <summary>
  173. /// 射击消息
  174. /// </summary>
  175. private void OnDeviceShoot(byte[] bytes)
  176. {
  177. LOG($"{PortName} 收到设备射击消息!");
  178. var check = bytes[1] + bytes[2] + bytes[3] + bytes[4];//校验:命令+长度+数据内容
  179. if (check != bytes[5])
  180. {
  181. LOG("OnDeviceShoot 数据校验错误!");
  182. }else
  183. SerialPortHelper.shoot?.Invoke(bytes);
  184. }
  185. /// <summary>
  186. /// 按键消息
  187. /// </summary>
  188. /// <param name="bytes"></param>
  189. private void OnDeviceButton(byte[] bytes)
  190. {
  191. Debug.Log($"{PortName} 收到设备按键消息!");
  192. var check = bytes[1] + bytes[2] + bytes[3];//校验:命令+长度+数据内容
  193. if (check != bytes[4])
  194. {
  195. LOG("OnDeviceButton 数据校验错误!");
  196. }
  197. else
  198. SerialPortHelper.aim?.Invoke(bytes);
  199. }
  200. #endregion
  201. }