| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- 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>();
- arrowPort = FindObjectOfType<ArrowSerialPort>();
- }
- return _ins;
- }
- }
- public static System.Action<byte[]> aim;
- public static System.Action<byte[]> shoot;
- public static System.Action<byte[]> magazineChange;
- void Start()
- {
- shoot = handleDataBytes;
- aim = OnDataReceived;
- magazineChange = OnMagazineChange;
- }
- public void OnConnect(string deviceName)
- {
-
- }
- public void OnDisConnect(string deviceName)
- {
- }
- //bool newPort = false;//true 用0 8串口 false 4替代0 6替代8
- ///// <summary>
- ///// 获取串口实例(0串口和8串口 4替代0 6替代8)
- ///// </summary>
- //public SerialPortExample GetPort(int port)
- //{
- // if (port == 0 || port == 8)
- // {
- // if (!newPort)
- // {
- // if (port == 0) port = 4;
- // else if (port == 8) port = 6;
- // }
- // foreach (var item in SerialPortExampleGroup)
- // {
- // if (item.name.Contains($"com{port}"))
- // return item;
- // }
- // }
- // return null;
- //}
- /// <summary>
- /// 获取串口实例(4,6)
- /// </summary>
- public SerialPortExample GetPort(int port)
- {
- if (port == 4 || port == 6)
- {
- foreach (var item in SerialPortExampleGroup)
- {
- if (item.name.Contains($"com{port}"))
- return item;
- }
- }
- return null;
- }
- /// <summary>
- /// 获取串口实例(1串口)
- /// </summary>
- public ArrowSerialPort GetPort()
- {
- return arrowPort;
- }
- #region 弹匣变更
- public void OnMagazineChange(byte[] bytes)//3-弹夹状态(0x00 弹夹弹出 0x01 弹夹插入)
- {
- //00 弹夹分离,01 上弹夹
- if (ShootCheck.ins) ShootCheck.ins.UpdateTheMagazine(bytes);
- }
- #endregion
- #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 标定键- 双击
- switch (bytes[3])
- {
- case 0x01:
- if (UserSettings.ins.selectDevicesName == "ARTEMIS Pro")
- AimHandler.ins.OnDataReceived(new byte[] { 0x66, 0x31 });
- break;
- case 0x03:
- if (UserSettings.ins.selectDevicesName == "ARTEMIS Pro")
- AimHandler.ins.OnDataReceived(new byte[] { 0x66, 0x32 });
- break;
- case 0x04:
- AimHandler.ins.OnDataReceived(new byte[] { 0x66, 0x31 });
- break;
- case 0x05:
- AimHandler.ins.OnDataReceived(new byte[] { 0x66, 0x32 });
- break;
- case 0x06:
- break;
- }
- }
- #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);
- }
- }
- }
- // 红外射击检测
- byte[] shootBytes = new byte[] { bytes[0], bytes[1], bytes[3], bytes[4], bytes[5] };
- ShootCheck.ins.ShootByInfrared(shootBytes);
- }
- #endregion
- }
|