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(); DontDestroyOnLoad(go); SerialPortExampleGroup = FindObjectsOfType(); arrowPort = FindObjectOfType(); } return _ins; } } public static System.Action aim; public static System.Action shoot; public static System.Action 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 ///// ///// 获取串口实例(0串口和8串口 4替代0 6替代8) ///// //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; //} /// /// 获取串口实例(4,6) /// 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; } /// /// 获取串口实例(1串口) /// 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 按键 /// /// /// /// 0-起始码 1-序号 2-长度 3-按键类型 4-异或校验 5-结束码 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 dataBytes = new List(); 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 }