SerialPortHelper.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. }
  72. #endregion
  73. #region 射击
  74. bool dataGetting = false;
  75. int dataGetLen = 0;
  76. List<byte> dataBytes = new List<byte>();
  77. int TwoByteToInt(byte b1, byte b2)
  78. {
  79. ushort twoByte = (ushort)(b1 * 256 + b2);
  80. short shortNum = (short)twoByte;
  81. return (int)shortNum;
  82. }
  83. void cacheDataBytes(byte[] bytes, int startIndex)
  84. {
  85. for (int i = startIndex; i < bytes.Length; i++)
  86. {
  87. byte b = bytes[i];
  88. if (dataBytes.Count < dataGetLen)
  89. {
  90. dataBytes.Add(b);
  91. }
  92. else
  93. {
  94. if (ShootCheck.ins) ShootCheck.ins.OnDataReceived(dataBytes.ToArray());
  95. dataGetting = false;
  96. dataBytes.Clear();
  97. break;
  98. }
  99. }
  100. }
  101. //0-起始码 1-序号 2-长度值 3-时间1 4--时间2 5--异或校验 6--结束码
  102. public void handleDataBytes(byte[] bytes)
  103. {
  104. if (dataGetting)
  105. {
  106. cacheDataBytes(bytes, 0);
  107. }
  108. else
  109. {
  110. if (bytes.Length >= 5)
  111. {
  112. int byte3Value = 0; //轴数据识别,1:x轴,2:y轴,3:z轴
  113. if (ShootCheck.ins)
  114. {
  115. CMD cmd = ShootCheck.ins.cmd;
  116. if (cmd.a == "x") byte3Value = 1;
  117. if (cmd.a == "y") byte3Value = 2;
  118. if (cmd.a == "z") byte3Value = 3;
  119. }
  120. if (bytes[0] == 255 || bytes[1] == 255 && bytes[2] == byte3Value)
  121. {
  122. dataGetting = true;
  123. dataGetLen = TwoByteToInt(bytes[3], bytes[4]);
  124. cacheDataBytes(bytes, 5);
  125. }
  126. }
  127. }
  128. }
  129. #endregion
  130. }