callm 2 дней назад
Родитель
Сommit
330e27073f

+ 15 - 0
UAS_MES_JWS/.workbuddy/memory/2026-10-26.md

@@ -0,0 +1,15 @@
+# 2026-10-26 工作日志
+
+## 修复 Packing_PackageCollectionWeigh 串口假死问题
+- 文件:`FunctionCode/Packing/Packing_PackageCollectionWeigh.cs`
+- 现象:连接串口后隔一段时间界面假死,weight 不再刷新
+- 根因:
+  1. 构造函数漏写 `CheckForIllegalCrossThreadCalls = false`(同项目其他称重窗体如 Packing_BigBoxWeight/Packing_CartonBoxWeigh 均有此行),导致子线程访问 weight.Text/pa_remark 抛 InvalidOperationException 被吞,线程提前退出
+  2. `serialPort1.ReadLine()` 未设 ReadTimeout,默认 InfiniteTimeout,电子秤发不完整帧时无限阻塞,线程挂起
+  3. `catch(Exception){ GetData=false; }` 一刀切,超时也被当致命错误退出循环
+- 改动:
+  1. 构造函数补 `CheckForIllegalCrossThreadCalls = false;`
+  2. Load 中开串口前设 `serialPort1.ReadTimeout = 1000;`
+  3. getSerialData 拆分 TimeoutException(continue 继续循环)与其它异常(退出)
+  4. FormClosing 改为先 GetData=false 再关串口再 Interrupt,加 try-catch 保护
+- 注意:同类窗体 Packing_PackageCollectionWeigh_ReadLine / _ReadLine1 用的是 BytesToRead+Read(buffer) 方式,阻塞风险不同,本次未动

+ 32 - 15
UAS_MES_JWS/FunctionCode/Packing/Packing_PackageCollectionWeigh.cs

@@ -59,6 +59,8 @@ namespace UAS_MES_NEW.Packing
         public Packing_PackageCollectionWeigh()
         {
             InitializeComponent();
+            // 与其他称重窗体保持一致:串口数据在子线程读取,需直接更新 weight/pa_remark 等控件
+            CheckForIllegalCrossThreadCalls = false;
         }
 
         //创建打印进程
@@ -99,6 +101,8 @@ namespace UAS_MES_NEW.Packing
                 GetData = true;
                 serialPort1.PortName = ComList.Text;
                 serialPort1.BaudRate = int.Parse(Baurate.Text);
+                // 设置读取超时,避免 ReadLine 在电子秤未发送换行符时无限阻塞导致界面假死
+                serialPort1.ReadTimeout = 1000;
                 serialPort1.Open();
                 thread.Start();
             }
@@ -132,17 +136,20 @@ namespace UAS_MES_NEW.Packing
                         {
                             try
                             {
-                                weight.Text = re.Match(serialPort1.ReadLine().Trim()).Groups[0].Value;
-                                pa_remark.AppendText(weight.Text + "\n");
-                                //int len = serialPort1.BytesToRead;
-                                //Byte[] readBuffer = new Byte[len];
-                                //serialPort1.Read(readBuffer, 0, len); //将数据读入缓存
-                                //string weigh = Encoding.Default.GetString(readBuffer);
-                                //if (weigh != "")
-                                //{
-                                //    weight.Text = re.Matches(weigh)[0].Value;
-                                //}
+                                // 先把一行读出来,再更新UI,避免UI异常影响读取流程
+                                string line = serialPort1.ReadLine().Trim();
+                                if (line != "")
+                                {
+                                    weight.Text = re.Match(line).Groups[0].Value;
+                                    pa_remark.AppendText(weight.Text + "\n");
+                                }
+                            }
+                            // 读取超时属于正常现象(电子秤未持续发送数据),忽略后继续循环
+                            catch (TimeoutException)
+                            {
+                                continue;
                             }
+                            // 其他异常(串口被拔出、底层IO错误等)才退出循环
                             catch (Exception)
                             {
                                 GetData = false;
@@ -613,13 +620,23 @@ namespace UAS_MES_NEW.Packing
 
         private void Make_PackageCollectionWeigh_FormClosing(object sender, FormClosingEventArgs e)
         {
-            if (serialPort1.IsOpen)
+            // 先通知读取线程停止,再关闭串口,避免线程阻塞在ReadLine上
+            GetData = false;
+            try
             {
-                GetData = false;
-                serialPort1.Close();
-                SystemInf.OpenPort.Remove(serialPort1.PortName);
-                thread.Interrupt();
+                if (serialPort1.IsOpen)
+                {
+                    serialPort1.Close();
+                    SystemInf.OpenPort.Remove(serialPort1.PortName);
+                }
+            }
+            catch (Exception) { }
+            try
+            {
+                if (thread != null && thread.IsAlive)
+                    thread.Interrupt();
             }
+            catch (Exception) { }
             //thread.Abort();
         }
 

+ 1 - 1
UAS_MES_JWS/UAS_MES_JWS.csproj

@@ -4,7 +4,7 @@
   <PropertyGroup>
     <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
     <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
-    <ProjectGuid>{AF54EC25-011C-4023-8236-A62C017B55D2}</ProjectGuid>
+    <ProjectGuid>{836090E8-F024-405E-BBC2-9461CF5E1B4A}</ProjectGuid>
     <OutputType>WinExe</OutputType>
     <AppDesignerFolder>Properties</AppDesignerFolder>
     <RootNamespace>UAS_MES_NEW</RootNamespace>