Pārlūkot izejas kodu

修改了Key长度大于255时,通过判断FE字段进行操作

chenjiangqun 2 gadi atpakaļ
vecāks
revīzija
012d116618

+ 1 - 1
FactoryTool_CShare/Business/CommonMethod.cs

@@ -2445,7 +2445,7 @@ namespace MOKA_Factory_Tools
     {
         //生成CRC32码表
         static uint[] Crc32Table;
-        public static void GetCRC32Table()
+        private static void GetCRC32Table()
         {
             uint Crc;
             Crc32Table = new uint[256];

+ 74 - 50
FactoryTool_CShare/IO/SerialCMD.cs

@@ -8,6 +8,9 @@ using System.Collections;
 using System.Threading;
 using SXLibrary;
 using System.Diagnostics;
+using System.Web.UI.WebControls;
+using System.ComponentModel;
+using CCWin.Win32.Com;
 
 namespace MOKA_Factory_Tools
 {
@@ -517,6 +520,8 @@ namespace MOKA_Factory_Tools
         /// </summary>
         /// <param name="Comport"></param>
         /// <param name="param"></param>
+        /// 
+        /// 
         /// <param name="result"></param>
         /// <param name="data"></param>
         /// <param name="error"></param>
@@ -2843,64 +2848,83 @@ namespace MOKA_Factory_Tools
                     //delay(Comport, CMDID, true);
                     if (mark)
                     {
-                        string Br = "";
-                        string Sr = "";
                         // 分2段2次返回,第2次同样要再次设置超时值;
-                        Comport.ReadTimeout = ReadTimeout;
-                        while (Comport.BytesToRead >= 0)
-                        {
-                            if (Comport.ReadByte() == ACKhead[0])
-                            {
-                                result[5] = ACKhead[0];
-                                break;
-                            }                           
-                        }
-                        if (!TwoLengthBit)
+                        Comport.ReadTimeout = ReadTimeout;                
+                        //等待一段时间确保接收到完整数据
+                        Thread.Sleep(100);
+                        //取一个空间存放第二段
+                        //校验第二段CRC是否正确
+                        //根据长度判断是否有FE字段
+                        byte[] receive2Data = new byte[20480];
+                        //划一个空间用来存放实际长度
+                        int reallyLength=Comport.Read(receive2Data, 0,20480);
+                        //CRC校验
+                        byte crcHighByte = receive2Data[reallyLength - 2];
+                        byte crcLowByte = receive2Data[reallyLength - 1];
+
+                        string crc = CrcTest.Program.CalcCRC16(receive2Data.Take(reallyLength - 2).ToArray());
+
+                        byte[] calCrc = SerialInit.HexToByte(crc);
+                        if(crcHighByte == calCrc[0] && crcLowByte == calCrc[1])
                         {
-                            int k = Comport.ReadByte();
-                            result[6] = (byte)k;
-                            int timeout = 0;
-                            while (Comport.BytesToRead < (k - 2) && timeout < 20)
+                            //根据实际收到的数据判断是否有FE字段
+                            if (reallyLength > 255)
                             {
-                                Thread.Sleep(100);
-                                timeout++;
-                            }
-                            Comport.Read(result, 7, k - 2);
-                            Comport.ReadTimeout = 200;
-                            Br = SerialInit.ByteToHex(result.Skip(8).Take(k - 5).ToArray());
-                            Sr = Encoding.ASCII.GetString(result.Skip(8).Take(k - 5).ToArray());
-                            data = result.Skip(8).Take(k - 5).ToArray();
-                            Log.WriteInfoLog("Received:" + SerialInit.ByteToHex(result.Take(5 + k).ToArray()));
-                        }                           
-                        else
-                        {
-                            int mark1= Comport.ReadByte();                    
-                            result[6] = (byte)mark1;
-                            if (result[6]!=0xFE)
-                            {                           
-                                Log.WriteInfoLog("返回错误,返回的第二段串口值不正确");
-                                return false;
+                                //数据大于255,应该有FE              
+                                if (receive2Data[1] != 0xFE)
+                                {
+                                    Log.WriteErrorLog("数据大于255,协议无FE字段");
+                                    return false;
+                                }
+
+                                //获取数据,获取长度字节,然后判断
+                                uint packetLen = (uint)((receive2Data[2] << 8) | receive2Data[3]);
+                                if (packetLen != reallyLength)
+                                {
+                                    Log.WriteErrorLog("实际长度与读出的长度不符");
+                                    return false;
+                                }
+                                //判断命令是否正确(有无FC字段,有的时候命令应往后移一个
+                                if (receive2Data[receive2Data[4] != 0xFC ? 4 : 5] != CMDID[0] + 1)
+                                {
+                                    Log.WriteErrorLog("收到的命令与发送的命令计算后值不同");
+                                    return false;
+                                }
+                                // 获取数据部分;
+                                int skipLen = receive2Data[4] == 0xFC ? 6 : 5;
+                                data = receive2Data.Skip(skipLen).Take(reallyLength - skipLen - 2).ToArray();
+                                Log.WriteInfoLog("Receive Data:" + SerialInit.ByteToHex(result.Take(5).ToArray()) + SerialInit.ByteToHex(receive2Data.Take(reallyLength).ToArray()));
+                                return true;
                             }
 
-                            int k1 = Comport.ReadByte();
-                            int k2 = Comport.ReadByte();
-                            result[7] = (byte)k1;
-                            result[8] = (byte)k2;
-                            int k = k1 * 256 + k2;
-                            int timeout = 0;
-                            while (Comport.BytesToRead < (k - 4) && timeout < 20)
+                            else
                             {
-                                Thread.Sleep(100);
-                                timeout++;
+                                //获取数据,获取长度字节,然后判断                     
+                                int packetLength = receive2Data[1];
+                                if (packetLength != reallyLength)
+                                {
+                                    Log.WriteErrorLog("实际长度与读出的长度不符");
+                                    return false;
+                                }
+                                //判断命令是否正确(有无FC字段,有的时候命令应往后移一个
+                                if (receive2Data[receive2Data[2] == 0xFC ? 3 : 2] != CMDID[0] + 1)
+                                {
+                                    Log.WriteErrorLog("收到的命令与发送的命令计算后值不同");
+                                    return false;
+                                }
+
+                                int skipLen = receive2Data[2] == 0xFC ? 4 : 3;
+                                data = receive2Data.Skip(skipLen).Take(reallyLength - skipLen - 2).ToArray();
+                                Log.WriteInfoLog("Receive Data:" + SerialInit.ByteToHex(result.Take(5).ToArray()) + SerialInit.ByteToHex(receive2Data.Take(packetLength).ToArray()));
+                                return true;
                             }
-                            Comport.Read(result, 9, k - 4);
-                            Comport.ReadTimeout = 200;
-                            Br = SerialInit.ByteToHex(result.Skip(10).Take(k - 7).ToArray());
-                            Sr = Encoding.ASCII.GetString(result.Skip(10).Take(k - 7).ToArray());
-                            data = result.Skip(10).Take(k - 7).ToArray();
-                            Log.WriteInfoLog("Received:" + SerialInit.ByteToHex(result.Take(5 + k).ToArray()));
                         }
-
+                        else
+                        {
+                            Log.WriteErrorLog("校验错误");
+                            return false;
+                        }
+                                                         
                         Thread.Sleep(NextCommandWaitTime);
                         return true;
                     }