|
@@ -59,6 +59,8 @@ namespace UAS_MES_NEW.Packing
|
|
|
public Packing_PackageCollectionWeigh()
|
|
public Packing_PackageCollectionWeigh()
|
|
|
{
|
|
{
|
|
|
InitializeComponent();
|
|
InitializeComponent();
|
|
|
|
|
+ // 与其他称重窗体保持一致:串口数据在子线程读取,需直接更新 weight/pa_remark 等控件
|
|
|
|
|
+ CheckForIllegalCrossThreadCalls = false;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
//创建打印进程
|
|
//创建打印进程
|
|
@@ -99,6 +101,8 @@ namespace UAS_MES_NEW.Packing
|
|
|
GetData = true;
|
|
GetData = true;
|
|
|
serialPort1.PortName = ComList.Text;
|
|
serialPort1.PortName = ComList.Text;
|
|
|
serialPort1.BaudRate = int.Parse(Baurate.Text);
|
|
serialPort1.BaudRate = int.Parse(Baurate.Text);
|
|
|
|
|
+ // 设置读取超时,避免 ReadLine 在电子秤未发送换行符时无限阻塞导致界面假死
|
|
|
|
|
+ serialPort1.ReadTimeout = 1000;
|
|
|
serialPort1.Open();
|
|
serialPort1.Open();
|
|
|
thread.Start();
|
|
thread.Start();
|
|
|
}
|
|
}
|
|
@@ -132,17 +136,20 @@ namespace UAS_MES_NEW.Packing
|
|
|
{
|
|
{
|
|
|
try
|
|
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)
|
|
catch (Exception)
|
|
|
{
|
|
{
|
|
|
GetData = false;
|
|
GetData = false;
|
|
@@ -613,13 +620,23 @@ namespace UAS_MES_NEW.Packing
|
|
|
|
|
|
|
|
private void Make_PackageCollectionWeigh_FormClosing(object sender, FormClosingEventArgs e)
|
|
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();
|
|
//thread.Abort();
|
|
|
}
|
|
}
|
|
|
|
|
|