Browse Source

添加 新功能码 3d

jochu_computer 3 weeks ago
parent
commit
4fc382511a

+ 3 - 1
NB_IOT_TCP_HP_SOCKET/MainForm_TCP.cs

@@ -1167,7 +1167,7 @@ namespace NB_IOT_TCP_HP_SOCKET
                                         paramByte.Clear();
                                     }
                                 }
-                                // 抄表数据
+                                // 抄表数据  // f3d 属于历史数据
                                 else
                                 {
                                     modbus.FunCode = code;
@@ -1208,6 +1208,7 @@ namespace NB_IOT_TCP_HP_SOCKET
                         InsourcingData insourcingData = (InsourcingData)insourcingQueue.Dequeue();
 
                         DataTable dt = insourcingData.DT;
+                        // 电池供电
                         if ("CD_CellMonitor".Equals(insourcingData.Type))
                         {
                             // 参数包
@@ -1561,6 +1562,7 @@ namespace NB_IOT_TCP_HP_SOCKET
                     }
                 }
                 HistoryRecord record = new HistoryRecord(recordtime, list.ToArray());
+                record.Funcode = cm._funCode;
                 if (cm != null)
                 {
                     record.DataSource = "历史记录";

+ 21 - 0
NB_IOT_TCP_HP_SOCKET/WWKJUtil/HistoryRecord.cs

@@ -17,6 +17,23 @@ namespace NB_IOT_TCP_HP_SOCKET.WWKJUtil
 
         private string dataSource;
 
+        // 2025-03-11 添加功能字段 为了尽量不影响以前逻辑  by jochu
+        private byte funcode = 0x2c;
+
+        public byte Funcode {
+
+            get
+            {
+                return this.funcode;
+            }
+
+            set
+            {
+                this.funcode = value;
+            }
+
+        }
+
         public string DataSource
         {
             get
@@ -63,6 +80,10 @@ namespace NB_IOT_TCP_HP_SOCKET.WWKJUtil
             {
                 return this._dataList;
             }
+            //set 
+            //{
+            //    this._dataList = value;
+            //}
         }
 
 

+ 57 - 3
NB_IOT_TCP_HP_SOCKET/WWKJUtil/Modbus.cs

@@ -206,6 +206,7 @@ namespace NB_IOT_TCP_HP_SOCKET.WWKJUtil
                     return new ResMsg(false, string.Concat(new object[] { "打包:地址超范围,功能码:", modbus.FunCode, ",起始地址:", modbus.StartingAddress }));
 
                 case FunctionCode.F2C:
+                case FunctionCode.F3D:
                 case FunctionCode.F2D:
                 case FunctionCode.F2E:
                 case FunctionCode.F2F:
@@ -285,7 +286,15 @@ namespace NB_IOT_TCP_HP_SOCKET.WWKJUtil
                     bytCommand[2] = (byte)(id >> 8);
                     bytCommand[3] = (byte)(id & 0xff);
                     break;
-
+                case FunctionCode.F3D:
+                    bytCommand = new byte[4];
+                    id = modbus.Id;
+                    bytCommand[0] = (byte)(id & 0xff);
+                    bytCommand[1] = (byte)modbus.FunCode;
+                    id = modbus._returnDataLength;
+                    bytCommand[2] = (byte)(id >> 8);
+                    bytCommand[3] = (byte)(id & 0xff);
+                    break;
                 case FunctionCode.F2D:
                     {
                         bytCommand = new byte[14];
@@ -411,6 +420,7 @@ namespace NB_IOT_TCP_HP_SOCKET.WWKJUtil
                     return new ResMsg(false, string.Concat(new object[] { "打包:地址超范围,功能码:", modbus.FunCode, ",起始地址:", modbus.StartingAddress }));
 
                 case FunctionCode.F2C:
+                case FunctionCode.F3D:
                 case FunctionCode.F2D:
                 case FunctionCode.F2E:
                 case FunctionCode.F2F:
@@ -515,7 +525,21 @@ namespace NB_IOT_TCP_HP_SOCKET.WWKJUtil
                     bytCommand[8] = (byte)(id >> 8);
                     bytCommand[9] = (byte)(id & 0xff);
                     break;
-
+                case FunctionCode.F3D:
+                    bytCommand = new byte[10];
+                    bytCommand[0] = 0;
+                    bytCommand[1] = 0;
+                    bytCommand[2] = 0;
+                    bytCommand[3] = 0;
+                    bytCommand[4] = 0;
+                    bytCommand[5] = 4;
+                    id = modbus.Id;
+                    bytCommand[6] = (byte)(id & 0xff);
+                    bytCommand[7] = (byte)modbus.FunCode;
+                    id = modbus._returnDataLength;
+                    bytCommand[8] = (byte)(id >> 8);
+                    bytCommand[9] = (byte)(id & 0xff);
+                    break;
                 case FunctionCode.F2D:
                     {
                         bytCommand = new byte[20];
@@ -828,6 +852,20 @@ namespace NB_IOT_TCP_HP_SOCKET.WWKJUtil
                         goto Label_0813;
                     }
                     return new ResMsg(false, "上报数据长度错误");
+                case FunctionCode.F3D:
+                    num = (package[index] << 8) + package[index + 1];
+                    index += 2;
+                    if ((num + 6) == package.Length)
+                    {
+                        modbus._returnDataLength = (ushort)num;
+                        modbus.UpDataArea = new byte[num];
+                        for (num4 = 0; num4 < modbus.UpDataArea.Length; num4++)
+                        {
+                            modbus.UpDataArea[num4] = package[num4 + index];
+                        }
+                        goto Label_0813;
+                    }
+                    return new ResMsg(false, "上报数据长度错误");
 
                 case FunctionCode.F2E:
                     modbus._returnDataLength = 2;
@@ -1113,6 +1151,20 @@ namespace NB_IOT_TCP_HP_SOCKET.WWKJUtil
                         goto Label_07A3;
                     }
                     return new ResMsg(false, "上报数据长度错误");
+                case FunctionCode.F3D:
+                    num = (package[index] << 8) + package[index + 1];
+                    index += 2;
+                    if ((num + 6) == package.Length)
+                    {
+                        modbus._returnDataLength = (ushort)num;
+                        modbus.UpDataArea = new byte[num];
+                        for (num4 = 0; num4 < modbus.UpDataArea.Length; num4++)
+                        {
+                            modbus.UpDataArea[num4] = package[num4 + index];
+                        }
+                        goto Label_07A3;
+                    }
+                    return new ResMsg(false, "上报数据长度错误");
 
                 case FunctionCode.F2E:
                     modbus._returnDataLength = 2;
@@ -1335,7 +1387,9 @@ namespace NB_IOT_TCP_HP_SOCKET.WWKJUtil
             F3A = 0x3a,
             F3B = 0x3b,
             F80 = 0x80,
-            F90 = 0x90
+            F90 = 0x90,
+            // 2025-03-11 添加超时水表问题 by jochu 
+            F3D = 0x3d
         }
     }
 }

+ 108 - 33
NB_IOT_TCP_HP_SOCKET/WWKJUtil/ProtocolAnalysisTools.cs

@@ -226,51 +226,85 @@ namespace NB_IOT_TCP_HP_SOCKET.WWKJUtil
             int num3 = 0;
             int num4 = 0;
             int num5 = 0;
-            while (num5 < strReturnData.Length)
-            {
-                if ((num5 == 11) && ((ch2 = ch = strReturnData[15 - num5]).ToString() == "1"))
-                {
-                    num2++;
-                }
-                else if (strReturnData.Substring((strReturnData.Length - 1) - num5, 1) == "1")
+            if (cm.FunCode == 0x3d) {
+                for (int i = 0; i < cm._recordCount; i++)
                 {
-                    if (num5 < 11)
+                    CellRecord item = new CellRecord();
+                    CellMonitorConvertByType.ConvertData(data, 20 + ((i * (num + 1)) * 4), "int", "4", out string str3);
+                    item.PickTime = RecordTime(str3);
+                    int num7 = 0;
+                    for (num5 = 0; num5 < strReturnData.Length; num5++)
                     {
-                        num3++;
-                    }
-                    else
-                    {
-                        num4++;
+                        if (strReturnData.Substring((strReturnData.Length - 1) - num5, 1) == "1")
+                        {
+
+                            num7++;
+                            if (num5 == 11) {
+                                CellMonitorConvertByType.ConvertData(data, 20 + ((i * (num + 1)) * 4) + ((num7) * 4), "int", "", out string str5);
+                                MonitorValue value2 = new MonitorValue();
+                                value2.Name = "B" + num5.ToString();
+                                value2.Value = str5;
+                                item.Analog.Add(value2);
+                            } else {
+                                CellMonitorConvertByType.ConvertData(data, 20 + ((i * (num + 1)) * 4) + ((num7) * 4), "float", "", out string str5);
+                                MonitorValue value2 = new MonitorValue();
+                                value2.Name = "B" + num5.ToString();
+                                value2.Value = str5;
+                                item.Analog.Add(value2);
+                            }
+                        }
                     }
+                    cm._record.Add(item);
                 }
-                num5++;
-            }
-            for (int i = 0; i < cm._recordCount; i++)
-            {
-                CellRecord item = new CellRecord();
-                CellMonitorConvertByType.ConvertData(data, 20 + ((i * (num + 1)) * 4), "int", "4", out string str3);
-                item.PickTime = RecordTime(str3);
-                int num7 = 0;
-                int num8 = 0;
-                for (num5 = 0; num5 < strReturnData.Length; num5++)
+            
+            } else { 
+            
+                while (num5 < strReturnData.Length)
                 {
                     if ((num5 == 11) && ((ch2 = ch = strReturnData[15 - num5]).ToString() == "1"))
                     {
-                        num8 = 1;
-                        CellMonitorConvertByType.ConvertData(data, (20 + (((((1 + num3) + num4) + num2) * 4) * i)) + ((num3 + 1) * 4), "0101", "4", out string str4);
-                        item.Switch = OrgnizeSwitch(str4, strVol);
+                        num2++;
                     }
                     else if (strReturnData.Substring((strReturnData.Length - 1) - num5, 1) == "1")
                     {
-                        num7++;
-                        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;
-                        item.Analog.Add(value2);
+                        if (num5 < 11)
+                        {
+                            num3++;
+                        }
+                        else
+                        {
+                            num4++;
+                        }
+                    }
+                    num5++;
+                }
+                for (int i = 0; i < cm._recordCount; i++)
+                {
+                    CellRecord item = new CellRecord();
+                    CellMonitorConvertByType.ConvertData(data, 20 + ((i * (num + 1)) * 4), "int", "4", out string str3);
+                    item.PickTime = RecordTime(str3);
+                    int num7 = 0;
+                    int num8 = 0;
+                    for (num5 = 0; num5 < strReturnData.Length; num5++)
+                    {
+                        if ((num5 == 11) && ((ch2 = ch = strReturnData[15 - num5]).ToString() == "1"))
+                        {
+                            num8 = 1;
+                            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++;
+                            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;
+                            item.Analog.Add(value2);
+                        }
                     }
+                    cm._record.Add(item);
                 }
-                cm._record.Add(item);
             }
             return new ResMsg();
         }
@@ -590,6 +624,47 @@ namespace NB_IOT_TCP_HP_SOCKET.WWKJUtil
                             }
                         }
                     }
+                    else if(record2.Funcode == 0x3d) // 添加历史记录新指令  默认 老指令先发, 新指令再发,否则会丢数据,已约定好,2c 先发 3d后发
+                    {
+                        StringBuilder sb = new StringBuilder();
+                        List<CommData> dataList = record2.DataList;
+                        sb.Append("Update ").Append(tablename).Append(" SET ");
+                        for (int i = 0; i < dataList.Count; i++)
+                        {
+                            CommData commData = dataList[i];
+                            sb.Append(commData.Name).Append("=");
+                            if ((((commData.Value == null) || (commData.Value == "")) || ((commData.Value == "未知") || (commData.Value == "--"))) || (commData.Value == "非数字"))
+                            {
+                                sb.Append("0");
+                            }
+                            else
+                            {
+                                if (commData.Tag == null || "".Equals(commData.Tag) || "BIT".Equals(commData.Tag.ToUpper()))
+                                {
+                                    sb.Append("'").Append(commData.Value).Append("'");
+                                }
+                                else
+                                {
+                                    sb.Append(commData.Value);
+                                }
+                            }
+
+                            if (i < dataList.Count - 1) {
+                                sb.Append(",");
+                            }
+                        }
+                        sb.Append("设备ID=").Append(deviceid).Append(" AND 采集时间 ='").Append(record2.RecordTime.ToString()).Append("'");
+
+                        int rs = DBHelper.Query(sb.ToString());
+
+                        if (rs > 0)
+                        {
+                            // 合并 最后状态
+                            var record = lastData[deviceid];
+                            record.DataList.AddRange(record2.DataList);
+                            lastData[deviceid] = record;
+                        }
+                    }
                 }
                 catch (Exception ex)
                 {