|
|
@@ -0,0 +1,142 @@
|
|
|
+using System.Collections.Generic;
|
|
|
+using UnityEngine;
|
|
|
+
|
|
|
+public class SerialPortHelper : MonoBehaviour
|
|
|
+{
|
|
|
+ public static SerialPortExample[] SerialPortExampleGroup;
|
|
|
+ private static ArrowSerialPort arrowPort;
|
|
|
+
|
|
|
+ private static SerialPortHelper _ins;
|
|
|
+
|
|
|
+ public static SerialPortHelper ins
|
|
|
+ {
|
|
|
+ get
|
|
|
+ {
|
|
|
+ if (_ins == null)
|
|
|
+ {
|
|
|
+ var go = new GameObject("SerialPortHelper");
|
|
|
+ _ins = go.AddComponent<SerialPortHelper>();
|
|
|
+ DontDestroyOnLoad(go);
|
|
|
+ SerialPortExampleGroup = FindObjectsOfType<SerialPortExample>();
|
|
|
+ }
|
|
|
+ return _ins;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static System.Action<byte[]> aim;
|
|
|
+ public static System.Action<byte[]> shoot;
|
|
|
+
|
|
|
+ void Start()
|
|
|
+ {
|
|
|
+ shoot = handleDataBytes;
|
|
|
+ aim = OnDataReceived;
|
|
|
+ }
|
|
|
+ // 0x01 开机键 - 短按
|
|
|
+ // 0x02 开机键 - 长按(1.5 秒)
|
|
|
+ // 0x03 开机键 - 双击
|
|
|
+ // 0x04 标定键 - 短按
|
|
|
+ // 0x05 标定键 - 长按(1.5 秒)
|
|
|
+ // 0x06 标定键 - 双击
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 获取串口实例(0串口和8串口)
|
|
|
+ /// </summary>
|
|
|
+ public SerialPortExample GetPort(int port)
|
|
|
+ {
|
|
|
+ if (port == 0 || port == 8)
|
|
|
+ {
|
|
|
+ foreach (var item in SerialPortExampleGroup)
|
|
|
+ {
|
|
|
+ if (item.name.Contains("com0"))
|
|
|
+ return item;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 获取串口实例(1串口)
|
|
|
+ /// </summary>
|
|
|
+ public ArrowSerialPort GetPort()
|
|
|
+ {
|
|
|
+ return arrowPort;
|
|
|
+ }
|
|
|
+
|
|
|
+ #region 按键
|
|
|
+ /// <summary>
|
|
|
+ ///
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="bytes">0-起始码 1-序号 2-长度 3-按键类型 4-异或校验 5-结束码</param>
|
|
|
+ public void OnDataReceived(byte[] bytes)
|
|
|
+ {
|
|
|
+ //按键类型
|
|
|
+ //0x01 开机键- 短按
|
|
|
+ //0x02 开机键- 长按(1.5秒)
|
|
|
+ //0x03 开机键- 双击
|
|
|
+ //0x04 标定键- 短按
|
|
|
+ //0x05 标定键- 长按(1.5秒)
|
|
|
+ //0x06 标定键- 双击
|
|
|
+ }
|
|
|
+ #endregion
|
|
|
+
|
|
|
+ #region 射击
|
|
|
+ bool dataGetting = false;
|
|
|
+ int dataGetLen = 0;
|
|
|
+ List<byte> dataBytes = new List<byte>();
|
|
|
+
|
|
|
+ int TwoByteToInt(byte b1, byte b2)
|
|
|
+ {
|
|
|
+ ushort twoByte = (ushort)(b1 * 256 + b2);
|
|
|
+ short shortNum = (short)twoByte;
|
|
|
+ return (int)shortNum;
|
|
|
+ }
|
|
|
+
|
|
|
+ void cacheDataBytes(byte[] bytes, int startIndex)
|
|
|
+ {
|
|
|
+ for (int i = startIndex; i < bytes.Length; i++)
|
|
|
+ {
|
|
|
+ byte b = bytes[i];
|
|
|
+ if (dataBytes.Count < dataGetLen)
|
|
|
+ {
|
|
|
+ dataBytes.Add(b);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ if (ShootCheck.ins) ShootCheck.ins.OnDataReceived(dataBytes.ToArray());
|
|
|
+ dataGetting = false;
|
|
|
+ dataBytes.Clear();
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //0-起始码 1-序号 2-长度值 3-时间1 4--时间2 5--异或校验 6--结束码
|
|
|
+ public void handleDataBytes(byte[] bytes)
|
|
|
+ {
|
|
|
+ if (dataGetting)
|
|
|
+ {
|
|
|
+ cacheDataBytes(bytes, 0);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ if (bytes.Length >= 5)
|
|
|
+ {
|
|
|
+ int byte3Value = 0; //轴数据识别,1:x轴,2:y轴,3:z轴
|
|
|
+ if (ShootCheck.ins)
|
|
|
+ {
|
|
|
+ CMD cmd = ShootCheck.ins.cmd;
|
|
|
+ if (cmd.a == "x") byte3Value = 1;
|
|
|
+ if (cmd.a == "y") byte3Value = 2;
|
|
|
+ if (cmd.a == "z") byte3Value = 3;
|
|
|
+ }
|
|
|
+ if (bytes[0] == 255 || bytes[1] == 255 && bytes[2] == byte3Value)
|
|
|
+ {
|
|
|
+ dataGetting = true;
|
|
|
+ dataGetLen = TwoByteToInt(bytes[3], bytes[4]);
|
|
|
+ cacheDataBytes(bytes, 5);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ #endregion
|
|
|
+}
|