Browse Source

串口事件BUG调整

章政 6 years ago
parent
commit
763cc91269
1 changed files with 30 additions and 4 deletions
  1. 30 4
      UAS-出货标签管理(泽天)/UAS_出货标签管理.cs

+ 30 - 4
UAS-出货标签管理(泽天)/UAS_出货标签管理.cs

@@ -14,6 +14,7 @@ using FastReport;
 using System.Linq;
 using System.Reflection;
 using UAS_LabelMachine.CustomControl;
+using System.ComponentModel;
 
 namespace UAS_LabelMachine
 {
@@ -2052,6 +2053,18 @@ namespace UAS_LabelMachine
 
         private void UAS_出货标签打印_Click(object sender, EventArgs e)
         {
+            if (Com.IsOpen && Com.PortName == cis.Controls["PortName"].Text)
+            {
+                MessageBox.Show("串口" + Com.PortName + "已打开");
+                return;
+            }
+            else
+            {
+                if (Com.IsOpen)
+                {
+                    Com.Close();
+                }
+            }
             int BaudRate = 0;
             Com.PortName = cis.Controls["PortName"].Text;
             if (int.TryParse(cis.Controls["BaudRate"].Text, out BaudRate))
@@ -2068,10 +2081,23 @@ namespace UAS_LabelMachine
 
         private void Com_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
         {
-            int len = Com.BytesToRead;
-            byte[] readBuffer = new byte[len];
-            Com.Read(readBuffer, 0, len); //将数据读入缓存
-            Weight.Text = reg.Match(Encoding.ASCII.GetString(readBuffer, 0, len)).Value; //获取出入库产品编号
+            try
+            {
+                int len = Com.BytesToRead;
+                byte[] readBuffer = new byte[len];
+                Com.Read(readBuffer, 0, len); //将数据读入缓存
+                Weight.Text = reg.Match(Encoding.ASCII.GetString(readBuffer, 0, len)).Value;
+                //读不到数据的时间删掉
+                if (Weight.Text == "")
+                {
+                    Com.DataReceived -= Com_DataReceived;
+                    return;
+                }
+            }
+            catch (Exception)
+            {
+                return;
+            }
         }
     }
 }