SerialPortHelper.cs 4.3 KB

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