Browse Source

校时封包

yinyujing 5 years ago
parent
commit
53f630125b

+ 16 - 5
NB_IOT_TCP_HP_SOCKET/MainForm_TCP.cs

@@ -321,10 +321,9 @@ namespace NB_IOT_TCP_HP_SOCKET
                         // 判断包类型 心跳或数据包
                         if (psp.Type == FrameType.GPRS_Link)
                         {
-                            byte[] buffer2;
                             protocol2 = new PSProtocol(psp.DestNumber, psp.SourceNumber, new byte[] { 0x55 });
                             protocol2.Type = psp.Type;
-                            if (!ProtocolAnalysisTools.OuterPack(protocol2, out buffer2).Result)
+                            if (!ProtocolAnalysisTools.OuterPack(protocol2, out byte[] buffer2).Result)
                             {
 
                                 AddMsg($" > [{clientInfo.ConnId},OnReceive] -> {clientInfo.IpAddress}:{clientInfo.Port} {psp.SourceNumber} ({bytes.Length} bytes) 心跳回应打包失败 ", lbReceiveData);
@@ -714,9 +713,21 @@ namespace NB_IOT_TCP_HP_SOCKET
                                 // 如果是最后一包则发送校时指令
                                 if (cm.IsEnd)
                                 {
-                                    byte[] buffer = RandomTime();
-                                    AddMsg($" > [{clientInfo.ConnId},OnSend] -> {clientInfo.IpAddress}:{clientInfo.Port} {sourceNumber} ({buffer.Length} bytes) CD_CellMonitor最后一包校时 \n {BitConverter.ToString(buffer, 0, buffer.Length).Replace("-", " ")} ", lbReceiveData);
-                                    server.Send(clientInfo.ConnId, buffer, buffer.Length);// 发送数据
+                                    PSProtocol protocol2 = new PSProtocol(psp.DestNumber, psp.SourceNumber, RandomTime());
+                                    protocol2.Type = psp.Type;
+                                    protocol2.PackNum = psp.PackNum;
+
+                                    if (!ProtocolAnalysisTools.OuterPack(protocol2, out byte[] buffer3).Result)
+                                    {
+                                        AddMsg($" > [{clientInfo.ConnId},OnSend] -> {clientInfo.IpAddress}:{clientInfo.Port} {sourceNumber} ({psp.Data.Length} bytes) CD_CellMonitor最后一包校时打包失败 \n", lbReceiveData);
+
+                                    }
+                                    else
+                                    {
+
+                                        AddMsg($" > [{clientInfo.ConnId},OnSend] -> {clientInfo.IpAddress}:{clientInfo.Port} {sourceNumber} ({psp.Data.Length} bytes) CD_CellMonitor最后一包校时 \n {BitConverter.ToString(buffer3, 0, buffer3.Length).Replace("-", " ")} ", lbReceiveData);
+                                        server.Send(clientInfo.ConnId, buffer3, buffer3.Length);// 发送数据
+                                    }
                                 }
 
                                 List<HistoryRecord> list = new List<HistoryRecord>();

+ 0 - 1
NB_IOT_TCP_HP_SOCKET/WWKJUtil/HexToValue.cs

@@ -717,7 +717,6 @@ namespace NB_IOT_TCP_HP_SOCKET.WWKJUtil
 
         public ResMsg ConvertData(byte[] bytInPack, int intStartIndex, string strConvertType, string strConvertParam, out string strReturnData)
         {
-            intStartIndex = intStartIndex;
             ResMsg msg = new ResMsg();
             if (bytInPack.Length == 0)
             {

+ 1 - 1
NB_IOT_TCP_HP_SOCKET/WWKJUtil/LogUtil.cs

@@ -33,7 +33,7 @@ namespace NB_IOT_TCP_HP_SOCKET.WWKJUtil
                     sw = new StreamWriter(fs, Encoding.UTF8);
                     sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff") + "     " + debugstr + "\r\n");
                 }
-                catch(Exception ex)
+                catch(Exception)
                 {
 
                 }

+ 32 - 111
NB_IOT_TCP_HP_SOCKET/WWKJUtil/ProtocolAnalysisTools.cs

@@ -176,82 +176,6 @@ namespace NB_IOT_TCP_HP_SOCKET.WWKJUtil
             return new ResMsg(true, "模块ID:" + psp.SourceNumber + ",信息:" + message);
         }
 
-        /// <summary>
-        /// 验证内包数据包 并简单拆包  UpSimplePack(ClientInfo info,byte[] originData)
-        /// </summary>
-        /// <param name="info">自带的附加数据的类</param>
-        /// <param name="originData">接收的数据包 byte数组</param>
-        public static void UpSimplePack(ClientInfo info, byte[] originData)
-        {
-            // 定义变量
-            byte[] _cmdTypeArr = new byte[2], dataBuffer;
-            int dataBufferLength = 0, num = 0;
-            byte _chekCode = 0, chekCode = 0;//下划线 数据包中的校验和 没有下滑线 计算出的校验和
-
-            // 初始话赋值
-            info.flag = true;
-            info.CmdType = CmdType.NoCmd;
-            info.BArr = null;
-            //info.
-            LogUtil.WriteLog($"接收的数据: " + ByteToHexCode(originData, 0, originData.Length));
-
-            //  检查包头包尾 获取数据长度 把数据临时存入 databuffer中
-            for (int i = 0; i < originData.Length; i++)
-            {
-                // 检测包的位置
-                if (originData[i] == 0xA5 && originData[originData.Length - 1] == 0xAA)
-                {
-                    num = i;
-                    // 包的最小长度为18
-                    if (18 < originData.Length)
-                    {
-                        byte hL = originData[num + 1];
-                        byte lL = originData[num + 2];
-                        // 获取数据区域的长度
-                        dataBufferLength = (hL << 8) + lL;
-                        if (dataBufferLength > 12)
-                        {
-                            dataBufferLength = dataBufferLength - 10; // 减去 控制码和水表地址的长度
-                            chekCode = SumCheck(originData, num + 4, originData.Length - 2);// 计算校验和
-                            _chekCode = originData[originData.Length - 2];//校验和
-                            if (true)// (chekCode == _chekCode)
-                            {
-                                //_cmdTypeArr[0] = originData[num + 4];//控制码    
-                                // _cmdTypeArr[1] = originData[num + 5];//辅助控制码
-                                dataBuffer = new byte[dataBufferLength];
-                                //info.CmdType = GetFunc(_cmdTypeArr);// 指令类型
-                                info.Address = ByteToHexCodeLToH(originData, num + 7, num + 13);
-                                //将数据域复制到 dataBufferLength 数组中 表地址后边的数据域  
-                                Array.Copy(originData, num + 14, dataBuffer, 0, dataBufferLength - 10);
-                                info.BArr = dataBuffer;
-                                info.msg = "校验成功";
-                            }
-                            else
-                            {
-
-                                info.flag = false;
-                                info.msg = "校验失败";
-                            }
-                        }
-                        else
-                        {
-                            info.flag = false;
-                            info.msg = "数据帧不完整";
-                        }
-                        break;
-
-
-                    }
-                    else
-                    {
-                        //  Debug.WriteLine("################" + num + 1 + "#############" + num + 2);
-                        info.flag = false;
-                        info.msg = "数据帧不完整";
-                    }
-                }
-            }
-        }
-
         /// <summary>
         /// 电池供电进行内包拆包操作
         /// </summary>
@@ -288,9 +212,8 @@ namespace NB_IOT_TCP_HP_SOCKET.WWKJUtil
             {
                 strVol = "1";
             }
-            string strReturnData = "";
-            CellMonitorConvertByType.ConvertData(data, 9, "0101", "2", out strReturnData);
-            int num = count(strReturnData);
+            CellMonitorConvertByType.ConvertData(data, 9, "0101", "2", out string strReturnData);
+            int num = Count(strReturnData);
             int num2 = 0;
             int num3 = 0;
             int num4 = 0;
@@ -317,8 +240,7 @@ namespace NB_IOT_TCP_HP_SOCKET.WWKJUtil
             for (int i = 0; i < cm._recordCount; i++)
             {
                 CellRecord item = new CellRecord();
-                string str3 = "";
-                CellMonitorConvertByType.ConvertData(data, 20 + ((i * (num + 1)) * 4), "int", "4", out str3);
+                CellMonitorConvertByType.ConvertData(data, 20 + ((i * (num + 1)) * 4), "int", "4", out string str3);
                 item.PickTime = RecordTime(str3);
                 int num7 = 0;
                 int num8 = 0;
@@ -326,16 +248,14 @@ namespace NB_IOT_TCP_HP_SOCKET.WWKJUtil
                 {
                     if ((num5 == 11) && ((ch2 = ch = strReturnData[15 - num5]).ToString() == "1"))
                     {
-                        string str4 = "";
                         num8 = 1;
-                        CellMonitorConvertByType.ConvertData(data, (20 + (((((1 + num3) + num4) + num2) * 4) * i)) + ((num3 + 1) * 4), "0101", "4", out str4);
+                        CellMonitorConvertByType.ConvertData(data, (20 + (((((1 + num3) + num4) + num2) * 4) * i)) + ((num3 + 1) * 4), "0101", "4", out string str4);
                         item.Switch = OrgnizeSwitch(str4, strVol);
                     }
                     else if (strReturnData.Substring((strReturnData.Length - 1) - num5, 1) == "1")
                     {
                         num7++;
-                        string str5 = "";
-                        CellMonitorConvertByType.ConvertData(data, (20 + ((i * (((num3 + num4) + num2) + 1)) * 4)) + ((num7 + num8) * 4), "float", "", out str5);
+                        CellMonitorConvertByType.ConvertData(data, (20 + ((i * (((num3 + num4) + num2) + 1)) * 4)) + ((num7 + num8) * 4), "float", "", out string str5);
                         MonitorValue value2 = new MonitorValue();
                         value2.Name = "A" + num5.ToString();
                         value2.Value = str5;
@@ -450,7 +370,7 @@ namespace NB_IOT_TCP_HP_SOCKET.WWKJUtil
             return true;
         }
 
-        private static int count(string strformat)
+        private static int Count(string strformat)
         {
             int num = 0;
             for (int i = 0; i < strformat.Length; i++)
@@ -833,28 +753,28 @@ namespace NB_IOT_TCP_HP_SOCKET.WWKJUtil
             
             foreach (KeyValuePair<string, CommOption> analogData in _analogData)
             {
-                if (analogData.Value.IsDerivate == "基础量")
+                try
                 {
-                    string subDeviceIndex = analogData.Value.SubDeviceIndex;
-                    int intStartIndex = (Convert.ToInt32(analogData.Value.StartIndex) - _analogStartAddress) * 2;
-                    if ((intStartIndex < 0) || (intStartIndex > (ReturnData.Length - 1)))
-                    {
-                        analogData.Value.Value = "量起始地址设置错误";
-                    }
-                    else
+                    if (analogData.Value.IsDerivate == "基础量")
                     {
-                        try
+                        string subDeviceIndex = analogData.Value.SubDeviceIndex;
+                        int intStartIndex = (Convert.ToInt32(analogData.Value.StartIndex) - _analogStartAddress) * 2;
+                        if ((intStartIndex < 0) || (intStartIndex > (ReturnData.Length - 1)))
                         {
-                            string str2;
-                            msg = ModBusConvertByType.ConvertData(ReturnData, intStartIndex, analogData.Value.ConvertMode, analogData.Value.ConvertParam, out str2);
-                            msg = analogData.Value.ExplainData(str2);
+                            analogData.Value.Value = "量起始地址设置错误";
                         }
-                        catch (Exception exception)
+                        else
                         {
-                            
+                            msg = ModBusConvertByType.ConvertData(ReturnData, intStartIndex, analogData.Value.ConvertMode, analogData.Value.ConvertParam, out string str2);
+                            msg = analogData.Value.ExplainData(str2);
+
                         }
                     }
                 }
+                catch (Exception)
+                {
+                    continue;
+                }
             }
         }
 
@@ -866,23 +786,24 @@ namespace NB_IOT_TCP_HP_SOCKET.WWKJUtil
         /// <param name="_switchStartAddress"></param>
         public static void OrganizeSwitchData(byte[] ReturnData, Dictionary<string, CommOption> _switchData, int _switchStartAddress)
         {
-            int num;
             foreach (KeyValuePair<string, CommOption> switchData in _switchData)
             {
-                if (switchData.Value.IsDerivate == "基础量")
+                try
                 {
-                    string subDeviceIndex = switchData.Value.SubDeviceIndex;
-                    int intStartIndex = (Convert.ToInt32(switchData.Value.StartIndex) - _switchStartAddress) * 2;
-                    try
-                    {
-                        string str2;
-                        ResMsg msg = ModBusConvertByType.ConvertData(ReturnData, intStartIndex, switchData.Value.ConvertMode, switchData.Value.ConvertParam, out str2);
-                        msg = switchData.Value.ExplainData(str2);
-                    }
-                    catch (Exception exception)
+                    if (switchData.Value.IsDerivate == "基础量")
                     {
+                        string subDeviceIndex = switchData.Value.SubDeviceIndex;
+                        int intStartIndex = (Convert.ToInt32(switchData.Value.StartIndex) - _switchStartAddress) * 2;
+                    
+                            ResMsg msg = ModBusConvertByType.ConvertData(ReturnData, intStartIndex, switchData.Value.ConvertMode, switchData.Value.ConvertParam, out string str2);
+                            msg = switchData.Value.ExplainData(str2);
+                    
                     }
                 }
+                catch (Exception)
+                {
+                    continue;
+                }
             }
         }
     }