|
|
@@ -10,7 +10,8 @@ public class ArrowSerialPort : MonoBehaviour
|
|
|
private int baudrate = 115200;
|
|
|
private string PortName = "/dev/ttyS0";
|
|
|
private static Dictionary<string, int> _isInit = new();
|
|
|
- public bool testMode = false;
|
|
|
+ public bool testMode = true;
|
|
|
+
|
|
|
private void Awake()
|
|
|
{
|
|
|
if (!_isInit.TryGetValue(gameObject.name, out int instanceID) && instanceID == 0)
|
|
|
@@ -31,6 +32,7 @@ public class ArrowSerialPort : MonoBehaviour
|
|
|
private void Init()
|
|
|
{
|
|
|
_isInit[gameObject.name] = GetInstanceID();
|
|
|
+
|
|
|
#if UNITY_ANDROID && !UNITY_EDITOR
|
|
|
serialPortUtility = gameObject.GetComponent<SerialPortUtilityPro>();
|
|
|
serialPortUtility.OpenMethod = openMode;
|
|
|
@@ -68,10 +70,11 @@ public class ArrowSerialPort : MonoBehaviour
|
|
|
{
|
|
|
SerialPortHelper.ins.OnConnect(arg0.DeviceName);
|
|
|
//应用启动 间隔十秒请求一次设备信息
|
|
|
+ CancelInvoke("RequestDeviceIno");
|
|
|
InvokeRepeating("RequestDeviceIno", 0f, 10f);
|
|
|
LOG($"{PortName} 串口打开成功!");
|
|
|
- // 关闭一次摄像头
|
|
|
- RequestLightState(false);
|
|
|
+ //激光默认打开
|
|
|
+ RequestLightState(true);
|
|
|
}
|
|
|
else if (msg.Equals("CLOSED"))//串口断开
|
|
|
{
|
|
|
@@ -90,19 +93,35 @@ public class ArrowSerialPort : MonoBehaviour
|
|
|
_isInit.Remove(gameObject.name);
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/// <summary>
|
|
|
/// 串口读取二进制流数据(界面挂载调用)
|
|
|
/// </summary>
|
|
|
/// <param name="data"></param>
|
|
|
public void ReadStreamingBinary(object data)
|
|
|
{
|
|
|
- PhraseData(data as byte[]);
|
|
|
+ var realData = new List<byte>();
|
|
|
+ var tempData = data as byte[];
|
|
|
+ for (int i = 0; i < tempData.Length; i++)
|
|
|
+ {
|
|
|
+ realData.Add(tempData[i]);
|
|
|
+ if (tempData.Length > i + 1)
|
|
|
+ {
|
|
|
+ if (tempData[i + 1] == 0XAA) //多条消息 粘包位置
|
|
|
+ {
|
|
|
+ //把上一条发送
|
|
|
+ PhraseData(realData.ToArray());
|
|
|
+ realData.Clear();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ PhraseData(realData.ToArray());
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
public void TestRead(byte[] bytes)
|
|
|
{
|
|
|
- PhraseData(bytes);
|
|
|
+ ReadStreamingBinary(bytes);
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
@@ -166,7 +185,6 @@ public class ArrowSerialPort : MonoBehaviour
|
|
|
|
|
|
#region APP请求
|
|
|
// 异或校验内容:命令+长度+数据内容
|
|
|
-
|
|
|
/// <summary>
|
|
|
/// 设置激光状态
|
|
|
/// </summary>
|
|
|
@@ -202,11 +220,11 @@ public class ArrowSerialPort : MonoBehaviour
|
|
|
data.Add(0x85);//异或校验
|
|
|
data.Add(0x55);//结束码
|
|
|
serialPortUtility?.Write(data.ToArray());
|
|
|
-
|
|
|
+ Debug.Log("发送心跳包");
|
|
|
DevicesHolder.ins?.ShowConnectTip();
|
|
|
}
|
|
|
#endregion
|
|
|
-
|
|
|
+
|
|
|
#region 返回消息处理
|
|
|
/// <summary>
|
|
|
/// 弹夹消息
|
|
|
@@ -233,37 +251,36 @@ public class ArrowSerialPort : MonoBehaviour
|
|
|
{
|
|
|
Debug.Log($"{PortName} 收到设备信息响应!");
|
|
|
var check = bytes[1] + bytes[2] + bytes[3] + bytes[4];//校验:命令+长度+数据内容
|
|
|
- //if (check != bytes[5])
|
|
|
- // LOG("OnDeviceInfoBack 数据校验错误!");
|
|
|
- //else
|
|
|
- //{
|
|
|
- //0x01 HOUYI Pro
|
|
|
- //0x02 ARTEMIS Pro
|
|
|
- //0x03 Pistol 1
|
|
|
- var deviceType = bytes[3];//设备类型
|
|
|
- Debug.Log("设备类型 Device Type: " + deviceType);
|
|
|
- switch (deviceType)
|
|
|
- {
|
|
|
- case 0x01:
|
|
|
- UserSettings.ins.selectDevicesName = "HOUYI Pro";
|
|
|
- DevicesHolder.ins.SwitchDeviceByType(AimDeviceType.HOUYIPRO);
|
|
|
- break;
|
|
|
- case 0x02:
|
|
|
- UserSettings.ins.selectDevicesName = "ARTEMIS Pro";
|
|
|
- DevicesHolder.ins.SwitchDeviceByType(AimDeviceType.ARTEMISPRO);
|
|
|
- break;
|
|
|
- case 0x03:
|
|
|
- UserSettings.ins.selectDevicesName = "Pistol M9";
|
|
|
- DevicesHolder.ins.SwitchDeviceByType(AimDeviceType.Gun);
|
|
|
- break;
|
|
|
- }
|
|
|
- UserSettings.ins.Save();
|
|
|
- //刷新界面
|
|
|
- var setting = FindAnyObjectByType<CustomUIView.BoxUserSettings>();
|
|
|
- setting?.FlushDeviceSelect();
|
|
|
- //}
|
|
|
+ if (HomeView.ins == null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ //0x01 HOUYI Pro
|
|
|
+ //0x02 ARTEMIS Pro
|
|
|
+ //0x03 Pistol 1
|
|
|
+ var deviceType = bytes[3];//设备类型
|
|
|
+ Debug.Log("设备类型 Device Type: " + deviceType);
|
|
|
+ switch (deviceType)
|
|
|
+ {
|
|
|
+ case 0x01:
|
|
|
+ UserSettings.ins.selectDevicesName = "HOUYI Pro";
|
|
|
+ DevicesHolder.ins.SwitchDeviceByType(AimDeviceType.HOUYIPRO);
|
|
|
+ break;
|
|
|
+ case 0x02:
|
|
|
+
|
|
|
+ UserSettings.ins.selectDevicesName = "ARTEMIS Pro";
|
|
|
+ DevicesHolder.ins.SwitchDeviceByType(AimDeviceType.ARTEMISPRO);
|
|
|
+ break;
|
|
|
+ case 0x03:
|
|
|
+ UserSettings.ins.selectDevicesName = "Pistol M9";
|
|
|
+ DevicesHolder.ins.SwitchDeviceByType(AimDeviceType.Gun);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ UserSettings.ins.Save();
|
|
|
+ //刷新界面
|
|
|
+ var setting = FindAnyObjectByType<CustomUIView.BoxUserSettings>();
|
|
|
+ setting?.FlushDeviceSelect();
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/// <summary>
|
|
|
/// 射击消息
|
|
|
/// </summary>
|
|
|
@@ -272,11 +289,11 @@ public class ArrowSerialPort : MonoBehaviour
|
|
|
LOG($"{PortName} 收到设备射击消息!");
|
|
|
|
|
|
var check = bytes[1] + bytes[2] + bytes[3] + bytes[4];//校验:命令+长度+数据内容
|
|
|
- //if (check != bytes[5])
|
|
|
- //{
|
|
|
- // LOG("OnDeviceShoot 数据校验错误!");
|
|
|
- //}else
|
|
|
- SerialPortHelper.shoot?.Invoke(bytes);
|
|
|
+ //if (check != bytes[5])
|
|
|
+ //{
|
|
|
+ // LOG("OnDeviceShoot 数据校验错误!");
|
|
|
+ //}else
|
|
|
+ SerialPortHelper.shoot?.Invoke(bytes);
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
@@ -299,6 +316,6 @@ public class ArrowSerialPort : MonoBehaviour
|
|
|
RequestDeviceIno();
|
|
|
SerialPortHelper.aim?.Invoke(bytes);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
#endregion
|
|
|
}
|