|
|
@@ -9,13 +9,20 @@
|
|
9
|
9
|
|
|
10
|
10
|
#include "ProtocolImpl.h"
|
|
11
|
11
|
|
|
|
12
|
+
|
|
12
|
13
|
#include <cmath>
|
|
13
|
14
|
#include <cstdlib>
|
|
14
|
15
|
#include <vector>
|
|
15
|
16
|
#include <sstream>
|
|
16
|
17
|
#include <stdexcept>
|
|
17
|
18
|
#include <iostream>
|
|
18
|
|
-
|
|
|
19
|
+#include <unistd.h>
|
|
|
20
|
+#include <sys/types.h>
|
|
|
21
|
+#include <sys/stat.h>
|
|
|
22
|
+#include <fcntl.h>
|
|
|
23
|
+#include <termios.h>
|
|
|
24
|
+#include <errno.h>
|
|
|
25
|
+#include <string.h>
|
|
19
|
26
|
|
|
20
|
27
|
#include "../../common/GUID.cpp"
|
|
21
|
28
|
#include "../../servicemodel/Base.h"
|
|
|
@@ -23,16 +30,17 @@
|
|
23
|
30
|
#include "../../servicemodel/Device.h"
|
|
24
|
31
|
#include "../../servicemodel/Item.h"
|
|
25
|
32
|
#include "../../servicemodel/Packet.h"
|
|
26
|
|
-
|
|
27
|
33
|
using namespace std;
|
|
28
|
34
|
bool ProtocolImpl::hasInit = false;
|
|
29
|
35
|
|
|
|
36
|
+
|
|
30
|
37
|
uint8_t ProtocolImpl::retBigEndian(uint32_t value, int offset)
|
|
31
|
38
|
{
|
|
32
|
39
|
uint8_t ret = 0;
|
|
33
|
40
|
ret = (value >> offset) & 0xFF;
|
|
34
|
41
|
return ret;
|
|
35
|
42
|
}
|
|
|
43
|
+
|
|
36
|
44
|
int ProtocolImpl::getRegAddrMax(Packet *ppacket, Item *pitem)
|
|
37
|
45
|
{
|
|
38
|
46
|
std::string sParaRegAddr;
|
|
|
@@ -50,7 +58,6 @@ int ProtocolImpl::getRegAddrMax(Packet *ppacket, Item *pitem)
|
|
50
|
58
|
}
|
|
51
|
59
|
else
|
|
52
|
60
|
{
|
|
53
|
|
-
|
|
54
|
61
|
}
|
|
55
|
62
|
}
|
|
56
|
63
|
log_i("最大的起始地址:0x%x", maxRegAddr);
|
|
|
@@ -82,6 +89,7 @@ int ProtocolImpl::getRegAddrMin(Packet *ppacket, Item *pitem)
|
|
82
|
89
|
|
|
83
|
90
|
return iMinRegAddr;
|
|
84
|
91
|
}
|
|
|
92
|
+
|
|
85
|
93
|
int ProtocolImpl::retStateResponse(Packet *ppacket, Item *pitem, const int len)
|
|
86
|
94
|
{
|
|
87
|
95
|
int maxRegAddr = -1;
|
|
|
@@ -150,7 +158,7 @@ std::map<int, int> ProtocolImpl::handleDataLess(PBYTE pbuf,int len,int regNum,in
|
|
150
|
158
|
std::map<int, int> dataSet;
|
|
151
|
159
|
iPbufId = getRealFrame(pbuf, 0);
|
|
152
|
160
|
|
|
153
|
|
-
|
|
|
161
|
+ log_i("handleDataLess");
|
|
154
|
162
|
for (int i = 0; i < regNum; i++)
|
|
155
|
163
|
{
|
|
156
|
164
|
uint16_t regValue = 0;
|
|
|
@@ -192,8 +200,6 @@ std::map<int,int> ProtocolImpl::handleDataGreater(PBYTE pbuf,int len,int regNum,
|
|
192
|
200
|
count += 12)
|
|
193
|
201
|
{
|
|
194
|
202
|
iPbufId = getRealFrame(pbuf, count);
|
|
195
|
|
-
|
|
196
|
|
-
|
|
197
|
203
|
for (int i = 0; i < 4 && regNum > 0; i++,regNum --) // i代表第i个寄存器
|
|
198
|
204
|
{
|
|
199
|
205
|
|
|
|
@@ -222,6 +228,34 @@ std::map<int,int> ProtocolImpl::handleDataGreater(PBYTE pbuf,int len,int regNum,
|
|
222
|
228
|
|
|
223
|
229
|
}
|
|
224
|
230
|
|
|
|
231
|
+//返回IO数据集
|
|
|
232
|
+std::map<int,int> ProtocolImpl::retIoDataSet(Packet *ppacket, Item *pitem,PBYTE pbuf,int len)
|
|
|
233
|
+{
|
|
|
234
|
+ std::map<int, int> dataSet;
|
|
|
235
|
+
|
|
|
236
|
+ dataSet = handleIoData(pbuf);
|
|
|
237
|
+ return dataSet;
|
|
|
238
|
+
|
|
|
239
|
+}
|
|
|
240
|
+
|
|
|
241
|
+std::map<int,int> ProtocolImpl::handleIoData(PBYTE pbuf)
|
|
|
242
|
+{
|
|
|
243
|
+ int iPbufId;
|
|
|
244
|
+ std::map<int, int> dataSet;
|
|
|
245
|
+ iPbufId = getRealFrame(pbuf, 0);
|
|
|
246
|
+ uint16_t regValue = 0;
|
|
|
247
|
+ regValue = pbuf[7];
|
|
|
248
|
+ dataSet[iPbufId] = regValue;
|
|
|
249
|
+
|
|
|
250
|
+ log_i("handleIoData");
|
|
|
251
|
+
|
|
|
252
|
+ for (const auto &entry : dataSet)
|
|
|
253
|
+ {
|
|
|
254
|
+ log_i("addrFrame = 0x%x, value = 0x%x", entry.first, entry.second);
|
|
|
255
|
+ }
|
|
|
256
|
+
|
|
|
257
|
+ return dataSet;
|
|
|
258
|
+}
|
|
225
|
259
|
std::map<int,int>ProtocolImpl::retDataSet(Packet *ppacket, Item *pitem,PBYTE pbuf,int len)
|
|
226
|
260
|
{
|
|
227
|
261
|
std::map<int, int> dataSet; // 存储寄存器地址和值
|
|
|
@@ -229,8 +263,10 @@ std::map<int,int>ProtocolImpl::retDataSet(Packet *ppacket, Item *pitem,PBYTE pbu
|
|
229
|
263
|
int datasLen = 0;
|
|
230
|
264
|
maxRegAddr = getRegAddrMax(ppacket, pitem);
|
|
231
|
265
|
minRegAddr = getRegAddrMin(ppacket, pitem);
|
|
|
266
|
+
|
|
232
|
267
|
regNum = maxRegAddr - minRegAddr + 1;
|
|
233
|
268
|
datasLen = getRealValidLen(regNum);
|
|
|
269
|
+ log_i("regNum:%d,dataLen:%d",regNum,datasLen);
|
|
234
|
270
|
if (datasLen <= 12)
|
|
235
|
271
|
{
|
|
236
|
272
|
dataSet = handleDataLess(pbuf,len,regNum,datasLen);
|
|
|
@@ -240,12 +276,10 @@ std::map<int,int>ProtocolImpl::retDataSet(Packet *ppacket, Item *pitem,PBYTE pbu
|
|
240
|
276
|
dataSet = handleDataGreater(pbuf,len,regNum,datasLen);
|
|
241
|
277
|
}
|
|
242
|
278
|
return dataSet;
|
|
243
|
|
-
|
|
244
|
|
-
|
|
245
|
279
|
}
|
|
|
280
|
+
|
|
246
|
281
|
int ProtocolImpl::iGetItemParaConfig(Item *pitem,std::string attribute)
|
|
247
|
282
|
{
|
|
248
|
|
-
|
|
249
|
283
|
Item *t_item = pitem;
|
|
250
|
284
|
Base &t_base = t_item->getBase();
|
|
251
|
285
|
string sAttribute = "";
|
|
|
@@ -256,10 +290,7 @@ int ProtocolImpl::iGetItemParaConfig(Item *pitem,std::string attribute)
|
|
256
|
290
|
|
|
257
|
291
|
iAttribute = strtol(sAttribute.c_str(), NULL, HEX);
|
|
258
|
292
|
log_i("retV:0x%x",iAttribute);
|
|
259
|
|
-
|
|
260
|
|
-
|
|
261
|
293
|
return iAttribute;
|
|
262
|
|
-
|
|
263
|
294
|
}
|
|
264
|
295
|
|
|
265
|
296
|
std::string ProtocolImpl::sGetItemParaConfig(Item *pitem, std::string attribute)
|
|
|
@@ -269,9 +300,7 @@ std::string ProtocolImpl::sGetItemParaConfig(Item *pitem, std::string attribute
|
|
269
|
300
|
string sAttribute = "";
|
|
270
|
301
|
sAttribute = t_base.getParam(attribute);
|
|
271
|
302
|
log_i("%s: %s",attribute.c_str(), sAttribute.c_str());
|
|
272
|
|
-
|
|
273
|
303
|
return sAttribute;
|
|
274
|
|
-
|
|
275
|
304
|
}
|
|
276
|
305
|
|
|
277
|
306
|
void ProtocolImpl::setData(Item *pitem,int16_t pbuf,int receiveData,int paraData,std::string t_sDataType,std::string t_sByteOrder,string t_sBits)
|
|
|
@@ -288,8 +317,6 @@ void ProtocolImpl::setData(Item *pitem,int16_t pbuf,int receiveData,int paraData
|
|
288
|
317
|
{
|
|
289
|
318
|
log_i("数据类型为I,调用merge16函数,buf=0x%x",buf);
|
|
290
|
319
|
t_item->setValue(merge16(buf, t_sByteOrder));
|
|
291
|
|
-
|
|
292
|
|
-
|
|
293
|
320
|
}
|
|
294
|
321
|
else if (t_sDataType == "UI")
|
|
295
|
322
|
{
|
|
|
@@ -314,15 +341,153 @@ void ProtocolImpl::setData(Item *pitem,int16_t pbuf,int receiveData,int paraData
|
|
314
|
341
|
snprintf(t, 256, "%d", bits);
|
|
315
|
342
|
t_str = t;
|
|
316
|
343
|
t_item->setValue(t_str);
|
|
317
|
|
-
|
|
318
|
344
|
}
|
|
319
|
|
-
|
|
320
|
345
|
}
|
|
321
|
346
|
}
|
|
|
347
|
+}
|
|
|
348
|
+
|
|
|
349
|
+int ProtocolImpl::set_gpio_value(const std::string& gpio_path, const std::string& value)
|
|
|
350
|
+{
|
|
|
351
|
+ int fd = open(gpio_path.c_str(), O_WRONLY);
|
|
|
352
|
+ if (fd < 0)
|
|
|
353
|
+ {
|
|
|
354
|
+ log_e("Failed to open GPIO value file: %s", gpio_path.c_str());
|
|
|
355
|
+ return -1;
|
|
|
356
|
+ }
|
|
|
357
|
+
|
|
|
358
|
+ if (write(fd, value.c_str(), value.length()) < 0)
|
|
|
359
|
+ {
|
|
|
360
|
+ log_e("Failed to write to GPIO value file: %s", gpio_path.c_str());
|
|
|
361
|
+ close(fd);
|
|
|
362
|
+ return -1;
|
|
|
363
|
+ }
|
|
|
364
|
+
|
|
|
365
|
+ close(fd);
|
|
|
366
|
+ return 0;
|
|
|
367
|
+}
|
|
|
368
|
+
|
|
|
369
|
+
|
|
|
370
|
+void ProtocolImpl::proceessReplyFrame(int frame,int datas)
|
|
|
371
|
+{
|
|
|
372
|
+ log_i("当前状态:%d ---0-初始化,1-发送,2-接收,3-结束",currentState);
|
|
|
373
|
+ if (frame == 0x7F0) // 总主分配ID回复帧的CANID
|
|
|
374
|
+ {
|
|
|
375
|
+ log_i("processReply datas = %d",datas);
|
|
|
376
|
+ unsigned char reply_id = datas; // 回复自身的ID编号
|
|
|
377
|
+ log_i("reply_id = 0x%x",reply_id);
|
|
|
378
|
+ if (reply_id == currentId)
|
|
|
379
|
+ {
|
|
|
380
|
+ log_i("收到设备%d的回复", reply_id);
|
|
|
381
|
+ if(currentId == 2)
|
|
|
382
|
+ {
|
|
|
383
|
+ log_i("回复帧,currentId = %d",currentId );
|
|
|
384
|
+ currentState = END;
|
|
|
385
|
+ idSuccessFlag =2;
|
|
|
386
|
+ }
|
|
|
387
|
+ else
|
|
|
388
|
+ {
|
|
|
389
|
+ if (orderMode == 0) // 根据分配次序修改下一个要分配的ID
|
|
|
390
|
+ {
|
|
|
391
|
+ currentId ++; // 正序分配
|
|
|
392
|
+ currentState = SEND;
|
|
|
393
|
+ }
|
|
|
394
|
+ else
|
|
|
395
|
+ {
|
|
|
396
|
+ currentId --; // 倒序分配
|
|
|
397
|
+ currentState = SEND;
|
|
|
398
|
+ }
|
|
|
399
|
+ // 使能下一个设备
|
|
|
400
|
+ //数据发送正确,且有回复,开始发送下一帧
|
|
|
401
|
+ }
|
|
|
402
|
+
|
|
|
403
|
+
|
|
|
404
|
+ }
|
|
|
405
|
+ else
|
|
|
406
|
+ {
|
|
|
407
|
+ currentId = 1;
|
|
|
408
|
+ idSuccessFlag = 0;
|
|
|
409
|
+ log_i("收到错误的回复帧,ID不匹配");
|
|
|
410
|
+ }
|
|
|
411
|
+ }
|
|
|
412
|
+ else
|
|
|
413
|
+ {
|
|
|
414
|
+ //currentId = 1;
|
|
|
415
|
+ currentState = SEND;
|
|
|
416
|
+ idSuccessFlag = 0;
|
|
|
417
|
+ log_i("收到未知的CAN帧");
|
|
|
418
|
+ }
|
|
|
419
|
+ if(idSuccessFlag == 2)
|
|
|
420
|
+ {
|
|
|
421
|
+ findAddrFlag = 1;
|
|
|
422
|
+ currentId = 1;
|
|
|
423
|
+ currentState = END;
|
|
|
424
|
+ }
|
|
322
|
425
|
|
|
323
|
426
|
|
|
324
|
427
|
}
|
|
325
|
428
|
|
|
|
429
|
+//发送分配帧
|
|
|
430
|
+void ProtocolImpl::sendAllocFrame(PBYTE pbuf, int id, int &len)
|
|
|
431
|
+{
|
|
|
432
|
+ pbuf[0] = 0x00;
|
|
|
433
|
+ pbuf[1] = 0x00;
|
|
|
434
|
+ pbuf[2] = 0x07;
|
|
|
435
|
+ pbuf[3] = 0xf0;
|
|
|
436
|
+ pbuf[4] = static_cast<uint8_t>(id); // 转换为字节
|
|
|
437
|
+ pbuf[5] = static_cast<uint8_t>(maxId); // 转换为字节
|
|
|
438
|
+ pbuf[6] = static_cast<uint8_t>(orderMode); // 转换为字节
|
|
|
439
|
+ pbuf[7] = static_cast<uint8_t>(orderState); // 转换为字节
|
|
|
440
|
+ pbuf[8] = 0;
|
|
|
441
|
+ pbuf[9] = 0;
|
|
|
442
|
+ pbuf[10] = 0;
|
|
|
443
|
+ pbuf[11] = 0;
|
|
|
444
|
+ // 打印调试信息
|
|
|
445
|
+ log_i("发送分配地址帧: ");
|
|
|
446
|
+ for (int i = 0; i < 8; i++) {
|
|
|
447
|
+ log_i("%02X ", pbuf[i]);
|
|
|
448
|
+ }
|
|
|
449
|
+ currentState = RECEIVED ;
|
|
|
450
|
+
|
|
|
451
|
+ len = 12;
|
|
|
452
|
+
|
|
|
453
|
+}
|
|
|
454
|
+
|
|
|
455
|
+void ProtocolImpl::initIO()
|
|
|
456
|
+{
|
|
|
457
|
+ currentId = 1;
|
|
|
458
|
+ if(count == 1)
|
|
|
459
|
+ {
|
|
|
460
|
+ if (set_gpio_value(devPath, lowLevel) == 0)
|
|
|
461
|
+ {
|
|
|
462
|
+ log_i("Set GPIO 436 to high");
|
|
|
463
|
+ }
|
|
|
464
|
+ else
|
|
|
465
|
+ {
|
|
|
466
|
+ log_e("Failed to set GPIO 436 to high");
|
|
|
467
|
+ }
|
|
|
468
|
+ }
|
|
|
469
|
+ else
|
|
|
470
|
+ {
|
|
|
471
|
+ if(count > 4)
|
|
|
472
|
+ {
|
|
|
473
|
+ // 设置 GPIO 436 为低电平
|
|
|
474
|
+ if (set_gpio_value(devPath, highLevel) == 0)
|
|
|
475
|
+ {
|
|
|
476
|
+ log_i("Set GPIO 436 to low");
|
|
|
477
|
+ currentState = SEND;
|
|
|
478
|
+ }
|
|
|
479
|
+ else
|
|
|
480
|
+ {
|
|
|
481
|
+ log_e("Failed to set GPIO 436 to low");
|
|
|
482
|
+ }
|
|
|
483
|
+
|
|
|
484
|
+ count = 0;
|
|
|
485
|
+
|
|
|
486
|
+ }
|
|
|
487
|
+ }
|
|
|
488
|
+ count ++;
|
|
|
489
|
+}
|
|
|
490
|
+
|
|
326
|
491
|
HRESULT ProtocolImpl::queryInterface(const IID &iid, void **ppv)
|
|
327
|
492
|
{
|
|
328
|
493
|
if (iid == IID_IProtocol)
|
|
|
@@ -377,146 +542,236 @@ ULONG ProtocolImpl::getVersion()
|
|
377
|
542
|
HRESULT ProtocolImpl::onRead(Device *pdevice, Packet *ppacket, Item *pitem,
|
|
378
|
543
|
PBYTE pbuf, int &len)
|
|
379
|
544
|
{
|
|
380
|
|
- log_i("onRead");
|
|
381
|
|
-
|
|
382
|
|
-
|
|
383
|
545
|
|
|
|
546
|
+ log_i("onRead");
|
|
|
547
|
+ int deviceCounts;
|
|
|
548
|
+ Channel *pC = pdevice->getParent();
|
|
384
|
549
|
|
|
385
|
|
- if (ppacket->getVitem().size() == 0)
|
|
|
550
|
+ log_i("Channel pointer: %p", pC);
|
|
|
551
|
+ if (pC == nullptr)
|
|
386
|
552
|
{
|
|
|
553
|
+ log_i("Channel pointer is null");
|
|
387
|
554
|
return S_FALSE;
|
|
388
|
555
|
}
|
|
|
556
|
+ if (m_tmpItems.count(pC->getBase().getObjid().toString()) == 0)
|
|
|
557
|
+ {
|
|
|
558
|
+ deviceCounts = pC->getDeviceCount();
|
|
|
559
|
+ //maxId = deviceCounts;
|
|
|
560
|
+ maxId = 2;
|
|
389
|
561
|
|
|
390
|
|
- pdevice->getBase().setRwstate(READ_WAIT); //----设置设备状态
|
|
391
|
|
-
|
|
392
|
|
- std::string sParaId;
|
|
393
|
|
- int iParaId;
|
|
394
|
|
- Base &t_base = ppacket->getVitem().at(0)->getBase();
|
|
395
|
|
- sParaId = t_base.getParam("功能码");
|
|
396
|
|
- log_i("Parameter ID: %s", sParaId.c_str());
|
|
397
|
|
- iParaId = strtol(sParaId.c_str(), NULL, HEX);
|
|
398
|
|
- log_i("Parameter ID: %s", sParaId.c_str());
|
|
399
|
|
- log_i("Converted Parameter ID: 0x%x", iParaId);
|
|
400
|
|
- int frameFuncCode = (iParaId & 0x070000);
|
|
401
|
|
- log_i("frameFuncCode = 0x%x",frameFuncCode);
|
|
402
|
|
- if (ppacket == nullptr || pitem == nullptr)
|
|
|
562
|
+ log_i("maxid =%d",maxId);
|
|
|
563
|
+ }
|
|
|
564
|
+ log_i("当前状态:%d ---0-初始化,1-发送,2-接收,3-结束",currentState);
|
|
|
565
|
+ if(currentState == INIT)
|
|
403
|
566
|
{
|
|
404
|
|
- log_i("Invalid packet or item");
|
|
405
|
|
- return S_FALSE;
|
|
|
567
|
+ initIO();
|
|
406
|
568
|
}
|
|
407
|
|
- string t_saddr;
|
|
408
|
|
- t_saddr = pdevice->getBase().getParam("站地址");
|
|
409
|
|
- int t_iaddr = 0;
|
|
410
|
|
- t_iaddr = strtol(t_saddr.c_str(), NULL, HEX);
|
|
411
|
|
- log_i("stationAddr = %d",t_iaddr);
|
|
412
|
|
- iParaId |= (t_iaddr << 19);
|
|
413
|
|
- log_i("onread -addStation-id = 0x%x",iParaId);
|
|
414
|
|
-
|
|
415
|
|
- //判断寄存器是读寄存器
|
|
416
|
|
- if(frameFuncCode == 0x030000)
|
|
|
569
|
+ if(currentState == SEND)
|
|
|
570
|
+ {
|
|
|
571
|
+ sendAllocFrame(pbuf,currentId , len);
|
|
|
572
|
+ timeOut = 0;
|
|
|
573
|
+ //pdevice->getBase().setRwstate(READ_WAIT);
|
|
|
574
|
+ pdevice->getBase().setRwstate(WRITE_WAIT);
|
|
|
575
|
+ }
|
|
|
576
|
+ if(currentState == RECEIVED)
|
|
|
577
|
+ {
|
|
|
578
|
+ if(timeOut > 30)
|
|
|
579
|
+ {
|
|
|
580
|
+ currentState = INIT;
|
|
|
581
|
+ }
|
|
|
582
|
+ timeOut ++;
|
|
|
583
|
+ }
|
|
|
584
|
+ if(currentState != END)
|
|
417
|
585
|
{
|
|
418
|
|
- int maxRegAddr = getRegAddrMax(ppacket, pitem);
|
|
419
|
|
- int minRegAddr = getRegAddrMin(ppacket, pitem);
|
|
420
|
|
- log_i("MaxRegAddr: 0x%x, MinRegAddr: 0x%x", maxRegAddr, minRegAddr);
|
|
|
586
|
+ log_i("len的长度:%d",len);
|
|
|
587
|
+
|
|
|
588
|
+ return S_OK;
|
|
|
589
|
+ }
|
|
421
|
590
|
|
|
422
|
|
- if (maxRegAddr < minRegAddr)
|
|
|
591
|
+// if(findAddrFlag == 0)
|
|
|
592
|
+// {
|
|
|
593
|
+// log_i("寻址标志是0");
|
|
|
594
|
+// if(currentId == 1)
|
|
|
595
|
+// {
|
|
|
596
|
+// //setGpioDetrict();
|
|
|
597
|
+//
|
|
|
598
|
+// // 设置 GPIO 436 为高电平
|
|
|
599
|
+// log_i("计数count = %d",count);
|
|
|
600
|
+// if(count == 1)
|
|
|
601
|
+// {
|
|
|
602
|
+// if (set_gpio_value(devPath, lowLevel) == 0)
|
|
|
603
|
+// {
|
|
|
604
|
+// log_i("Set GPIO 436 to high");
|
|
|
605
|
+// }
|
|
|
606
|
+// else
|
|
|
607
|
+// {
|
|
|
608
|
+// log_e("Failed to set GPIO 436 to high");
|
|
|
609
|
+// }
|
|
|
610
|
+// }
|
|
|
611
|
+//
|
|
|
612
|
+// else
|
|
|
613
|
+// {
|
|
|
614
|
+// if(count > 4)
|
|
|
615
|
+// {
|
|
|
616
|
+// // 设置 GPIO 436 为低电平
|
|
|
617
|
+// if (set_gpio_value(devPath, highLevel) == 0)
|
|
|
618
|
+// {
|
|
|
619
|
+// log_i("Set GPIO 436 to low");
|
|
|
620
|
+// }
|
|
|
621
|
+// else
|
|
|
622
|
+// {
|
|
|
623
|
+// log_e("Failed to set GPIO 436 to low");
|
|
|
624
|
+// }
|
|
|
625
|
+// sendAllocFrame(pbuf,currentId , len);
|
|
|
626
|
+// count = 0;
|
|
|
627
|
+// }
|
|
|
628
|
+// }
|
|
|
629
|
+// count ++;
|
|
|
630
|
+//
|
|
|
631
|
+//
|
|
|
632
|
+// }
|
|
|
633
|
+// if()
|
|
|
634
|
+// {
|
|
|
635
|
+// sendAllocFrame(pbuf,currentId , len);
|
|
|
636
|
+// }
|
|
|
637
|
+// log_i("len的长度:%d",len);
|
|
|
638
|
+// return S_OK;
|
|
|
639
|
+
|
|
|
640
|
+ //}
|
|
|
641
|
+ if(currentState == END)
|
|
|
642
|
+ //if(findAddrFlag == 1)
|
|
|
643
|
+ {
|
|
|
644
|
+ idSuccessFlag = 0;
|
|
|
645
|
+ if (ppacket->getVitem().size() == 0)
|
|
423
|
646
|
{
|
|
424
|
|
- log_i("Invalid register address range");
|
|
425
|
647
|
return S_FALSE;
|
|
426
|
648
|
}
|
|
427
|
|
- int regNum = maxRegAddr - minRegAddr + 1;
|
|
428
|
649
|
|
|
429
|
|
- pbuf[0] = retBigEndian(iParaId + minRegAddr, 24);
|
|
430
|
|
- pbuf[1] = retBigEndian(iParaId + minRegAddr, 16);
|
|
431
|
|
- pbuf[2] = retBigEndian(iParaId + minRegAddr, 8);
|
|
432
|
|
- pbuf[3] = retBigEndian(iParaId + minRegAddr, 0);
|
|
433
|
|
- pbuf[4] = retBigEndian(regNum, 8);
|
|
434
|
|
- pbuf[5] = retBigEndian(regNum, 0);
|
|
435
|
|
- len = 6;
|
|
436
|
|
-
|
|
437
|
|
- if (pbuf == nullptr)
|
|
|
650
|
+ pdevice->getBase().setRwstate(READ_WAIT); //----设置设备状态
|
|
|
651
|
+
|
|
|
652
|
+ std::string sParaId;
|
|
|
653
|
+ int iParaId;
|
|
|
654
|
+ Base &t_base = ppacket->getVitem().at(0)->getBase();
|
|
|
655
|
+ sParaId = t_base.getParam("功能码");
|
|
|
656
|
+ log_i("Parameter ID: %s", sParaId.c_str());
|
|
|
657
|
+ iParaId = strtol(sParaId.c_str(), NULL, HEX);
|
|
|
658
|
+ log_i("Parameter ID: %s", sParaId.c_str());
|
|
|
659
|
+ log_i("Converted Parameter ID: 0x%x", iParaId);
|
|
|
660
|
+ int frameFuncCode = (iParaId & 0x070000);
|
|
|
661
|
+ log_i("frameFuncCode = 0x%x",frameFuncCode);
|
|
|
662
|
+ if (ppacket == nullptr || pitem == nullptr)
|
|
438
|
663
|
{
|
|
439
|
|
- log_i("pbuf is null");
|
|
|
664
|
+ log_i("Invalid packet or item");
|
|
440
|
665
|
return S_FALSE;
|
|
441
|
666
|
}
|
|
442
|
|
- for (int i = 0; i < 6; i++)
|
|
|
667
|
+ string t_saddr;
|
|
|
668
|
+ t_saddr = pdevice->getBase().getParam("站地址");
|
|
|
669
|
+ int t_iaddr = 0;
|
|
|
670
|
+ t_iaddr = strtol(t_saddr.c_str(), NULL, HEX);
|
|
|
671
|
+ log_i("stationAddr = %d",t_iaddr);
|
|
|
672
|
+ iParaId |= (t_iaddr << 19);
|
|
|
673
|
+ log_i("onread -addStation-id = 0x%x",iParaId);
|
|
|
674
|
+
|
|
|
675
|
+ //判断寄存器是读寄存器
|
|
|
676
|
+ if(frameFuncCode == 0x030000)
|
|
443
|
677
|
{
|
|
444
|
|
- log_i("onRead-读数据:pbuf[%d]:0x%x", i, pbuf[i]);
|
|
|
678
|
+ int maxRegAddr = getRegAddrMax(ppacket, pitem);
|
|
|
679
|
+ int minRegAddr = getRegAddrMin(ppacket, pitem);
|
|
|
680
|
+ log_i("MaxRegAddr: 0x%x, MinRegAddr: 0x%x", maxRegAddr, minRegAddr);
|
|
|
681
|
+
|
|
|
682
|
+ if (maxRegAddr < minRegAddr)
|
|
|
683
|
+ {
|
|
|
684
|
+ log_i("Invalid register address range");
|
|
|
685
|
+ return S_FALSE;
|
|
|
686
|
+ }
|
|
|
687
|
+ int regNum = maxRegAddr - minRegAddr + 1;
|
|
|
688
|
+
|
|
|
689
|
+ pbuf[0] = retBigEndian(iParaId + minRegAddr, 24);
|
|
|
690
|
+ pbuf[1] = retBigEndian(iParaId + minRegAddr, 16);
|
|
|
691
|
+ pbuf[2] = retBigEndian(iParaId + minRegAddr, 8);
|
|
|
692
|
+ pbuf[3] = retBigEndian(iParaId + minRegAddr, 0);
|
|
|
693
|
+ pbuf[4] = retBigEndian(regNum, 8);
|
|
|
694
|
+ pbuf[5] = retBigEndian(regNum, 0);
|
|
|
695
|
+ len = 6;
|
|
|
696
|
+
|
|
|
697
|
+ if (pbuf == nullptr)
|
|
|
698
|
+ {
|
|
|
699
|
+ log_i("pbuf is null");
|
|
|
700
|
+ return S_FALSE;
|
|
|
701
|
+ }
|
|
|
702
|
+ for (int i = 0; i < 6; i++)
|
|
|
703
|
+ {
|
|
|
704
|
+ log_i("onRead-读数据:pbuf[%d]:0x%x", i, pbuf[i]);
|
|
|
705
|
+ }
|
|
445
|
706
|
}
|
|
446
|
|
- }
|
|
447
|
707
|
|
|
448
|
|
- Channel *pC = pdevice->getParent();
|
|
449
|
|
- log_i("Channel pointer: %p", pC);
|
|
450
|
|
- if (pC == nullptr)
|
|
451
|
|
- {
|
|
452
|
|
- log_i("Channel pointer is null");
|
|
453
|
|
- return S_FALSE;
|
|
454
|
|
- }
|
|
455
|
|
- if (pC != NULL)
|
|
456
|
|
- {
|
|
457
|
|
- if (m_tmpItems.count(pC->getBase().getObjid().toString()) == 0)
|
|
|
708
|
+ //初始化数据库容器
|
|
|
709
|
+ if (pC != NULL)
|
|
458
|
710
|
{
|
|
459
|
|
- log_i("Temporary items not found for Channel ID: %s",
|
|
460
|
|
- pC->getBase().getObjid().toString().c_str());
|
|
461
|
|
- int ccount = pC->getDeviceCount();
|
|
462
|
|
- log_i("Channel device count: %d", ccount);
|
|
463
|
|
- std::multimap<int, Item*> tmpItems;
|
|
464
|
|
- for (int i = 0; i < ccount; i++)
|
|
|
711
|
+ if (m_tmpItems.count(pC->getBase().getObjid().toString()) == 0)
|
|
465
|
712
|
{
|
|
466
|
|
-
|
|
467
|
|
- for (size_t j = 0;
|
|
468
|
|
- j < pC->getVDevice().at(i)->getVitem().size(); j++)
|
|
|
713
|
+ log_i("Temporary items not found for Channel ID: %s",
|
|
|
714
|
+ pC->getBase().getObjid().toString().c_str());
|
|
|
715
|
+ int ccount = pC->getDeviceCount();
|
|
|
716
|
+ log_i("Channel device count: %d", ccount);
|
|
|
717
|
+ std::multimap<int, Item*> tmpItems;
|
|
|
718
|
+ for (int i = 0; i < ccount; i++)
|
|
469
|
719
|
{
|
|
470
|
|
- string t_sId;
|
|
471
|
|
- Base &t_base =
|
|
472
|
|
- pC->getVDevice().at(i)->getVitem().at(j)->getBase();
|
|
473
|
|
- t_sId = t_base.getParam("写功能码");
|
|
474
|
|
- log_i("写功能码: %s", t_sId.c_str());
|
|
475
|
|
- int t_iId = 0;
|
|
476
|
|
- t_iId = strtol(t_sId.c_str(), NULL, HEX); //----获取帧ID
|
|
477
|
|
- log_i("功能码转换为整数: 0x%x", t_iId);
|
|
478
|
|
- string t_stype;
|
|
479
|
|
- t_stype = t_base.getParam("帧类型");
|
|
480
|
|
- log_i("帧类型: %s", t_stype.c_str());
|
|
481
|
|
-
|
|
482
|
|
- if (t_stype == "1")
|
|
|
720
|
+
|
|
|
721
|
+ for (size_t j = 0;
|
|
|
722
|
+ j < pC->getVDevice().at(i)->getVitem().size(); j++)
|
|
483
|
723
|
{
|
|
484
|
|
- t_iId |= 0x80000000;
|
|
485
|
|
- log_i("帧类型为1,功能码更新为: 0x%x", t_iId);
|
|
486
|
|
- }
|
|
|
724
|
+ string t_sId;
|
|
|
725
|
+ Base &t_base =
|
|
|
726
|
+ pC->getVDevice().at(i)->getVitem().at(j)->getBase();
|
|
|
727
|
+ t_sId = t_base.getParam("写功能码");
|
|
|
728
|
+ log_i("写功能码: %s", t_sId.c_str());
|
|
|
729
|
+ int t_iId = 0;
|
|
|
730
|
+ t_iId = strtol(t_sId.c_str(), NULL, HEX); //----获取帧ID
|
|
|
731
|
+ log_i("功能码转换为整数: 0x%x", t_iId);
|
|
|
732
|
+ string t_stype;
|
|
|
733
|
+ t_stype = t_base.getParam("帧类型");
|
|
|
734
|
+ log_i("帧类型: %s", t_stype.c_str());
|
|
|
735
|
+
|
|
|
736
|
+ if (t_stype == "1")
|
|
|
737
|
+ {
|
|
|
738
|
+ t_iId |= 0x80000000;
|
|
|
739
|
+ log_i("帧类型为1,功能码更新为: 0x%x", t_iId);
|
|
|
740
|
+ }
|
|
487
|
741
|
|
|
488
|
|
- string sStartAddr;
|
|
489
|
|
- //sStartAddr = pC->getVDevice().at(i)->getBase().getParam("起始地址");
|
|
490
|
|
- sStartAddr = t_base.getParam("起始地址");
|
|
491
|
|
- log_i("起始地址 =%s", sStartAddr.c_str());
|
|
492
|
|
-
|
|
493
|
|
- int iStartAddr = 0;
|
|
494
|
|
- iStartAddr = strtol(sStartAddr.c_str(), NULL, HEX);
|
|
495
|
|
- log_i("起始地址转换为整数: 0x%x", iStartAddr);
|
|
496
|
|
- t_iId += iStartAddr;
|
|
497
|
|
- string t_saddr;
|
|
498
|
|
- t_saddr = pC->getVDevice().at(i)->getBase().getParam("站地址");
|
|
499
|
|
- int t_iaddr = 0;
|
|
500
|
|
- t_iaddr = strtol(t_saddr.c_str(), NULL, HEX);
|
|
501
|
|
- t_iId |= (t_iaddr << 19);
|
|
502
|
|
- tmpItems.insert(
|
|
503
|
|
- make_pair(t_iId,
|
|
504
|
|
- pC->getVDevice().at(i)->getVitem().at(j)));
|
|
505
|
|
- log_i("插入临时条目: ID=0x%x", t_iId);
|
|
|
742
|
+ string sStartAddr;
|
|
|
743
|
+ //sStartAddr = pC->getVDevice().at(i)->getBase().getParam("起始地址");
|
|
|
744
|
+ sStartAddr = t_base.getParam("起始地址");
|
|
|
745
|
+ log_i("起始地址 =%s", sStartAddr.c_str());
|
|
|
746
|
+
|
|
|
747
|
+ int iStartAddr = 0;
|
|
|
748
|
+ iStartAddr = strtol(sStartAddr.c_str(), NULL, HEX);
|
|
|
749
|
+ log_i("起始地址转换为整数: 0x%x", iStartAddr);
|
|
|
750
|
+ t_iId += iStartAddr;
|
|
|
751
|
+ string t_saddr;
|
|
|
752
|
+ t_saddr = pC->getVDevice().at(i)->getBase().getParam("站地址");
|
|
|
753
|
+ int t_iaddr = 0;
|
|
|
754
|
+ t_iaddr = strtol(t_saddr.c_str(), NULL, HEX);
|
|
|
755
|
+ t_iId |= (t_iaddr << 19);
|
|
|
756
|
+ tmpItems.insert(
|
|
|
757
|
+ make_pair(t_iId,
|
|
|
758
|
+ pC->getVDevice().at(i)->getVitem().at(j)));
|
|
|
759
|
+ log_i("插入临时条目: ID=0x%x", t_iId);
|
|
|
760
|
+ }
|
|
506
|
761
|
}
|
|
507
|
|
- }
|
|
508
|
762
|
|
|
509
|
|
- m_tmpItems.insert(
|
|
510
|
|
- make_pair(pC->getBase().getObjid().toString(), tmpItems));
|
|
511
|
|
- }
|
|
512
|
|
- else
|
|
513
|
|
- {
|
|
514
|
|
- log_i("Temporary items already exist for Channel ID: %s",
|
|
515
|
|
- pC->getBase().getObjid().toString().c_str());
|
|
|
763
|
+ m_tmpItems.insert(
|
|
|
764
|
+ make_pair(pC->getBase().getObjid().toString(), tmpItems));
|
|
|
765
|
+ }
|
|
|
766
|
+ else
|
|
|
767
|
+ {
|
|
|
768
|
+ log_i("Temporary items already exist for Channel ID: %s",
|
|
|
769
|
+ pC->getBase().getObjid().toString().c_str());
|
|
|
770
|
+ }
|
|
516
|
771
|
}
|
|
517
|
|
- }
|
|
518
|
772
|
|
|
519
|
|
- return S_OK;
|
|
|
773
|
+ return S_OK;
|
|
|
774
|
+ }
|
|
520
|
775
|
}
|
|
521
|
776
|
|
|
522
|
777
|
/**
|
|
|
@@ -659,7 +914,10 @@ HRESULT ProtocolImpl::isResponseOK(Device *pdevice, Packet *ppacket,
|
|
659
|
914
|
{
|
|
660
|
915
|
log_i("isResponseOk");
|
|
661
|
916
|
int ret;
|
|
662
|
|
-
|
|
|
917
|
+ if(findAddrFlag == 0)
|
|
|
918
|
+ {
|
|
|
919
|
+ return S_OK;
|
|
|
920
|
+ }
|
|
663
|
921
|
switch (pdevice->getBase().getRwstate())
|
|
664
|
922
|
{
|
|
665
|
923
|
case READ_O:
|
|
|
@@ -689,162 +947,188 @@ HRESULT ProtocolImpl::isResponseOK(Device *pdevice, Packet *ppacket,
|
|
689
|
947
|
|
|
690
|
948
|
}
|
|
691
|
949
|
|
|
692
|
|
-/**
|
|
693
|
|
- * @content:解析数据
|
|
694
|
|
- * @time:2016-9-9
|
|
695
|
|
- * @author:Mr_zhu
|
|
696
|
|
- * @param: pdevice(设备句柄),ppacket(包句柄),pitem(点句柄),pbuf(数据缓冲区),len(返回报文长度),deletelen(待删除数据长度)
|
|
697
|
|
- * @return: HRESULT(S_OK,S_FALSE)
|
|
698
|
|
- * @decribe
|
|
699
|
|
- * 1#2016-9-9#V1.0#首次生成
|
|
700
|
|
- */
|
|
701
|
|
-HRESULT ProtocolImpl::onResponse(Device *pdevice, Packet *ppacket, Item *pitem,
|
|
702
|
|
- PBYTE pbuf, const int len, int &deletelen)
|
|
|
950
|
+//寻址未成功,解析寻址报文
|
|
|
951
|
+void ProtocolImpl::ioResponse(Packet *ppacket, Item *pitem,PBYTE pbuf, const int len)
|
|
703
|
952
|
{
|
|
|
953
|
+ std::map<int, int> dataSets; // 存储寄存器地址和值
|
|
|
954
|
+ int id;
|
|
|
955
|
+ int16_t buf;
|
|
|
956
|
+ dataSets=retIoDataSet(ppacket, pitem, pbuf, len);
|
|
|
957
|
+ log_i("pbuf[4]= 0x%x,pbuf[5] = 0x%x,pbuf[6] = 0x%x,pbuf[7] = 0x%x",pbuf[4],pbuf[5],pbuf[6],pbuf[7]);
|
|
704
|
958
|
|
|
705
|
|
- log_i("onResponse");
|
|
706
|
|
- int validlen = len;
|
|
707
|
|
- int t_iRWState = pdevice->getBase().getRwstate(); //----设备读写状态
|
|
708
|
|
- Base *pbase = new Base();
|
|
709
|
|
- Base &base = *pbase;
|
|
|
959
|
+ for (const auto &entry : dataSets)
|
|
|
960
|
+ {
|
|
|
961
|
+ log_i("addrFrame = 0x%x, value = 0x%x", entry.first,
|
|
|
962
|
+ entry.second);
|
|
|
963
|
+ }
|
|
|
964
|
+ for (const auto &dataPair : dataSets)
|
|
|
965
|
+ {
|
|
|
966
|
+ log_i("addData.size=%d", dataSets.size());
|
|
|
967
|
+ id = dataPair.first;
|
|
710
|
968
|
|
|
711
|
|
- std::vector<Item*> items = ppacket->getVitem();
|
|
712
|
|
- std::map<int, int> dataSet; // 存储寄存器地址和值
|
|
|
969
|
+ id = (0x7fffffff) & id;
|
|
|
970
|
+ buf = dataPair.second;
|
|
|
971
|
+ log_i("id = 0x%x,dataPair.second =0x%x",id,buf);
|
|
|
972
|
+
|
|
|
973
|
+ if(currentState == RECEIVED )
|
|
|
974
|
+ {
|
|
|
975
|
+ proceessReplyFrame(id,buf);
|
|
|
976
|
+ }
|
|
|
977
|
+ }
|
|
|
978
|
+
|
|
|
979
|
+}
|
|
|
980
|
+
|
|
|
981
|
+//寻址成功,解析BMS传的数据
|
|
|
982
|
+int ProtocolImpl::normalDatasResponse(Device* pdevice,Packet* ppacket,Item* pitem,PBYTE pbuf,const int len)
|
|
|
983
|
+{
|
|
|
984
|
+ std::map<int, int> dataSet;
|
|
713
|
985
|
int id;
|
|
714
|
986
|
int16_t buf;
|
|
715
|
|
-
|
|
716
|
987
|
Channel *pC = pdevice->getParent();
|
|
|
988
|
+
|
|
717
|
989
|
if (pC == NULL)
|
|
718
|
990
|
{
|
|
719
|
991
|
log_e("%s, 父通道为空, 无法进行数据解析", pdevice->getBase().getName().c_str());
|
|
720
|
992
|
return S_FALSE;
|
|
721
|
993
|
}
|
|
722
|
|
- switch (t_iRWState)
|
|
723
|
|
- {
|
|
724
|
|
- case READ_O:
|
|
725
|
|
- case READ_WAIT:
|
|
726
|
|
- case WRITE_WAIT:
|
|
727
|
|
- log_i("WRITE_WAIT");
|
|
728
|
|
- if (validlen >= 6)
|
|
729
|
|
- {
|
|
730
|
|
- dataSet=retDataSet(ppacket, pitem,pbuf,len);
|
|
731
|
|
- log_i("pbuf[4]= 0x%x,pbuf[5] = 0x%x",pbuf[4],pbuf[5]);
|
|
|
994
|
+ dataSet = retDataSet(ppacket, pitem,pbuf,len);
|
|
|
995
|
+ log_i("pbuf[4]= 0x%x,pbuf[5] = 0x%x,pbuf[6] = 0x%x,pbuf[7] = 0x%x",pbuf[4],pbuf[5],pbuf[6],pbuf[7]);
|
|
732
|
996
|
|
|
733
|
|
- for (const auto &entry : dataSet)
|
|
734
|
|
- {
|
|
735
|
|
- log_i("addrFrame = 0x%x, value = 0x%x", entry.first,
|
|
736
|
|
- entry.second);
|
|
737
|
|
- }
|
|
738
|
|
- for (const auto &dataPair : dataSet)
|
|
739
|
|
- {
|
|
740
|
|
- log_i("addData.size=%d", dataSet.size());
|
|
741
|
|
- id = dataPair.first;
|
|
742
|
|
-
|
|
743
|
|
- id = (0x7fffffff) & id;
|
|
744
|
|
- buf = dataPair.second;
|
|
745
|
|
- log_i("dataPair.second =0x%x",buf);
|
|
|
997
|
+ for (const auto &entry : dataSet)
|
|
|
998
|
+ {
|
|
|
999
|
+ log_i("addrFrame = 0x%x, value = 0x%x", entry.first,
|
|
|
1000
|
+ entry.second);
|
|
|
1001
|
+ }
|
|
|
1002
|
+ for (const auto &dataPair : dataSet)
|
|
|
1003
|
+ {
|
|
|
1004
|
+ log_i("addData.size=%d", dataSet.size());
|
|
|
1005
|
+ id = dataPair.first;
|
|
746
|
1006
|
|
|
|
1007
|
+ id = (0x7fffffff) & id;
|
|
|
1008
|
+ buf = dataPair.second;
|
|
747
|
1009
|
|
|
748
|
|
- //buf = (PBYTE) &dataPair.second;
|
|
749
|
|
- //log_i("buf[0] =0x%x,buf[1] =0x%x",buf[0],buf[1]);
|
|
|
1010
|
+ log_i("Processing data pair with ID: 0x%x", id);
|
|
750
|
1011
|
|
|
|
1012
|
+ if (m_tmpItems.count(pC->getBase().getObjid().toString()) > 0)
|
|
|
1013
|
+ {
|
|
751
|
1014
|
|
|
752
|
|
- log_i("Processing data pair with ID: 0x%x", id);
|
|
|
1015
|
+ log_i("Found temporary items for Channel ID: %s",
|
|
|
1016
|
+ pC->getBase().getObjid().toString().c_str());
|
|
|
1017
|
+ std::multimap<int, Item*> tmpItms = m_tmpItems.find(
|
|
|
1018
|
+ pC->getBase().getObjid().toString())->second; //是否有效
|
|
|
1019
|
+ for (const auto &data : tmpItms)
|
|
|
1020
|
+ {
|
|
|
1021
|
+ log_i("缓存条目中的缓存地址:0x%x,0x%x", data.first, data.second);
|
|
|
1022
|
+ }
|
|
|
1023
|
+ log_i("Temporary items size: %zu", tmpItms.size());
|
|
753
|
1024
|
|
|
754
|
|
- if (m_tmpItems.count(pC->getBase().getObjid().toString()) > 0)
|
|
|
1025
|
+ std::multimap<int, Item*>::size_type cnt = tmpItms.count(
|
|
|
1026
|
+ id);
|
|
|
1027
|
+ log_i("Number of items with ID 0x%x: %zu", id, cnt);
|
|
|
1028
|
+ std::multimap<int, Item*>::iterator iter = tmpItms.find(id);
|
|
|
1029
|
+ if (iter != tmpItms.end())
|
|
|
1030
|
+ {
|
|
|
1031
|
+ for (; cnt > 0; cnt--, iter++)
|
|
755
|
1032
|
{
|
|
|
1033
|
+ log_i("Processing item %zu for ID 0x%x", cnt, id);
|
|
|
1034
|
+ Item *t_item = iter->second;
|
|
|
1035
|
+ //Device *ptdevice = (Device*) (t_item->getParent());
|
|
756
|
1036
|
|
|
757
|
|
- log_i("Found temporary items for Channel ID: %s",
|
|
758
|
|
- pC->getBase().getObjid().toString().c_str());
|
|
759
|
|
- std::multimap<int, Item*> tmpItms = m_tmpItems.find(
|
|
760
|
|
- pC->getBase().getObjid().toString())->second; //是否有效
|
|
761
|
|
- for (const auto &data : tmpItms)
|
|
|
1037
|
+ if (t_item != NULL && pdevice != NULL)
|
|
762
|
1038
|
{
|
|
763
|
|
- log_i("缓存条目中的缓存地址:0x%x,0x%x", data.first, data.second);
|
|
|
1039
|
+ log_i("Item and Device are valid");
|
|
|
1040
|
+ int iId = iGetItemParaConfig(t_item,"写功能码");
|
|
|
1041
|
+ int iStartAddr = iGetItemParaConfig(t_item,"起始地址");
|
|
|
1042
|
+ string t_stype = sGetItemParaConfig(t_item,"帧类型");
|
|
|
1043
|
+ string t_sDataType = sGetItemParaConfig(t_item,"数据类型");
|
|
|
1044
|
+ string t_sByteOrder = sGetItemParaConfig(t_item,"字节序");
|
|
|
1045
|
+ string t_sBits= sGetItemParaConfig(t_item ,"位地址");
|
|
|
1046
|
+ iId += iStartAddr;
|
|
|
1047
|
+ string t_saddr;
|
|
|
1048
|
+ t_saddr = pdevice->getBase().getParam("站地址");
|
|
|
1049
|
+ int t_iaddr = 0;
|
|
|
1050
|
+ t_iaddr = strtol(t_saddr.c_str(), NULL, HEX);
|
|
|
1051
|
+ log_i("t_iId = 0x%x,t_iaddr =%d,",iId,t_iaddr);
|
|
|
1052
|
+
|
|
|
1053
|
+ iId |= (t_iaddr << 19);
|
|
|
1054
|
+
|
|
|
1055
|
+ log_i("设置的数据buf=%d,paraID = 0x%x",buf,iId);
|
|
|
1056
|
+ setData(t_item, buf,id,iId,t_sDataType,t_sByteOrder,t_sBits);
|
|
764
|
1057
|
}
|
|
765
|
|
- log_i("Temporary items size: %zu", tmpItms.size());
|
|
766
|
1058
|
|
|
767
|
|
- std::multimap<int, Item*>::size_type cnt = tmpItms.count(
|
|
768
|
|
- id);
|
|
769
|
|
- log_i("Number of items with ID 0x%x: %zu", id, cnt);
|
|
770
|
|
- std::multimap<int, Item*>::iterator iter = tmpItms.find(id);
|
|
771
|
|
- if (iter != tmpItms.end())
|
|
|
1059
|
+ else
|
|
772
|
1060
|
{
|
|
773
|
|
- for (; cnt > 0; cnt--, iter++)
|
|
|
1061
|
+ if(t_item == NULL)
|
|
774
|
1062
|
{
|
|
775
|
|
- log_i("Processing item %zu for ID 0x%x", cnt, id);
|
|
776
|
|
- Item *t_item = iter->second;
|
|
777
|
|
- //Device *ptdevice = (Device*) (t_item->getParent());
|
|
778
|
|
-
|
|
779
|
|
- if (t_item != NULL && pdevice != NULL)
|
|
780
|
|
- {
|
|
781
|
|
- log_i("Item and Device are valid");
|
|
782
|
|
- int iId = iGetItemParaConfig(t_item,"写功能码");
|
|
783
|
|
- int iStartAddr = iGetItemParaConfig(t_item,"起始地址");
|
|
784
|
|
- string t_stype = sGetItemParaConfig(t_item,"帧类型");
|
|
785
|
|
- string t_sDataType = sGetItemParaConfig(t_item,"数据类型");
|
|
786
|
|
- string t_sByteOrder = sGetItemParaConfig(t_item,"字节序");
|
|
787
|
|
- string t_sBits= sGetItemParaConfig(t_item ,"位地址");
|
|
788
|
|
- iId += iStartAddr;
|
|
789
|
|
-
|
|
790
|
|
- string t_saddr;
|
|
791
|
|
- t_saddr = pdevice->getBase().getParam("站地址");
|
|
792
|
|
- int t_iaddr = 0;
|
|
793
|
|
- t_iaddr = strtol(t_saddr.c_str(), NULL, HEX);
|
|
794
|
|
- log_i("t_iId = 0x%x,t_iaddr =%d,",iId,t_iaddr);
|
|
795
|
|
-
|
|
796
|
|
- iId |= (t_iaddr << 19);
|
|
797
|
|
-
|
|
798
|
|
- log_i("设置的数据buf=%d,paraID = 0x%x",buf,iId);
|
|
799
|
|
- setData(t_item, buf,id,iId,t_sDataType,t_sByteOrder,t_sBits);
|
|
800
|
|
- }
|
|
801
|
|
-
|
|
802
|
|
- else
|
|
803
|
|
- {
|
|
804
|
|
- if(t_item == NULL)
|
|
805
|
|
- {
|
|
806
|
|
- log_i("......item== NULL......");
|
|
807
|
|
- }
|
|
808
|
|
- return S_FALSE;
|
|
809
|
|
- }
|
|
|
1063
|
+ log_i("......item== NULL......");
|
|
810
|
1064
|
}
|
|
|
1065
|
+ return S_FALSE;
|
|
811
|
1066
|
}
|
|
812
|
1067
|
}
|
|
813
|
|
- else
|
|
814
|
|
- {
|
|
815
|
|
- log_i("m_tmpItems.count(pC->getBase().getObjid().toString()) <0");
|
|
816
|
|
- return S_FALSE;
|
|
817
|
|
- }
|
|
818
|
|
-
|
|
819
|
1068
|
}
|
|
820
|
|
-
|
|
821
|
|
- return S_OK;
|
|
822
|
|
-
|
|
823
|
|
-
|
|
824
|
1069
|
}
|
|
825
|
|
-
|
|
826
|
|
- if (pitem != nullptr)
|
|
|
1070
|
+ else
|
|
827
|
1071
|
{
|
|
828
|
|
- delete pitem;
|
|
|
1072
|
+ log_i("m_tmpItems.count(pC->getBase().getObjid().toString()) <0");
|
|
|
1073
|
+ return S_FALSE;
|
|
829
|
1074
|
}
|
|
|
1075
|
+ }
|
|
|
1076
|
+ return S_OK;
|
|
|
1077
|
+}
|
|
830
|
1078
|
|
|
831
|
|
- break;
|
|
832
|
1079
|
|
|
833
|
|
- case WRITE_O:
|
|
834
|
|
- break;
|
|
835
|
1080
|
|
|
836
|
|
- default:
|
|
837
|
|
- break;
|
|
838
|
|
- }
|
|
839
|
|
- if (pbase != NULL)
|
|
|
1081
|
+
|
|
|
1082
|
+
|
|
|
1083
|
+/**
|
|
|
1084
|
+ * @content:解析数据
|
|
|
1085
|
+ * @time:2016-9-9
|
|
|
1086
|
+ * @author:Mr_zhu
|
|
|
1087
|
+ * @param: pdevice(设备句柄),ppacket(包句柄),pitem(点句柄),pbuf(数据缓冲区),len(返回报文长度),deletelen(待删除数据长度)
|
|
|
1088
|
+ * @return: HRESULT(S_OK,S_FALSE)
|
|
|
1089
|
+ * @decribe
|
|
|
1090
|
+ * 1#2016-9-9#V1.0#首次生成
|
|
|
1091
|
+ */
|
|
|
1092
|
+HRESULT ProtocolImpl::onResponse(Device *pdevice, Packet *ppacket, Item *pitem,
|
|
|
1093
|
+ PBYTE pbuf, const int len, int &deletelen)
|
|
|
1094
|
+{
|
|
|
1095
|
+ log_i("onResponse");
|
|
|
1096
|
+ int validlen = len;
|
|
|
1097
|
+ int t_iRWState = pdevice->getBase().getRwstate(); //----设备读写状态
|
|
|
1098
|
+ std::vector<Item*> items = ppacket->getVitem();
|
|
|
1099
|
+ switch (t_iRWState)
|
|
|
1100
|
+ {
|
|
|
1101
|
+ case READ_O:
|
|
|
1102
|
+ case READ_WAIT:
|
|
|
1103
|
+ case WRITE_WAIT:
|
|
|
1104
|
+ log_i("WRITE_WAIT");
|
|
|
1105
|
+ log_i("当前状态:%d ---0-初始化,1-发送,2-接收,3-结束",currentState);
|
|
|
1106
|
+ if (validlen >= 6)
|
|
840
|
1107
|
{
|
|
841
|
|
- delete pbase;
|
|
842
|
|
- pbase = NULL;
|
|
843
|
|
- }
|
|
|
1108
|
+ if(findAddrFlag == 0)
|
|
|
1109
|
+ {
|
|
|
1110
|
+ //寻址成功前,BCU -BAU寻址数据回应处理
|
|
|
1111
|
+ ioResponse(ppacket, pitem, pbuf, len);
|
|
|
1112
|
+ return S_OK;
|
|
|
1113
|
+ }
|
|
|
1114
|
+ else
|
|
|
1115
|
+ {
|
|
|
1116
|
+ //寻址成功,正常解析bms上传数据
|
|
|
1117
|
+ normalDatasResponse(pdevice, ppacket, pitem,pbuf,len);
|
|
|
1118
|
+ }
|
|
844
|
1119
|
|
|
845
|
|
- return S_OK;
|
|
|
1120
|
+ }
|
|
|
1121
|
+ break;
|
|
|
1122
|
+ case WRITE_O:
|
|
|
1123
|
+ break;
|
|
846
|
1124
|
|
|
|
1125
|
+ default:
|
|
|
1126
|
+ break;
|
|
847
|
1127
|
}
|
|
|
1128
|
+ return S_OK;
|
|
|
1129
|
+}
|
|
|
1130
|
+
|
|
|
1131
|
+
|
|
848
|
1132
|
#ifdef __cplusplus
|
|
849
|
1133
|
extern "C" IUnknown* CreateInstance()
|
|
850
|
1134
|
{
|