Prechádzať zdrojové kódy

feat: 换设备部分优化
1. 在测试完成后断开串口, 防止更换设备导致的异常

kindring 1 týždeň pred
rodič
commit
5b61f7fa91

+ 24 - 2
bird_tool/SerialManager.cs

@@ -8,6 +8,10 @@ namespace Bird_tool
 {
     class SerialManager
     {
+        public delegate void DisconnectHandler(bool isError);
+
+        public event DisconnectHandler OnDisconnect;
+
         private SerialPort _uart = new SerialPort();
         private int MAX_BUFFER_SIZE = 4096; // 最大缓冲区大小
 
@@ -34,7 +38,7 @@ namespace Bird_tool
 
         public bool Connect(string portName, int baudRate = 115200)
         {
-            if (_uart.IsOpen) return false;
+            if (_uart.IsOpen) return true;
 
             _uart.PortName = portName;
             _uart.BaudRate = baudRate;
@@ -54,16 +58,23 @@ namespace Bird_tool
             }
         }
 
-        public void Disconnect()
+        public void Disconnect(bool isError = false)
         {
             if (_uart.IsOpen)
             {
                 _uart.DataReceived -= CommDataReceived;
                 _uart.Close();
+                OnDisconnect?.Invoke(isError);
             }
             _tmp_text = ""; // 清空缓冲区
         }
 
+        public bool ReOpen()
+        {
+            return Connect(_uart.PortName, _uart.BaudRate);
+        }
+
+
         public bool IsOpen ()
         {
             return _uart.IsOpen;
@@ -92,9 +103,20 @@ namespace Bird_tool
                     ProcessReceivedData(receivedText);
                 }
             }
+            catch (InvalidOperationException ex)
+            {
+                // 专门捕获串口断开异常
+                ErrorHandle($"串口异常断开: {ex.Message}", ex);
+                Disconnect(true);  // 确保资源清理
+            }
             catch (Exception ex)
             {
                 ErrorHandle("串口接收异常", ex);
+                if (ex is InvalidOperationException || ex is System.IO.IOException)
+                {
+                    ErrorHandle($"串口意外断开: {ex.Message}", ex);
+                    Disconnect(true);  // 确保资源清理
+                }
             }
         }
 

+ 7 - 2
bird_tool/TestEngine.cs

@@ -417,6 +417,7 @@ namespace Bird_tool
                 // 清理资源
                 if (_serialManager != null)
                 {
+                    _serialManager.Disconnect();
                     _serialManager.OnLineReceived -= HandleResponse;
                 }
 
@@ -448,6 +449,11 @@ namespace Bird_tool
                     step.stepStatus = StepStatus.NotRun;
                     step.RetryCount = 0;
                 }
+                if (!_serialManager.ReOpen())
+                {
+                    OnLog?.Invoke("无法打开串口", LogLevel.error);
+                    OnLogShow?.Invoke("串口打开失败");
+                }
                 _serialManager.OnLineReceived += HandleResponse;
                 EnqueueTask(async ct =>
                 {
@@ -477,8 +483,7 @@ namespace Bird_tool
                     return false;
                 }
                 enableMakeResult = true;
-                StartTestWithSteps(_step_map);
-                return true;
+                return StartTestWithSteps(_step_map); ;
             }
         }
         public bool StartTestFromGroup(string groupId)

+ 1 - 1
bird_tool/bird_tool.Designer.cs

@@ -158,7 +158,7 @@ namespace Bird_tool
                 TabIndex = 4
             };
             progressPanel.OnLogRequested += (s, ev) => ShowLogWindow();
-            progressPanel.OnStopRequested += (s, ev) => StopTest();
+            progressPanel.OnStopRequested += (s, ev) => StopTest(true);
             progressPanel.OnStartRequested += (s, ev) => start_query_test();
             progressPanel.OnReturnRequested += (s, ev) => re_main();
             progressPanel.StepList.OnStepButtonClick += setpStart;

+ 18 - 12
bird_tool/bird_tool.cs

@@ -295,6 +295,12 @@ namespace Bird_tool
                 }
             }
             //_serialManager.OnLineReceived += _handle_received_line;
+            _serialManager.OnDisconnect += (isError) =>
+            {
+                _uart_ui_change(false);
+                // 判断是否在测试
+                StopTest(isError);
+            };
         }
         private void load_app_config()
         {
@@ -410,7 +416,7 @@ namespace Bird_tool
             if (_serialManager.IsOpen())
             {
                 // 调用停止函数
-                StopTest();
+                StopTest(true);
                 _serialManager.Disconnect();
                 System.Threading.Thread.Sleep(100);
                 _uart_ui_change(false);
@@ -754,7 +760,6 @@ namespace Bird_tool
             progressPanel.Visible = false;
         }
 
-
         private void ui_show_test_panpel()
         {
             ui_hide_start_panpel();
@@ -794,7 +799,7 @@ namespace Bird_tool
             if (!_testExecutor.InitTest(steps))
             {
                 Log_show("无法创建测试表");
-                StopTest();
+                StopTest(false);
                 return;
             }
             ui_show_test_panpel();
@@ -858,10 +863,10 @@ namespace Bird_tool
             }
             
         }
-        private void StopTest()
+        private void StopTest(bool isQuery = true)
         {
             // 停止测试
-            if (_testExecutor.isStart)
+            if (isQuery && _testExecutor.isStart)
             {
                 if (MessageBox.Show("确定要停止当前测试吗?", "确认停止",
                 MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
@@ -915,7 +920,7 @@ namespace Bird_tool
 
 
                 // 按钮长按功能测试
-                /**
+                ///**
                 new TestStepConfig
                 {
                     GroupId = "btn_long",
@@ -939,7 +944,7 @@ namespace Bird_tool
                     DelayBefore = 1000,
                     MaxRetries = 3,
                 },
-                */
+                //*/
 
                 // 按钮双击功能测试
                 new TestStepConfig
@@ -1177,7 +1182,7 @@ namespace Bird_tool
                 },
 
                 // 音频测试
-                /**
+                ///**
                 new TestStepConfig
                 {
                     GroupId = "Audio_test",
@@ -1239,7 +1244,7 @@ namespace Bird_tool
                         // 失败时重新录制
                     }
                 },
-                */
+                //*/
                 
                 // 补光灯测试
                 new TestStepConfig
@@ -1312,7 +1317,7 @@ namespace Bird_tool
                     GroupId = "wifi_test",
                     GroupName = "WiFi测试",
                     ShowStep = false,
-                    Name = "配置WiFi",
+                    Name = "配置WiFi账号",
                     Tips = "自动配置wifi信息",
                     Action = ActionMode.SetVal,
                     VariableNames = { "ssid", "password" },
@@ -1322,8 +1327,9 @@ namespace Bird_tool
                 {
                     GroupId = "wifi_test",
                     ShowStep = false,
-                    Name = "配置设备激活码",
-                    Tips = "自动配置wifi信息",
+                    Name = "配置设备wifi密码",
+                    Tips = "自动配置wifi账号",
+                    PrivateCammand = true,
                     Action = ActionMode.SetVal,
                     VariableNames = { "activeCode" },
                     VariableValues = { wifi_act },