SerialPortHelper.cs 4.4 KB

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