Эх сурвалжийг харах

去除delay函数,同时将ReadTimeout设置成可配置。
同时,新增NetCommandWaitTime时间。

sat23 3 жил өмнө
parent
commit
f88fc851d4

+ 21 - 8
SCBC Factory Tools/IO/SerialCMD.cs

@@ -13,6 +13,18 @@ namespace MOKA_Factory_Tools
     
     class SerialCMD
     {
+        /// <summary>
+        /// 串口指令读超时值,单位毫秒;
+        /// 默认:3000ms
+        /// </summary>
+        public static int ReadTimeout { get; set; } = 3000;
+
+        /// <summary>
+        /// 串口完成一次指令的Write、Read后,下一条指令等待多久开始执行(即执行完当前指令后,等待多久函数才返回);
+        /// 单位:毫秒,默认200毫秒
+        /// </summary>
+        public static int NextCommandWaitTime { get; set; } = 100;
+
         public static int SerialTimeout { get; set; }
         static byte[] ACKhead = new byte[5] { 0xAB, 0x05, 0x0A, 0xDF, 0x4E };//命令可执行数据返回
         static byte[] ACKhead2 = new byte[5] { 0xAB, 0x05, 0x0E, 0x9F, 0xCA };//命令错误或无法识别数据返回
@@ -2564,8 +2576,10 @@ namespace MOKA_Factory_Tools
                 s += SerialInit.ByteToHex(wdata);
                 Log.WriteInfoLog("Write:" + s);
                 bool mark = false;
-                Comport.ReadTimeout = 1500;
-                delay(Comport, CMDID);
+                // 设置读超时;
+                Comport.ReadTimeout = ReadTimeout;
+                // delay(Comport, CMDID);//已有ReadTimeout了,这里多余;
+                // ReadTimeout对ReadByte生效的话,所有ReadByte都会有超时响应,如果异常存在会导致更长的延时?
                 while (true)
                 {
                     if (Comport.ReadByte() == ACKhead[0])
@@ -2616,12 +2630,12 @@ namespace MOKA_Factory_Tools
                             }
                         }
                     }
-                    delay(Comport, CMDID);
+                    //delay(Comport, CMDID);
                 }
                 CheckTCL2Receive:
                 if (receive2)
                 {
-                    delay(Comport, CMDID, true);
+                    //delay(Comport, CMDID, true);
                     if (mark)
                     {
                         string Br = "";
@@ -2675,8 +2689,7 @@ namespace MOKA_Factory_Tools
                             Log.WriteInfoLog("Received:" + SerialInit.ByteToHex(result.Take(5 + k).ToArray()));
                         }
 
-                        if(waitTime>0)
-                            Thread.Sleep(waitTime);
+                        Thread.Sleep(NextCommandWaitTime);
                         return true;
                     }
                     else
@@ -2687,8 +2700,7 @@ namespace MOKA_Factory_Tools
                 else
                 {
                     Log.WriteInfoLog("Received ACK:" + SerialInit.ByteToHex(result.Take(5).ToArray()));
-                    if (waitTime > 0)
-                        Thread.Sleep(waitTime);
+                    Thread.Sleep(NextCommandWaitTime);
                     return true;
                 }
             }  
@@ -2705,6 +2717,7 @@ namespace MOKA_Factory_Tools
         {
             int l = 0;
             int num = SerialTimeout * 20;
+            // CMD = 0x10表示EnterFactory指令;
             if (CMDID.Length == 1 && CMDID[0] == 0x10)
                 num = 20;