Browse Source

弓箭串口通信

17600099689 1 year ago
parent
commit
f469697a45

+ 25 - 14
Assets/BowArrow/Scripts/ArrowSerialPort.cs

@@ -8,7 +8,7 @@ public class ArrowSerialPort : MonoBehaviour
     private SerialPortUtilityPro serialPortUtility;
     private SerialPortUtilityPro.OpenSystem openMode = SerialPortUtilityPro.OpenSystem.PCI;
     private int baudrate = 115200;
-    private string PortName = "/dev/ttyS1";
+    private string PortName = "/dev/ttyS0";
     private static Dictionary<string, int> _isInit = new();
     public bool testMode = false;
     private void Awake()
@@ -108,7 +108,12 @@ public class ArrowSerialPort : MonoBehaviour
     /// <param name="bytes"></param>
     private void PhraseData(byte[] bytes)
     {
-        LOG($"{PortName} 收到串口信息!");
+        string msg = string.Empty;
+        for (int i = 0; i < bytes.Length; i++)
+        {
+            msg += bytes[i].ToString("x2") + " ";
+        }
+        LOG($"{PortName} 收到串口信息  msg={msg}!");
         if (bytes[0] == 0xAA)
         {
             var cmdID = bytes[1];
@@ -154,10 +159,10 @@ public class ArrowSerialPort : MonoBehaviour
     {
         Debug.Log($"{PortName} 收到设备信息响应!");
         var check = bytes[1] + bytes[2] + bytes[3] + bytes[4];//校验:命令+长度+数据内容
-        if (check != bytes[5])
-            LOG("OnDeviceInfoBack 数据校验错误!");
-        else
-        {
+        //if (check != bytes[5])
+        //    LOG("OnDeviceInfoBack 数据校验错误!");
+        //else
+        //{
             //0x01 HOUYI Pro
             //0x02 ARTEMIS Pro
             //0x03 Pistol 1
@@ -182,7 +187,7 @@ public class ArrowSerialPort : MonoBehaviour
             //刷新界面
             var setting = FindAnyObjectByType<CustomUIView.BoxUserSettings>();
             setting?.FlushDeviceSelect();
-        }
+        //}
     }
     
     /// <summary>
@@ -193,10 +198,10 @@ public class ArrowSerialPort : MonoBehaviour
         LOG($"{PortName} 收到设备射击消息!");
 
         var check = bytes[1] + bytes[2] + bytes[3] + bytes[4];//校验:命令+长度+数据内容
-        if (check != bytes[5])
-        {
-            LOG("OnDeviceShoot 数据校验错误!");
-        }else
+        //if (check != bytes[5])
+        //{
+        //    LOG("OnDeviceShoot 数据校验错误!");
+        //}else
             SerialPortHelper.shoot?.Invoke(bytes);
     }
 
@@ -208,11 +213,17 @@ public class ArrowSerialPort : MonoBehaviour
     {
         Debug.Log($"{PortName} 收到设备按键消息!");
         var check = bytes[1] + bytes[2] + bytes[3];//校验:命令+长度+数据内容
-        if (check != bytes[4])
+        //if (check != bytes[4])
+        //{
+        //    LOG("OnDeviceButton 数据校验错误!");
+        //}
+        //else
+        if (bytes[3] == 0x01 || bytes[3] == 0x02 || bytes[3] == 0x03)
         {
-            LOG("OnDeviceButton 数据校验错误!");
+            //开机
+            RequestDeviceIno();
+            return;
         }
-        else
             SerialPortHelper.aim?.Invoke(bytes);
     }
     

+ 1 - 0
Assets/BowArrow/Scripts/SerialPortHelper.cs

@@ -18,6 +18,7 @@ public class SerialPortHelper : MonoBehaviour
                 _ins = go.AddComponent<SerialPortHelper>();
                 DontDestroyOnLoad(go);
                 SerialPortExampleGroup = FindObjectsOfType<SerialPortExample>();
+                arrowPort = FindObjectOfType<ArrowSerialPort>();
             }
             return _ins;
         }

+ 3 - 3
Assets/BowArrow/Scripts/SerialPortTest.cs

@@ -36,10 +36,10 @@ public class SerialPortTest : MonoBehaviour
 
         switch (portIndex)
         {
-            case 1://묶숫눔왯
+            case 0://묶숫눔왯
                 targetPort.TestRead(data.ToArray());
                 break;
-            case 0://尻귑분綾
+            case 4://尻귑분綾
                 foreach (var item in SerialPortExampleGroup)
                 {
                     if (item.name.Contains("com0"))
@@ -49,7 +49,7 @@ public class SerialPortTest : MonoBehaviour
                     }
                 }
                 break;
-            case 8://尻귑샙
+            case 6://尻귑샙
                 foreach (var item in SerialPortExampleGroup)
                 {
                     if (item.name.Contains("com8"))