SerialPortHelper.cs 4.1 KB

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