Procházet zdrojové kódy

分离出ACK响应内容处理。

jianfeng1.wang před 2 roky
rodič
revize
9c87402076
1 změnil soubory, kde provedl 63 přidání a 61 odebrání
  1. 63 61
      FactoryTool_CShare/IO/SerialCMD.cs

+ 63 - 61
FactoryTool_CShare/IO/SerialCMD.cs

@@ -2945,6 +2945,67 @@ namespace MOKA_Factory_Tools
             return true;
         }
 
+        private static bool ParseACK(SerialPort Comport, ref byte[] receiveData, out string error)
+        {
+            error = "";
+            // 设置读超时;
+            Comport.ReadTimeout = ReadTimeout;
+            if (Comport.ReadByte() == ACKhead[0])
+            {
+                if (Comport.ReadByte() == ACKhead[1])
+                {
+                    int onebit = Comport.ReadByte();
+                    if (onebit == ACKhead[2])
+                    {
+                        if (Comport.ReadByte() == ACKhead[3])
+                        {
+                            if (Comport.ReadByte() == ACKhead[4])
+                            {
+                                for (int j = 0; j <= 4; j++)
+                                {
+                                    receiveData[j] = ACKhead[j];
+                                }
+                                // ACK返回正确;
+                                return true;
+                            }
+                        }
+                    }
+                    else if (onebit == ACKhead2[2])
+                    {
+                        if (Comport.ReadByte() == ACKhead2[3])
+                        {
+                            if (Comport.ReadByte() == ACKhead2[4])
+                            {
+                                for (int j = 0; j <= 4; j++)
+                                {
+                                    receiveData[j] = ACKhead2[2];
+                                }
+                                error = "命令错误或无法识别数据返回";
+                                Log.WriteInfoLog("Received ErrorACK:" + SerialInit.ByteToHex(ACKhead2) + "\r\n" + error);
+                            }
+                        }
+                    }
+                    else if (onebit == ACKhead3[2])
+                    {
+                        if (Comport.ReadByte() == ACKhead3[3])
+                        {    
+                            if (Comport.ReadByte() == ACKhead3[4])
+                            {
+                                for (int j = 0; j <= 4; j++)
+                                {
+                                    receiveData[j] = ACKhead3[2];
+                                }
+                                Log.WriteInfoLog("Received ErrorACK:" + SerialInit.ByteToHex(ACKhead3) + "\r\n" + error);
+                                error = "CRC16校验错误返回";
+                            }
+                        }
+                    }
+                }
+            }
+            
+            return false;
+        }
+
         /// <summary>
         /// 通讯逻辑
         /// </summary>
@@ -3000,67 +3061,8 @@ namespace MOKA_Factory_Tools
                 Comport.Write(wdata, 0, wdata.Length);
                 s += SerialInit.ByteToHex(wdata);
                 Log.WriteInfoLog("Write:" + s);
-                bool mark = false;
-                // 设置读超时;
-                Comport.ReadTimeout = ReadTimeout;
-                //delay(Comport, CMDID);//已有ReadTimeout了,这里多余;
-                // ReadTimeout对ReadByte生效的话,所有ReadByte都会有超时响应,如果异常存在会导致更长的延时?
-                while (true)
-                {
-                    if (Comport.ReadByte() == ACKhead[0])
-                    {
-                        if (Comport.ReadByte() == ACKhead[1])
-                        {
-                            int onebit = Comport.ReadByte();
-                            if (onebit == ACKhead[2])
-                            {
-                                if (Comport.ReadByte() == ACKhead[3])
-                                {    
-                                    if (Comport.ReadByte() == ACKhead[4])
-                                    {
-                                        for (int j = 0; j <= 4; j++)
-                                        {
-                                            result[j] = ACKhead[j];
-                                        }
-                                        mark = true;    // 表示第一段接受正确;
-                                        break;
-                                    }
-                                }
-                            }
-                            else if (onebit == ACKhead2[2])
-                            {
-                                if (Comport.ReadByte() == ACKhead2[3])
-                                    if (Comport.ReadByte() == ACKhead2[4])
-                                    {
-                                        for (int j = 0; j <= 4; j++)
-                                        {
-                                            result[j] = ACKhead2[2];
-                                        }
-                                        error = "命令错误或无法识别数据返回";
-                                        Log.WriteInfoLog("Received ErrorACK:" + SerialInit.ByteToHex(ACKhead2) + "\r\n" + error);
-                                        return false;
-                                    }
-                            }
-                            else if (onebit == ACKhead3[2])
-                            {
-                                if (Comport.ReadByte() == ACKhead3[3])
-                                    if (Comport.ReadByte() == ACKhead3[4])
-                                    {
-                                        for (int j = 0; j <= 4; j++)
-                                        {
-                                            result[j] = ACKhead3[2];
-                                        }
-                                        Log.WriteInfoLog("Received ErrorACK:" + SerialInit.ByteToHex(ACKhead3) + "\r\n" + error);
-                                        error = "CRC16校验错误返回";
-                                        return false;
-                                    }
-                            }
-                        }
-                    }
-                    //delay(Comport, CMDID);
-                }
-
-                if ( !mark )
+                
+                if ( !ParseACK(Comport, ref result, out error) )
                 {
                     Log.WriteErrorLog("Received Error:" + SerialInit.ByteToHex(result.Take(5).ToArray()));
                     return false;