|
@@ -37,8 +37,8 @@ namespace GWSocketClient
|
37
|
37
|
|
38
|
38
|
private void Form1_Load(object sender, EventArgs e)
|
39
|
39
|
{
|
40
|
|
- XmlLoader xmlLoader = new XmlLoader();
|
41
|
|
- vcarecityProtocolBean = xmlLoader.loadXml();
|
|
40
|
+// XmlLoader xmlLoader = new XmlLoader();
|
|
41
|
+// vcarecityProtocolBean = xmlLoader.loadXml();
|
42
|
42
|
if (vcarecityProtocolBean != null)
|
43
|
43
|
{
|
44
|
44
|
this.Text = vcarecityProtocolBean.ProtocolName + " - " + vcarecityProtocolBean.ProtocolNo + " - " +
|
|
@@ -114,22 +114,23 @@ namespace GWSocketClient
|
114
|
114
|
//处理回复信息
|
115
|
115
|
private void handleResponse(SocketHelper.Sockets sks, string reqData)
|
116
|
116
|
{
|
117
|
|
- string resp = tfTmpResponse.Text.Trim();
|
|
117
|
+ //取出原数据功能码
|
|
118
|
+ string functionWord = reqData.Substring(18, 2);
|
|
119
|
+ int funcode = Utils.hexToTen(functionWord);
|
118
|
120
|
|
119
|
|
- if (resp.Equals(""))
|
|
121
|
+ if (!(funcode == 16 || funcode == 32))
|
120
|
122
|
{
|
121
|
|
- resp = "00";
|
|
123
|
+ Console.WriteLine(funcode + " 这个功能码不用回复!");
|
|
124
|
+ return;
|
122
|
125
|
}
|
123
|
126
|
|
124
|
|
- string functionWord = reqData.Substring(18, 2);
|
125
|
|
- int funcode = Utils.hexToTen(functionWord);
|
126
|
127
|
|
127
|
128
|
//16 表示要读数据,返回数据库中的数据
|
128
|
129
|
//32 表示返回结果 成功:00 失败:01
|
129
|
130
|
|
130
|
|
-
|
131
|
131
|
//数据长度
|
132
|
|
- int dataLen = Utils.hexToTen(reqData.Substring(20, 4));
|
|
132
|
+ int dataLen = Utils.hexToTen(reqData.Substring(20, 4)) * 2;
|
|
133
|
+
|
133
|
134
|
string readlData = "";
|
134
|
135
|
try
|
135
|
136
|
{
|
|
@@ -138,48 +139,72 @@ namespace GWSocketClient
|
138
|
139
|
catch (Exception e)
|
139
|
140
|
{
|
140
|
141
|
Console.WriteLine(e.Message);
|
141
|
|
- addToListboxItem("数据长度与帧长度对不上!!");
|
|
142
|
+ addToListboxItem("数据长度与帧长度对不上!! 帧数据长度=" + (dataLen / 2) + " ,Error=" + e.Message);
|
142
|
143
|
return;
|
143
|
144
|
}
|
144
|
145
|
|
|
146
|
+ //消息流水&测量点
|
|
147
|
+ string flowPoint = readlData.Substring(0, 6);
|
|
148
|
+ //ID
|
145
|
149
|
string myId = readlData.Substring(6, 4);
|
|
150
|
+ //对应信息
|
146
|
151
|
string idInfo = readlData.Substring(10);
|
147
|
152
|
|
|
153
|
+ //回复功能码
|
|
154
|
+ int responceFunCode = funcode + 128;
|
|
155
|
+
|
|
156
|
+ string cmd = reqData.Substring(0, 18); //前面的
|
|
157
|
+
|
|
158
|
+ cmd += Utils.stringWith0(Utils.tenToHex(responceFunCode), 2); //回复功能码
|
|
159
|
+ string body = flowPoint + myId;
|
|
160
|
+
|
148
|
161
|
if (funcode == 32)
|
149
|
162
|
{
|
150
|
163
|
//设置数据
|
151
|
164
|
int res = AccsessDbLoader.getInstance().setReceiveData(myId, idInfo);
|
|
165
|
+ //成功回复00 失败回复01
|
152
|
166
|
if (res > 0)
|
153
|
167
|
{
|
154
|
|
- //00
|
|
168
|
+ body += "00";
|
|
169
|
+ AccsessDbLoader.getInstance().reloadAccessDb();
|
155
|
170
|
}
|
156
|
171
|
else
|
157
|
172
|
{
|
158
|
|
- //01
|
|
173
|
+ body += "01";
|
159
|
174
|
}
|
160
|
175
|
}
|
161
|
|
- else if (funcode == 16)
|
|
176
|
+ else
|
162
|
177
|
{
|
163
|
|
- //返回数据库的数据
|
164
|
|
- }
|
165
|
|
-
|
|
178
|
+ //返回数据库的数据 OR 临时数据,如果临时数据有数据的话
|
|
179
|
+ try
|
|
180
|
+ {
|
|
181
|
+ string responce = AccsessDbLoader.getInstance().getUserIdInfo()[myId];
|
166
|
182
|
|
167
|
|
- if (funcode == 16 || funcode == 32)
|
168
|
|
- {
|
169
|
|
- funcode = funcode + 128;
|
170
|
|
- functionWord = Utils.stringWith0(Utils.tenToHex(funcode), 2);
|
171
|
|
- string cmd = reqData.Substring(0, 18) + functionWord;
|
172
|
|
- string data = reqData.Substring(24, 10) + resp;
|
173
|
|
- cmd = cmd + Utils.stringWith0(Utils.tenToHex(data.Length / 2), 4) + data;
|
174
|
|
- cmd = Utils.calcCrcCode(cmd);
|
|
183
|
+ if (!tfTmpResponse.Text.Trim().Equals(""))
|
|
184
|
+ {
|
|
185
|
+ string inputRespocse = tfTmpResponse.Text.Trim();
|
|
186
|
+ responce = Utils.stringWith0(inputRespocse, responce.Length);
|
|
187
|
+ }
|
|
188
|
+ body += responce;
|
|
189
|
+ }
|
|
190
|
+ catch (Exception e)
|
|
191
|
+ {
|
|
192
|
+ Console.WriteLine(e);
|
|
193
|
+ addToListboxItem("异常,没有这个ID=(" + myId + "),ERROR=" + e.Message);
|
|
194
|
+ return;
|
|
195
|
+ }
|
|
196
|
+ }
|
|
197
|
+ cmd += Utils.stringWith0(Utils.tenToHex(body.Length / 2), 4); //长度
|
|
198
|
+ cmd += body; //消息体
|
|
199
|
+ cmd = Utils.calcCrcCode(cmd); //计算检验码
|
175
|
200
|
|
176
|
|
- addToListboxItem(string.Format("回复服务端{0}回应消息:[{1}]", sks.Ip, cmd));
|
|
201
|
+ addToListboxItem(string.Format("回复服务端{0}回应消息:[{1}]", sks.Ip, cmd));
|
177
|
202
|
|
178
|
|
- byte[] ts = HexUtil.hexToBytes(cmd);
|
179
|
|
- sockeTcpClients.SendData(ts);
|
180
|
|
- }
|
|
203
|
+ byte[] ts = HexUtil.hexToBytes(cmd);
|
|
204
|
+ sockeTcpClients.SendData(ts);
|
181
|
205
|
}
|
182
|
206
|
|
|
207
|
+
|
183
|
208
|
private void tbEquipmentAddress_TextChanged(object sender, EventArgs e)
|
184
|
209
|
{
|
185
|
210
|
string equipmentAddress = tbEquipmentAddress.Text.Trim();
|
|
@@ -191,8 +216,8 @@ namespace GWSocketClient
|
191
|
216
|
{
|
192
|
217
|
try
|
193
|
218
|
{
|
194
|
|
- if (!showTopForNoLoadXML())
|
195
|
|
- return;
|
|
219
|
+ /*if (!showTopForNoLoadXML())
|
|
220
|
+ return;*/
|
196
|
221
|
|
197
|
222
|
ip = tbIpaddress.Text.Trim();
|
198
|
223
|
port = tbPort.Text.Trim();
|
|
@@ -414,7 +439,14 @@ namespace GWSocketClient
|
414
|
439
|
|
415
|
440
|
private void btnTextCmd_Click(object sender, EventArgs e)
|
416
|
441
|
{
|
417
|
|
- try
|
|
442
|
+ string code = Utils.calcCrcCode("7e7e7e123456789abc20000701010107030001");
|
|
443
|
+// code = Utils.calcCrcCode("7e7e7e123456789abc10000501010107023f");
|
|
444
|
+ Console.WriteLine(code);
|
|
445
|
+
|
|
446
|
+ handleResponse(null, code);
|
|
447
|
+
|
|
448
|
+
|
|
449
|
+ /*try
|
418
|
450
|
{
|
419
|
451
|
Dictionary<string, string> abc = AccsessDbLoader.getInstance().getUserIdInfo();
|
420
|
452
|
MessageBox.Show(abc.Count + "");
|
|
@@ -424,7 +456,7 @@ namespace GWSocketClient
|
424
|
456
|
catch (Exception exception)
|
425
|
457
|
{
|
426
|
458
|
Console.WriteLine(exception);
|
427
|
|
- }
|
|
459
|
+ }*/
|
428
|
460
|
}
|
429
|
461
|
}
|
430
|
462
|
}
|