张泳健 7 лет назад
Родитель
Сommit
3d756a5121

+ 1 - 0
.gitignore Просмотреть файл

@@ -1,4 +1,5 @@
1 1
 .vs/*
2
+.idea/*
2 3
 packages/*
3 4
 DotNettyFromTests/obj/*
4 5
 DotNettyFromTests/bin/*

Двоичные данные
DotNettyFrom.accdb Просмотреть файл


+ 6 - 0
DotNettyFrom/DotNettyFrom.csproj Просмотреть файл

@@ -34,6 +34,9 @@
34 34
   <PropertyGroup>
35 35
     <StartupObject />
36 36
   </PropertyGroup>
37
+  <PropertyGroup>
38
+    <ApplicationIcon>DotNettyFrom.ico</ApplicationIcon>
39
+  </PropertyGroup>
37 40
   <ItemGroup>
38 41
     <Reference Include="DotNetty.Buffers, Version=0.4.6.0, Culture=neutral, PublicKeyToken=bc13ca065fa06c29, processorArchitecture=MSIL">
39 42
       <HintPath>..\packages\DotNetty.Buffers.0.4.6\lib\net45\DotNetty.Buffers.dll</HintPath>
@@ -241,5 +244,8 @@
241 244
   <ItemGroup>
242 245
     <Folder Include="cmdparser\" />
243 246
   </ItemGroup>
247
+  <ItemGroup>
248
+    <Content Include="DotNettyFrom.ico" />
249
+  </ItemGroup>
244 250
   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
245 251
 </Project>

Двоичные данные
DotNettyFrom/DotNettyFrom.ico Просмотреть файл


+ 2 - 0
DotNettyFrom/MainForm.Designer.cs Просмотреть файл

@@ -29,6 +29,7 @@
29 29
         private void InitializeComponent()
30 30
         {
31 31
             this.components = new System.ComponentModel.Container();
32
+            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MainForm));
32 33
             this.LBLogShow = new System.Windows.Forms.ListBox();
33 34
             this.ListBoxLogContextMenu = new System.Windows.Forms.ContextMenuStrip(this.components);
34 35
             this.LB_Context_CpCmd = new System.Windows.Forms.ToolStripMenuItem();
@@ -607,6 +608,7 @@
607 608
             this.Controls.Add(this.LBLogShow);
608 609
             this.Controls.Add(this.menuStrip1);
609 610
             this.Font = new System.Drawing.Font("微软雅黑", 8.5F);
611
+            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
610 612
             this.MainMenuStrip = this.menuStrip1;
611 613
             this.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
612 614
             this.Name = "MainForm";

+ 15 - 14
DotNettyFrom/MainForm.cs Просмотреть файл

@@ -51,14 +51,8 @@ namespace DotNettyFrom
51 51
 
52 52
             ChangeDtu();
53 53
 
54
-            if ("".Equals(TbDeviceId.Text.Trim()))
55
-            {
56
-                CmdGenetor.Genetor.DeviceId = "000000000001";
57
-            }
58
-            else
59
-            {
60
-                CmdGenetor.Genetor.DeviceId = TbDeviceId.Text.Trim();
61
-            }
54
+            TbDeviceId.Text = SystemConfig.DefaultNo;
55
+
62 56
 
63 57
             var consumer = Task.Factory.StartNew(() =>
64 58
             {
@@ -153,6 +147,9 @@ namespace DotNettyFrom
153 147
                 this.host = hostAddress.ToString();
154 148
                 this.port = int.Parse(TbPort.Text.Trim());
155 149
 
150
+                //设置地址
151
+                CmdGenetor.Genetor.DeviceId = TbDeviceId.Text.Trim();
152
+
156 153
                 UpdateUiComponent();
157 154
             }
158 155
             catch (SocketException exception)
@@ -176,10 +173,14 @@ namespace DotNettyFrom
176 173
             water2ToolStripMenuItem.Checked = true;
177 174
             CmdGenetor.Genetor = null;
178 175
             CmdGenetor.Genetor = new Water2CmdGenetor();
179
-
180 176
             ConfigChange.ChangeDevice(SystemConfig.Water2);
177
+            TbDeviceId.Text = SystemConfig.DefaultNo;
178
+            TbPort.Text = "" + SystemConfig.DefaultPort;
181 179
         }
182 180
 
181
+        /// <summary>
182
+        /// 记得Reload数据库
183
+        /// </summary>
183 184
         private void ChangeDtu()
184 185
         {
185 186
             this.Text = "模拟器-DTU";
@@ -188,6 +189,8 @@ namespace DotNettyFrom
188 189
             CmdGenetor.Genetor = null;
189 190
             CmdGenetor.Genetor = new DtuCmdGenetor();
190 191
             ConfigChange.ChangeDevice(SystemConfig.Dtu);
192
+            TbDeviceId.Text = SystemConfig.DefaultNo;
193
+            TbPort.Text = "" + SystemConfig.DefaultPort;
191 194
         }
192 195
 
193 196
         #region Component Event
@@ -196,7 +199,6 @@ namespace DotNettyFrom
196 199
         {
197 200
             BeforeConnection();
198 201
 
199
-            CmdGenetor.Genetor.DeviceId = TbDeviceId.Text.Trim();
200 202
             Task startNew = Task.Factory.StartNew(() =>
201 203
             {
202 204
                 _nettyClient = new NettyClient();
@@ -210,7 +212,6 @@ namespace DotNettyFrom
210 212
 
211 213
             Task startNew = Task.Factory.StartNew(() =>
212 214
             {
213
-                CmdGenetor.Genetor.DeviceId = TbDeviceId.Text.Trim();
214 215
                 string loginCmd = CmdGenetor.Genetor.GetLoginCmd(TbCCID.Text.Trim());
215 216
                 _nettyClient = new NettyClient();
216 217
                 _nettyClient.StartNetty(host, port, loginCmd);
@@ -381,7 +382,7 @@ namespace DotNettyFrom
381 382
             if (index >= 0 && index < LBLogShow.Items.Count)
382 383
             {
383 384
                 UIInfoModel uiInfoModel = LBLogShow.SelectedItem as UIInfoModel;
384
-                string line = uiInfoModel.ToString();
385
+                string line = uiInfoModel?.ToString();
385 386
                 if (!line.Trim().Equals(""))
386 387
                     Clipboard.SetDataObject(line);
387 388
             }
@@ -393,7 +394,7 @@ namespace DotNettyFrom
393 394
             if (index >= 0 && index < LBLogShow.Items.Count)
394 395
             {
395 396
                 UIInfoModel uiInfoModel = LBLogShow.SelectedItem as UIInfoModel;
396
-                string line = CmdUtil.FilterCmd(uiInfoModel.ToString());
397
+                string line = CmdUtil.FilterCmd(uiInfoModel?.ToString());
397 398
                 if (!line.Trim().Equals(""))
398 399
                 {
399 400
                 }
@@ -406,7 +407,7 @@ namespace DotNettyFrom
406 407
             if (index >= 0 && index < LBLogShow.Items.Count)
407 408
             {
408 409
                 UIInfoModel uiInfoModel = LBLogShow.SelectedItem as UIInfoModel;
409
-                string line = CmdUtil.FilterCmd(uiInfoModel.ToString());
410
+                string line = CmdUtil.FilterCmd(uiInfoModel?.ToString());
410 411
                 if (!line.Trim().Equals(""))
411 412
                 {
412 413
                 }

Разница между файлами не показана из-за своего большого размера
+ 4512 - 0
DotNettyFrom/MainForm.resx


+ 1 - 1
DotNettyFrom/Properties/AssemblyInfo.cs Просмотреть файл

@@ -8,7 +8,7 @@ using System.Runtime.InteropServices;
8 8
 [assembly: AssemblyTitle("DotNettyFrom")]
9 9
 [assembly: AssemblyDescription("")]
10 10
 [assembly: AssemblyConfiguration("")]
11
-[assembly: AssemblyCompany("")]
11
+[assembly: AssemblyCompany("Vcarecity")]
12 12
 [assembly: AssemblyProduct("DotNettyFrom")]
13 13
 [assembly: AssemblyCopyright("Copyright ©  2017")]
14 14
 [assembly: AssemblyTrademark("")]

+ 4 - 2
DotNettyFrom/common/UIInfoModel..cs Просмотреть файл

@@ -75,7 +75,8 @@ namespace DotNettyFrom.common
75 75
             {
76 76
                 try
77 77
                 {
78
-                    string cmdIdString = cmd.Substring(SystemConfig.FunctionCodeStartPos, SystemConfig.FunctionCodeLength);
78
+                    string cmdIdString = cmd.Substring(SystemConfig.FunctionCodeStartPos * 2,
79
+                        SystemConfig.FunctionCodeLength * 2);
79 80
                     res += ". cmdId=" + cmdIdString;
80 81
                     cmdId = CalcCmdId(cmd);
81 82
                 }
@@ -91,7 +92,8 @@ namespace DotNettyFrom.common
91 92
         {
92 93
             try
93 94
             {
94
-                string cmdId = cmd.Substring(SystemConfig.FunctionCodeStartPos, SystemConfig.FunctionCodeLength);
95
+                string cmdId = cmd.Substring(SystemConfig.FunctionCodeStartPos * 2,
96
+                    SystemConfig.FunctionCodeLength * 2);
95 97
                 return int.Parse(cmdId);
96 98
             }
97 99
             catch (Exception)

+ 13 - 4
DotNettyFrom/config/ConfigChange.cs Просмотреть файл

@@ -3,6 +3,7 @@ using System.Collections.Generic;
3 3
 using System.Linq;
4 4
 using System.Text;
5 5
 using System.Threading.Tasks;
6
+using DotNettyFrom.db;
6 7
 
7 8
 namespace DotNettyFrom.config
8 9
 {
@@ -18,8 +19,6 @@ namespace DotNettyFrom.config
18 19
                 case SystemConfig.Water2:
19 20
                     changeWater2();
20 21
                     break;
21
-                default:
22
-                    break;
23 22
             }
24 23
         }
25 24
 
@@ -39,8 +38,15 @@ namespace DotNettyFrom.config
39 38
 
40 39
             SystemConfig.MeasurePointLen = Water2Config.MeasurePointLen;
41 40
             SystemConfig.MessageFlowLen = Water2Config.MessageFlowLen;
42
-      
43
-    }
41
+
42
+
43
+            SystemConfig.DefaultNo = Water2Config.DefaultWater2No;
44
+            SystemConfig.DefaultPort = Water2Config.DefaulWater2tPort;
45
+            
46
+            
47
+            
48
+            
49
+        }
44 50
 
45 51
         private static void changeDTU()
46 52
         {
@@ -58,6 +64,9 @@ namespace DotNettyFrom.config
58 64
 
59 65
             SystemConfig.MeasurePointLen = DtuConfig.MeasurePointLen;
60 66
             SystemConfig.MessageFlowLen = DtuConfig.MessageFlowLen;
67
+
68
+            SystemConfig.DefaultNo = DtuConfig.DefaultDtuNo;
69
+            SystemConfig.DefaultPort = DtuConfig.DefaulDtutPort;
61 70
         }
62 71
     }
63 72
 }

+ 7 - 2
DotNettyFrom/config/DtuConfig.cs Просмотреть файл

@@ -8,9 +8,14 @@ namespace DotNettyFrom.config
8 8
 {
9 9
     public class DtuConfig
10 10
     {
11
+        public static string DefaultDtuNo = "000000000001";
12
+
13
+        public static int DefaulDtutPort = 15440;
14
+
11 15
         //功能码的位置,长度
12
-        public static int FunctionCodeStartPos = 18;
13
-        public static int FunctionCodeLength = 2;
16
+        public static int FunctionCodeStartPos = 9;
17
+
18
+        public static int FunctionCodeLength = 1;
14 19
 
15 20
 
16 21
         //心跳指令的功能码,用来判定是否是心跳

+ 11 - 4
DotNettyFrom/config/SystemConfig.cs Просмотреть файл

@@ -14,6 +14,10 @@ namespace DotNettyFrom.config
14 14
         //当前是哪一个设备
15 15
         public static int CurrentType;
16 16
 
17
+        public static string DefaultNo = DtuConfig.DefaultDtuNo;
18
+        public static int DefaultPort = DtuConfig.DefaulDtutPort;
19
+
20
+
17 21
         //是否打印心跳指令
18 22
         public static bool IsLogHeartBeatInfo = false;
19 23
 
@@ -21,9 +25,9 @@ namespace DotNettyFrom.config
21 25
         public static int HeartbeatDownCmdCode = 4;
22 26
 
23 27
         //功能码的位置,长度
24
-        public static int FunctionCodeStartPos = 18;
28
+        public static int FunctionCodeStartPos = 9;
25 29
 
26
-        public static int FunctionCodeLength = 2;
30
+        public static int FunctionCodeLength = 1;
27 31
 
28 32
         //定时发送心中
29 33
         public static bool OpenHeartBeat = true;
@@ -37,12 +41,13 @@ namespace DotNettyFrom.config
37 41
 
38 42
         //测量点&消息流水
39 43
         public static int MeasurePointLen = 1;
40
-        public static int MessageFlowLen = 2;
41 44
 
45
+        public static int MessageFlowLen = 2;
42 46
 
43 47
 
44
-        //粘包-半包问题
48
+        //粘包-半包问题,长度
45 49
         public static int LengthFieldOffset = 10;
50
+
46 51
         public static int LengthFieldLength = 2;
47 52
         public static int LengthAdjustment = 1;
48 53
         public static int InitialBytesToStrip = 0;
@@ -54,12 +59,14 @@ namespace DotNettyFrom.config
54 59
 
55 60
         //1 发送的. 0:接收的, -1.Debug.日常信息
56 61
         public static int SendType = 1;
62
+
57 63
         public static int RecType = 0;
58 64
         public static int DebugType = -1;
59 65
         public static int ReplyType = 2;
60 66
 
61 67
         //数据库对应
62 68
         public const int IpHisType = 1;
69
+
63 70
         public const int AddrHisType = 2;
64 71
         public const int SendHisType = 3;
65 72
         public const int CcidHisType = 4;

+ 6 - 2
DotNettyFrom/config/Water2Config.cs Просмотреть файл

@@ -8,9 +8,13 @@ namespace DotNettyFrom.config
8 8
 {
9 9
     public class Water2Config
10 10
     {
11
+        public static string DefaultWater2No = "000000000002";
12
+
13
+        public static int DefaulWater2tPort = 15443;
14
+
11 15
         //功能码的位置,长度
12
-        public static int FunctionCodeStartPos = 18;
13
-        public static int FunctionCodeLength = 2;
16
+        public static int FunctionCodeStartPos = 9;
17
+        public static int FunctionCodeLength = 1;
14 18
 
15 19
         //心跳指令的功能码,用来判定是否是心跳
16 20
         public static int HeartbeatDownCmdCode = 3;

+ 26 - 0
DotNettyFrom/db/DBHelper.cs Просмотреть файл

@@ -237,6 +237,32 @@ namespace DotNettyFrom.db
237 237
             }
238 238
         }
239 239
 
240
+        /// <summary>
241
+        /// 首次数据库加载到内存
242
+        /// </summary>
243
+        /// <param name="sql"></param>
244
+        public void ReloadMoneryCache(string sql = null)
245
+        {
246
+            if (sql == null)
247
+            {
248
+                string table = DBConfig.GetTableName();
249
+                sql = @"select * from " + table;
250
+            }
251
+            using (OleDbCommand sqlcmd = new OleDbCommand(sql, _DbConn))
252
+            {
253
+                using (OleDbDataReader reader = sqlcmd.ExecuteReader())
254
+                {
255
+                    while (reader != null && reader.Read()) //这个read调用很重要!不写的话运行时将提示找不到数据  
256
+                    {
257
+                        var myId = reader.GetString(2);
258
+                        var userValue = CheckUtil.StringIsEmpty(reader.GetString(8));
259
+                        if (!_idInfo.ContainsKey(myId))
260
+                            _idInfo.Add(myId, userValue);
261
+                    }
262
+                }
263
+            }
264
+        }
265
+
240 266
         //Select
241 267
         public DataTable SelectCmds(string sql = null)
242 268
         {

+ 6 - 4
DotNettyFrom/form/ItemManagerForm.cs Просмотреть файл

@@ -7,6 +7,7 @@ using System.Linq;
7 7
 using System.Text;
8 8
 using System.Threading.Tasks;
9 9
 using System.Windows.Forms;
10
+using DotNettyFrom.config;
10 11
 using DotNettyFrom.db;
11 12
 using DotNettyFrom.excel;
12 13
 using DotNettyFrom.model;
@@ -167,20 +168,21 @@ namespace DotNettyFrom.form
167 168
 //                名字 - IDInfo
168 169
 //                描述 - Desp
169 170
                 int mType = CbFindType.SelectedIndex;
171
+                string table = DBConfig.GetTableName();
170 172
                 string sql = "";
171 173
                 switch (mType)
172 174
                 {
173 175
                     case 0:
174
-                        sql = @"select* from fw where my_index = " + query;
176
+                        sql = @"select* from " + table + " where my_index = " + query;
175 177
                         break;
176 178
                     case 1:
177
-                        sql = @"select* from fw where MY_ID = '" + query + "'";
179
+                        sql = @"select* from " + table + " where MY_ID = '" + query + "'";
178 180
                         break;
179 181
                     case 2:
180
-                        sql = @"select* from fw where idInfo like '%" + query + "%'";
182
+                        sql = @"select* from " + table + " where idInfo like '%" + query + "%'";
181 183
                         break;
182 184
                     case 3:
183
-                        sql = @"select* from fw where desp like '%" + query + "%'";
185
+                        sql = @"select* from " + table + " where desp like '%" + query + "%'";
184 186
                         break;
185 187
                     default:
186 188
                         ReLoadDatatable();

+ 3 - 2
DotNettyFrom/generatecmd/CmdGenetor.cs Просмотреть файл

@@ -6,6 +6,7 @@ using System.Threading.Tasks;
6 6
 using DotNetty.Handlers.Timeout;
7 7
 using DotNettyFrom.config;
8 8
 using DotNettyFrom.util;
9
+using NUnit.Framework;
9 10
 
10 11
 namespace DotNettyFrom.generatecmd
11 12
 {
@@ -25,7 +26,8 @@ namespace DotNettyFrom.generatecmd
25 26
         {
26 27
             try
27 28
             {
28
-                string cmdId = receiveCmd.Substring(SystemConfig.FunctionCodeStartPos, SystemConfig.FunctionCodeLength);
29
+                string cmdId = receiveCmd.Substring(2 * SystemConfig.FunctionCodeStartPos,
30
+                    2 * SystemConfig.FunctionCodeLength);
29 31
                 return Utils.hexToTen(cmdId);
30 32
             }
31 33
             catch (Exception)
@@ -33,6 +35,5 @@ namespace DotNettyFrom.generatecmd
33 35
             }
34 36
             return int.MinValue;
35 37
         }
36
-
37 38
     }
38 39
 }

+ 200 - 2
DotNettyFrom/generatecmd/DtuCmdGenetor.cs Просмотреть файл

@@ -1,16 +1,28 @@
1 1
 using System;
2 2
 using System.Collections.Generic;
3 3
 using System.Linq;
4
+using System.Security.AccessControl;
4 5
 using System.Text;
5 6
 using System.Threading.Tasks;
7
+using System.Windows.Forms;
8
+using DotNettyFrom.common;
9
+using DotNettyFrom.config;
10
+using DotNettyFrom.db;
6 11
 using DotNettyFrom.generatecmd;
12
+using DotNettyFrom.model;
7 13
 using DotNettyFrom.util;
8 14
 
9 15
 namespace DotNettyFrom.generatecmd
10 16
 {
11 17
     public class DtuCmdGenetor : CmdGenetor
12 18
     {
13
-        public override string GetLoginCmd( string ccid)
19
+        //ID LEN
20
+        private const int ID_LEN = 2;
21
+
22
+        private const int FUNC_REPLY_READ = 16; //要回复的功能码
23
+        private const int FUNC_REPLY_WRITE = 32; //要回复的功能码
24
+
25
+        public override string GetLoginCmd(string ccid)
14 26
         {
15 27
             string cmd = DeviceId + "820014" + ccid;
16 28
             cmd = "7e7e7e" + CmdUtil.CalcCrcCode(cmd);
@@ -27,7 +39,193 @@ namespace DotNettyFrom.generatecmd
27 39
 
28 40
         public override string GetReplyCmd(string receiveCmd)
29 41
         {
30
-            throw new NotImplementedException();
42
+            int cmdId = GetCmdId(receiveCmd);
43
+            Console.WriteLine("收到功能码=" + cmdId + " 16进制=" + Utils.tenToHex(cmdId));
44
+
45
+
46
+            //跳过心跳包
47
+            if (cmdId == SystemConfig.HeartbeatDownCmdCode)
48
+            {
49
+                if (SystemConfig.IsLogHeartBeatInfo)
50
+                {
51
+                    DataModel.ReceCollection.Add(new UIInfoModel("收到心跳", receiveCmd));
52
+                }
53
+                return null;
54
+            }
55
+
56
+            //处理其他的主动上报的回复
57
+
58
+
59
+            //处理其他的主动上报的回复
60
+
61
+
62
+            if (cmdId == int.MinValue)
63
+            {
64
+                DataModel.ReceCollection.Add(new UIInfoModel("帧错误", receiveCmd)
65
+                    .setAction(-1)
66
+                    .setSendType(SystemConfig.ReplyType));
67
+                return null;
68
+            }
69
+
70
+            int position = SystemConfig.FunctionCodeStartPos * 2;
71
+            //前面相同的, 到功能码前面
72
+            string resp = receiveCmd.Substring(0, position);
73
+            Console.WriteLine("前面回复相同的=" + resp);
74
+
75
+            position += SystemConfig.FunctionCodeLength * 2;
76
+
77
+            string frameDataLen = receiveCmd.Substring(position, SystemConfig.LengthFieldLength * 2);
78
+
79
+            //帧中定义的数据长度.10进制的
80
+            int frameDefineLen = Utils.hexToTen(frameDataLen);
81
+
82
+            Console.WriteLine("数据长度= " + frameDataLen + "  -- 10进制=" + Utils.hexToTen(frameDataLen));
83
+
84
+            position += SystemConfig.LengthFieldLength * 2;
85
+
86
+            //测量点&消息流水-所占长度
87
+            int msgFlowLength = SystemConfig.MeasurePointLen + SystemConfig.MessageFlowLen;
88
+
89
+            //测量点&消息流水
90
+            string msgFlow = receiveCmd.Substring(position, msgFlowLength * 2);
91
+            position += msgFlowLength * 2;
92
+            Console.WriteLine("测量点+消息流水=" + msgFlow);
93
+
94
+            string codeIdHex = receiveCmd.Substring(position, ID_LEN * 2);
95
+            int codeId = Utils.hexToTen(codeIdHex);
96
+            //0301 这些写死,2位
97
+            position += ID_LEN * 2;
98
+            Console.WriteLine("功能码=" + codeIdHex + "; 10进制=" + Utils.hexToTen(codeIdHex));
99
+
100
+
101
+            if (FUNC_REPLY_READ == cmdId)
102
+            {
103
+                DataModel.ReceCollection.Add(new UIInfoModel("收到读取", receiveCmd));
104
+
105
+                //读,回复
106
+                WvBean wb = DbHelper.GetInstance().SelectOneCmdByIndex(codeId);
107
+
108
+                if (wb == null)
109
+                {
110
+                    DataModel.ReceCollection.Add(
111
+                        new UIInfoModel(
112
+                                "读取-字典中不存在这个功能码=" + codeId + " 16进制=" + codeIdHex,
113
+                                receiveCmd)
114
+                            .setAction(-1)
115
+                            .setSendType(SystemConfig.ReplyType));
116
+                    return null;
117
+                }
118
+
119
+                string ud = wb.userValue;
120
+                Console.WriteLine(ud);
121
+                string lenMsgFlowData = ReadMessage(wb, msgFlow, codeIdHex);
122
+
123
+                int replyCmdId = cmdId + 128;
124
+                resp += CmdUtil.FrontWithZero(Utils.tenToHex(replyCmdId), SystemConfig.FunctionCodeLength * 2);
125
+                resp += lenMsgFlowData;
126
+                resp += CmdUtil.CalcCrcCodeSkip(resp, 3);
127
+
128
+                DataModel.ReceCollection.Add(
129
+                    new UIInfoModel("读取成功,回复", resp)
130
+                        .setAction(-1)
131
+                        .setSendType(SystemConfig.ReplyType));
132
+
133
+                return resp;
134
+            }
135
+            else if (FUNC_REPLY_WRITE == cmdId)
136
+            {
137
+                DataModel.ReceCollection.Add(new UIInfoModel("收到设置", receiveCmd));
138
+
139
+                //写
140
+                WvBean wb = DbHelper.GetInstance().SelectOneCmdByIndex(codeId);
141
+
142
+                if (wb == null)
143
+                {
144
+                    DataModel.ReceCollection.Add(
145
+                        new UIInfoModel(
146
+                                "设置-字典中不存在这个功能码=" + codeId + " 16进制=" + codeIdHex,
147
+                                receiveCmd)
148
+                            .setAction(-1)
149
+                            .setSendType(SystemConfig.ReplyType));
150
+                    return null;
151
+                }
152
+
153
+                //取出设置数据
154
+                Console.WriteLine("长度   " + (frameDefineLen - msgFlowLength - ID_LEN));
155
+                string setData = receiveCmd.Substring(position, 2 * (frameDefineLen - msgFlowLength - ID_LEN));
156
+
157
+                wb.userValue = setData;
158
+
159
+                string resultData = "";
160
+                string tip = "";
161
+                int result = DbHelper.GetInstance().UpdateCmd(wb);
162
+                if (result > 0)
163
+                {
164
+                    resultData = "00";
165
+                    tip = "设置成功,回复";
166
+                }
167
+                else
168
+                {
169
+                    resultData = "01";
170
+                    tip = "设置失败,回复";
171
+                }
172
+
173
+
174
+//                7E7E7E000000000001a000a1
175
+
176
+                int replyCmdId = cmdId + 128;
177
+                resp += CmdUtil.FrontWithZero(Utils.tenToHex(replyCmdId), SystemConfig.FunctionCodeLength * 2); //功能码
178
+
179
+                string replyData = msgFlow + codeIdHex + resultData;
180
+                if (replyData.Length % 2 != 0)
181
+                {
182
+                    replyData += "0";
183
+                }
184
+                //长度
185
+                string lenHex =
186
+                    CmdUtil.FrontWithZero(Utils.tenToHex(replyData.Length / 2), SystemConfig.LengthFieldLength * 2);
187
+                resp += lenHex;
188
+                resp += replyData; //消息体
189
+                resp += CmdUtil.CalcCrcCodeSkip(resp, 3);
190
+                DataModel.ReceCollection.Add(
191
+                    new UIInfoModel(tip + " set(" + codeIdHex + ")=value(" + setData + ")", resp)
192
+                        .setAction(-1)
193
+                        .setSendType(SystemConfig.ReplyType));
194
+
195
+                return resp;
196
+            }
197
+
198
+
199
+//            LengthAdjustment
200
+//            string codeData = receiveCmd.Substring(position, receiveCmd.Length - SystemConfig.LengthAdjustment * 2);
201
+//            Console.WriteLine("收到数量=" + codeData);
202
+
203
+            //处理主动上报的回复&心跳.那些不用回复
204
+
205
+            //处理主动上报的回复&心跳.那些不用回复
206
+
207
+
208
+//            WvBean wb = DbHelper.GetInstance().SelectOneCmdById(cmdId);
209
+//            Console.WriteLine(wb);
210
+
211
+            DataModel.ReceCollection.Add(new UIInfoModel("收到", receiveCmd));
212
+
213
+
214
+            return null;
215
+        }
216
+
217
+        private string ReadMessage(WvBean wb, string msgFlow, string codeIdHex)
218
+        {
219
+            string content = msgFlow + codeIdHex + wb.userValue.Trim();
220
+            if (content.Length % 2 != 0)
221
+            {
222
+                content = "0" + content;
223
+            }
224
+
225
+            string lenHex =
226
+                CmdUtil.FrontWithZero(Utils.tenToHex(content.Length / 2), SystemConfig.LengthFieldLength * 2);
227
+
228
+            return lenHex + content;
31 229
         }
32 230
     }
33 231
 }

+ 16 - 2
DotNettyFrom/generatecmd/Water2CmdGenetor.cs Просмотреть файл

@@ -12,6 +12,9 @@ using NUnit.Framework;
12 12
 
13 13
 namespace DotNettyFrom.generatecmd
14 14
 {
15
+    /// <summary>
16
+    /// 不用缓存了,直接直接Select(查询)数据库
17
+    /// </summary>
15 18
     public class Water2CmdGenetor : CmdGenetor
16 19
     {
17 20
         public override string GetLoginCmd(string ccid)
@@ -30,13 +33,24 @@ namespace DotNettyFrom.generatecmd
30 33
         {
31 34
             //功能码
32 35
             int cmdId = GetCmdId(receiveCmd);
33
-
34 36
             Console.WriteLine("功能码=" + cmdId + "; 16=" + Utils.tenToHex(cmdId));
35 37
 
38
+
39
+            if (SystemConfig.HeartbeatDownCmdCode == cmdId)
40
+            {
41
+                if (SystemConfig.IsLogHeartBeatInfo)
42
+                {
43
+                    DataModel.ReceCollection.Add(new UIInfoModel("收到心跳", receiveCmd));
44
+                }
45
+                return null;
46
+            }
47
+
36 48
             //处理主动上报的回复&心跳.那些不用回复
37 49
 
38 50
             //处理主动上报的回复&心跳.那些不用回复
39 51
 
52
+            DataModel.ReceCollection.Add(new UIInfoModel("收到", receiveCmd));
53
+
40 54
 
41 55
             if (cmdId == int.MinValue)
42 56
             {
@@ -59,7 +73,7 @@ namespace DotNettyFrom.generatecmd
59 73
                 return null;
60 74
             }
61 75
 
62
-            int pos = SystemConfig.FunctionCodeStartPos + SystemConfig.FunctionCodeLength;
76
+            int pos = 2 * (SystemConfig.FunctionCodeStartPos + SystemConfig.FunctionCodeLength);
63 77
 
64 78
 
65 79
             //前面的,回复用的

+ 3 - 10
DotNettyFrom/netty/NettyClient.cs Просмотреть файл

@@ -124,7 +124,7 @@ namespace DotNettyFrom.netty
124 124
                 if (message is IByteBuffer buffer)
125 125
                 {
126 126
                     string recCmd = AryUtil.ByteBuffer2Hex(buffer);
127
-                    DataModel.ReceCollection.Add(new UIInfoModel("收到", recCmd));
127
+
128 128
                     Console.WriteLine("收到:" + recCmd);
129 129
 
130 130
                     string cmd = CmdGenetor.Genetor?.GetReplyCmd(recCmd);
@@ -133,10 +133,6 @@ namespace DotNettyFrom.netty
133 133
                         IByteBuffer replyBuffer = AryUtil.Hex2IByteBuffer(cmd);
134 134
                         context.WriteAndFlushAsync(replyBuffer);
135 135
                     }
136
-                    else
137
-                    {
138
-                        Console.WriteLine("reply is null");
139
-                    }
140 136
                 }
141 137
                 else
142 138
                 {
@@ -159,15 +155,12 @@ namespace DotNettyFrom.netty
159 155
                     {
160 156
                         string hb = CmdGenetor.Genetor.GetHeartbeat();
161 157
                         context.Channel.WriteAndFlushAsync(AryUtil.Hex2IByteBuffer(hb));
162
-                        Console.WriteLine("发心跳:" + hb + ".log=" + SystemConfig.IsLogHeartBeatInfo);
158
+                        Console.WriteLine("发心跳:" + hb + ".log=" + SystemConfig.IsLogHeartBeatInfo);
163 159
                         if (SystemConfig.IsLogHeartBeatInfo)
164 160
                         {
165
-                            DataModel.ReceCollection.Add(new UIInfoModel("发送", hb).setSendType(SystemConfig.SendType));
161
+                            DataModel.ReceCollection.Add(new UIInfoModel("发送心跳", hb).setSendType(SystemConfig.SendType));
166 162
                         }
167 163
                     }
168
-                    else
169
-                    {
170
-                    }
171 164
                 }
172 165
             }
173 166
 

+ 19 - 1
DotNettyFrom/util/CmdUtil.cs Просмотреть файл

@@ -72,7 +72,7 @@ namespace DotNettyFrom.util
72 72
             }
73 73
 
74 74
             int result = 0;
75
-            for (int i = 0; i < list.Count(); i++)
75
+            for (int i = 0; i < list.Count; i++)
76 76
             {
77 77
                 result ^= Hex2Dec(list[i]);
78 78
             }
@@ -96,6 +96,24 @@ namespace DotNettyFrom.util
96 96
             return str + FrontWithZero(Dec2Hex(result), 2);
97 97
         }
98 98
 
99
+
100
+        public static string CalcCrcCodeSkip(string str, int skip)
101
+        {
102
+            List<string> list = new List<string>();
103
+            for (int i = 0; i < str.Length; i += 2)
104
+            {
105
+                list.Add(str.Substring(i, 2));
106
+            }
107
+
108
+            int result = 0;
109
+            for (int i = skip; i < list.Count; i++)
110
+            {
111
+                result ^= Hex2Dec(list[i]);
112
+            }
113
+            return FrontWithZero(Dec2Hex(result), 2);
114
+        }
115
+
116
+
99 117
         /// <summary>
100 118
         /// 只生成CRC16的校验码
101 119
         /// </summary>

+ 1 - 0
DotNettyFromTests/DotNettyFromTests.csproj Просмотреть файл

@@ -61,6 +61,7 @@
61 61
   <ItemGroup>
62 62
     <Compile Include="common\UIInfoModelTests.cs" />
63 63
     <Compile Include="generatecmd\CmdGenetorTests.cs" />
64
+    <Compile Include="generatecmd\DtuCmdGenetorTests.cs" />
64 65
     <Compile Include="generatecmd\Water2CmdGenetorTests.cs" />
65 66
     <Compile Include="Properties\AssemblyInfo.cs" />
66 67
     <Compile Include="UnitTest1.cs" />

+ 28 - 0
DotNettyFromTests/generatecmd/DtuCmdGenetorTests.cs Просмотреть файл

@@ -0,0 +1,28 @@
1
+using Microsoft.VisualStudio.TestTools.UnitTesting;
2
+using DotNettyFrom.generatecmd;
3
+using System;
4
+using System.Collections.Generic;
5
+using System.Linq;
6
+using System.Text;
7
+using System.Threading.Tasks;
8
+using DotNettyFrom.db;
9
+using DotNettyFrom.config;
10
+
11
+namespace DotNettyFrom.generatecmd.Tests
12
+{
13
+    [TestClass()]
14
+    public class DtuCmdGenetorTests
15
+    {
16
+        [TestMethod()]
17
+        public void GetReplyCmdTest()
18
+        {
19
+
20
+            DbHelper.Init(@"D:\CSharp\DotNettyFrom\DotNettyFrom\bin\Debug\DotNettyFrom.accdb");
21
+            ConfigChange.ChangeDevice(SystemConfig.Dtu);
22
+
23
+            DtuCmdGenetor dtuCmd = new DtuCmdGenetor();
24
+            //dtuCmd.GetReplyCmd("7e7e7e000000000001100005010101031006");
25
+            dtuCmd.GetReplyCmd("7e7e7e00000000000120000801010103100101f4cf");
26
+        }
27
+    }
28
+}

Двоичные данные
art/DotNettyFrom.ico Просмотреть файл


Двоичные данные
art/DotNettyFrom.png Просмотреть файл