浏览代码

fixed:寻址成功,读寄存器数据更新频率是3869

develop
qi-qin 1年前
父节点
当前提交
c92548f383
共有 4 个文件被更改,包括 250 次插入124 次删除
  1. 二进制
      Release/libcan_BMSer.so
  2. 二进制
      Release/service/impl/ProtocolImpl.o
  3. 233
    109
      service/impl/ProtocolImpl.cpp
  4. 17
    15
      service/impl/ProtocolImpl.h

二进制
Release/libcan_BMSer.so 查看文件


二进制
Release/service/impl/ProtocolImpl.o 查看文件


+ 233
- 109
service/impl/ProtocolImpl.cpp 查看文件

@@ -33,7 +33,15 @@
33 33
 using namespace std;
34 34
 bool ProtocolImpl::hasInit = false;
35 35
 
36
-
36
+int ProtocolImpl::findAddrFlag = 0;
37
+int ProtocolImpl::idSuccessFlag = 0;
38
+int ProtocolImpl::currentId = 1;
39
+int ProtocolImpl::timeOut = 0;
40
+int ProtocolImpl::count  = 1;
41
+int ProtocolImpl::currentState = 0;
42
+int ProtocolImpl::sendDelayCout = 0;
43
+int ProtocolImpl::flag = 0;
44
+int ProtocolImpl::deviceCounts = 0;
37 45
 uint8_t ProtocolImpl::retBigEndian(uint32_t value, int offset)
38 46
 {
39 47
 	uint8_t ret = 0;
@@ -60,7 +68,7 @@ int ProtocolImpl::getRegAddrMax(Packet *ppacket, Item *pitem)
60 68
 		{
61 69
 		}
62 70
 	}
63
-	log_i("最大的起始地址:0x%x", maxRegAddr);
71
+	//log_i("最大的起始地址:0x%x", maxRegAddr);
64 72
 	return maxRegAddr;
65 73
 }
66 74
 
@@ -71,13 +79,13 @@ int ProtocolImpl::getRegAddrMin(Packet *ppacket, Item *pitem)
71 79
 	std::string sParaRegAddr;
72 80
 	int iParaRegAddr;
73 81
 	std::vector<Item*> items = ppacket->getVitem();
74
-	log_i("Items size: %zu", items.size());
82
+	//log_i("Items size: %zu", items.size());
75 83
 	sMinRegAddr = items[0]->getBase().getParam("起始地址");
76 84
 	iMinRegAddr = strtol(sMinRegAddr.c_str(), NULL, HEX);
77
-	log_i("Initial minimum register address value: 0x%x", iMinRegAddr);
85
+	//log_i("Initial minimum register address value: 0x%x", iMinRegAddr);
78 86
 	for (size_t i = 0; i < items.size(); ++i)
79 87
 	{
80
-		log_i("Processing item %zu", i);
88
+		//log_i("Processing item %zu", i);
81 89
 		sParaRegAddr = items[i]->getBase().getParam("起始地址");
82 90
 		iParaRegAddr = strtol(sParaRegAddr.c_str(), NULL, HEX);
83 91
 		if (iParaRegAddr < iMinRegAddr)
@@ -85,25 +93,56 @@ int ProtocolImpl::getRegAddrMin(Packet *ppacket, Item *pitem)
85 93
 			iMinRegAddr = iParaRegAddr;
86 94
 		}
87 95
 	}
88
-	log_i("最小的寄存器起始地址:0x%x", iMinRegAddr);
96
+	//log_i("最小的寄存器起始地址:0x%x", iMinRegAddr);
89 97
 
90 98
 	return iMinRegAddr;
91 99
 }
92 100
 
93
-int ProtocolImpl::retStateResponse(Packet *ppacket, Item *pitem, const int len)
101
+//过滤出错误帧
102
+int ProtocolImpl::filterErrFrame(PBYTE pbuf,int len)
103
+{
104
+	int validlen = len;
105
+	int frameId ;
106
+	int kindId;
107
+	int errFrameCount = 0;
108
+	for (int count = 0;
109
+				((count < validlen) && ((validlen - count) >= 12));
110
+				count += 12)
111
+	{
112
+		frameId = getRealFrame(pbuf,count);
113
+
114
+		kindId = (frameId >> 24) & 0xffff;
115
+		//log_i("frameId = 0x%x,kindID = 0X%x",frameId,kindId);
116
+		if(kindId != 0xff84 )
117
+		{
118
+			errFrameCount ++;
119
+		}
120
+	}
121
+	return errFrameCount;
122
+}
123
+
124
+int ProtocolImpl::retStateResponse(Packet *ppacket, Item *pitem, const int len,PBYTE pbuf)
94 125
 {
95 126
 	int maxRegAddr = -1;
96 127
 	int minRegAddr = 0;
97 128
 	int regNum = 0;
98 129
 	int datasLen = 0;
130
+	int errFrameCount = 0;
131
+	int errFrameLen = 0;
99 132
 	maxRegAddr = getRegAddrMax(ppacket, pitem);
100 133
 	minRegAddr = getRegAddrMin(ppacket, pitem);
101 134
 	regNum = maxRegAddr - minRegAddr + 1;
102
-	log_i("reg:%d", regNum);
135
+	//log_i("reg:%d", regNum);
136
+	errFrameCount = filterErrFrame(pbuf,len);
137
+	errFrameLen = errFrameCount * 10;
103 138
 	datasLen = getRealValidLen(regNum);
104
-	log_i("最大寄存器地址:0x%x,最小寄存器数量:0x%x,寄存器个数:0x%x,数据长度:%d,合法的数据长度=%d", maxRegAddr,
105
-			minRegAddr, regNum, datasLen, len);
106
-	if (len >= datasLen)
139
+	//log_i("datasLen:%d",datasLen);
140
+
141
+	log_i("最大寄存器地址:0x%x,最小寄存器数量:0x%x,寄存器个数:0x%x,理论算出应收到合法数据长度:%d,\
142
+			实际收到数据长度=%d,错误帧个数=%d,错误帧长度:%d", maxRegAddr,
143
+			minRegAddr, regNum, datasLen, len,errFrameCount,errFrameLen);
144
+
145
+	if (len >= (datasLen + errFrameLen))
107 146
 	{
108 147
 		return S_OK;
109 148
 	}
@@ -121,19 +160,19 @@ int ProtocolImpl::getRealValidLen(int maxRegNum)
121 160
 	if (maxRegNum <= 4 && maxRegNum > 0)
122 161
 	{
123 162
 		len = 4 + 2 * maxRegNum;
124
-		log_i("寄存器数量小于等于4个,计算得出合法数据长度:%d", len);
163
+		log_i("第1种情况,寄存器数量小于等于4个,计算得出合法数据长度:%d", len);
125 164
 	}
126 165
 	else
127 166
 	{
128 167
 		if (maxRegNum % 4 == 0)
129 168
 		{
130 169
 			len = 12 * (maxRegNum / 4);
131
-			log_i("寄存器请求数量:%d,回复帧长度:%d",maxRegNum,len);
170
+			log_i("第2种情况,寄存器数量多余4且%4 == 0情况,寄存器请求数量:%d,回复帧长度:%d",maxRegNum,len);
132 171
 		}
133 172
 		else
134 173
 		{
135 174
 			len = 4*((maxRegNum / 4) +1) + (maxRegNum *2);   //帧数据长度+寄存器长度
136
-			log_i("寄存器请求数量:%d,回复帧长度:%d",maxRegNum,len);
175
+			log_i("第3种情况,寄存器数量多余4,寄存器请求数量:%d,回复帧长度:%d",maxRegNum,len);
137 176
 		}
138 177
 	}
139 178
 	return len;
@@ -146,7 +185,7 @@ int ProtocolImpl::getRealFrame(PBYTE pbuf, int count)
146 185
 			| ((ptemp[1 + count] << 16) & 0x00ff0000)
147 186
 			| ((ptemp[2 + count] << 8) & 0x0000ff00)
148 187
 			| ((ptemp[3 + count]) & 0x000000ff));
149
-	log_i("帧id:0x%x", id);
188
+	//log_i("帧id:0x%x", id);
150 189
 
151 190
 	return id;
152 191
 
@@ -191,39 +230,38 @@ std::map<int, int> ProtocolImpl::handleDataLess(PBYTE pbuf,int len,int regNum,in
191 230
 
192 231
 std::map<int,int> ProtocolImpl::handleDataGreater(PBYTE pbuf,int len,int regNum,int datasLen)
193 232
 {
194
-	int validlen = len;
195
-	int iPbufId;
233
+	log_i("handleDataGreater:收到实际数据长度:%d,请求采集个数:%d,理想情况下收到数据长度:%d", len, regNum, datasLen);
196 234
 	std::map<int, int> dataSet;
197 235
 
198
-	for (int count = 0;
199
-			((count < validlen) && ((validlen - count) >= 12));
200
-			count += 12)
236
+	for (int count = 0; (count < len) && (len - count >= 12); count += 12)
201 237
 	{
202
-		iPbufId = getRealFrame(pbuf, count);
203
-		for (int i = 0; i < 4 && regNum > 0; i++,regNum --)   // i代表第i个寄存器
238
+		int iPbufId = getRealFrame(pbuf, count); // 假设正确解析起始寄存器ID
239
+		int kindId = (iPbufId >> 24) & 0xffff;
240
+		//log_i("frameId = 0x%x,kindID = 0X%x",frameId,kindId);
241
+		if(kindId == 0xff84 )    //代表合法数据
204 242
 		{
205
-
206
-			uint16_t regValue = 0;
207
-			if (i * 2 + 4 + count <= datasLen)  //当前寄存器数量的总帧数
243
+			for (int i = 0; i < 4 && regNum >= 0; ++i, --regNum)
208 244
 			{
209
-				regValue = (pbuf[i * 2 + 4 + count] << 8)
210
-						| pbuf[i * 2 + 5 + count]; // 提取寄存器值
211
-				dataSet[iPbufId + i % 4] = regValue; // 绑定到map
212
-			}
213
-			else
214
-			{
215
-				// 数据不足,处理错误或默认值
216
-				regValue = 0xFFFF; // 默认值为0xFFFF
217
-				dataSet[iPbufId + i % 4] = regValue; // 绑定到map
245
+				uint16_t regValue;
246
+				int dataOffset = count + 4 + i * 2;
247
+
248
+				// 确保当前块的数据足够(两个字节)
249
+				if (dataOffset + 1 < len)
250
+				{
251
+					regValue = (pbuf[dataOffset] << 8) | pbuf[dataOffset + 1];
252
+				}
253
+				else
254
+				{
255
+					regValue = 0xFFFF; // 数据不足,默认值
256
+				}
257
+
258
+				dataSet[iPbufId + i] = regValue; // 寄存器ID连续递增
218 259
 			}
219 260
 		}
220
-		for (const auto &entry : dataSet)
221
-		{
222
-			log_i("addrFrame = 0x%x, value = 0x%x", entry.first,
223
-					entry.second);
224
-		}
261
+		// 每个块的数据部分为8字节(从count+4到count+11)
225 262
 
226 263
 	}
264
+
227 265
 	return dataSet;
228 266
 
229 267
 }
@@ -266,7 +304,7 @@ std::map<int,int>ProtocolImpl::retDataSet(Packet *ppacket, Item *pitem,PBYTE pbu
266 304
 
267 305
 	regNum = maxRegAddr - minRegAddr + 1;
268 306
 	datasLen = getRealValidLen(regNum);
269
-	log_i("regNum:%d,dataLen:%d",regNum,datasLen);
307
+
270 308
 	if (datasLen <= 12)
271 309
 	{
272 310
 		dataSet = handleDataLess(pbuf,len,regNum,datasLen);
@@ -286,10 +324,10 @@ int ProtocolImpl::iGetItemParaConfig(Item *pitem,std::string attribute)
286 324
 	int iAttribute = 0;
287 325
 
288 326
 	sAttribute = t_base.getParam(attribute);
289
-	log_i("%s: %s",attribute.c_str(), sAttribute.c_str());
327
+	//log_i("%s: %s",attribute.c_str(), sAttribute.c_str());
290 328
 
291 329
 	iAttribute = strtol(sAttribute.c_str(), NULL, HEX);
292
-	log_i("retV:0x%x",iAttribute);
330
+	//log_i("retV:0x%x",iAttribute);
293 331
 	return iAttribute;
294 332
 }
295 333
 
@@ -299,7 +337,7 @@ std::string  ProtocolImpl::sGetItemParaConfig(Item *pitem, std::string attribute
299 337
 	Base &t_base = t_item->getBase();
300 338
 	string sAttribute = "";
301 339
 	sAttribute = t_base.getParam(attribute);
302
-	log_i("%s: %s",attribute.c_str(), sAttribute.c_str());
340
+	//log_i("%s: %s",attribute.c_str(), sAttribute.c_str());
303 341
 	return sAttribute;
304 342
 }
305 343
 
@@ -309,29 +347,58 @@ void ProtocolImpl::setData(Item *pitem,int16_t pbuf,int receiveData,int paraData
309 347
 	int16_t buf = pbuf;
310 348
 	string t_str;
311 349
 	char t[256];
312
-	log_i("receiveData:0x%x,paraData:0x%x",receiveData,paraData);
350
+	//log_i("receiveData:0x%x,paraData:0x%x",receiveData,paraData);
313 351
 	if(receiveData == paraData)
314 352
 	{
315
-		log_i("ID匹配,处理数据");
353
+		//log_i("ID匹配,处理数据");
316 354
 		if (t_sDataType == "I")
317 355
 		{
318
-			log_i("数据类型为I,调用merge16函数,buf=0x%x",buf);
356
+			//log_i("数据类型为I,调用merge16函数,buf=0x%x",buf);
319 357
 			t_item->setValue(merge16(buf, t_sByteOrder));
358
+//			if(t_sByteOrder == "B")
359
+//			{
360
+//				log_i("字符类型B");
361
+//				int index = t_sBits.find(".");
362
+//				log_i("t_sBits =%s, index = %d",t_sBits.c_str(),index);
363
+//				if (index != -1)
364
+//				{
365
+//					string t_sBitnum = t_sBits.substr(0,index);
366
+//					int t_iBitnum =(int) (strtol(t_sBitnum.c_str(),NULL, 10));	//----位起始地址
367
+//					int t_Bits =(int) (strtol(t_sBits.substr(index + 1).c_str(),NULL, 10));
368
+//					unsigned short t_usdata =(unsigned short) (strtol(merge16_u(buf,t_sByteOrder).c_str(),NULL, 10));
369
+//					unsigned short bits = 0;
370
+//					log_i("位起始地址t_iBitnum = %d, t_Bits= %d, t_usdata = %d",t_iBitnum,t_Bits,t_usdata);
371
+//					for (int i = 0; i < t_Bits; i++)
372
+//					{
373
+//						unsigned short bit = (t_usdata >> (t_iBitnum + i))& 0x0001;
374
+//						bits += bit * pow(2, i);
375
+//					}
376
+//					snprintf(t, 256, "%d", bits);
377
+//					log_i("bits:%d",bits);
378
+//					t_str = t;
379
+//					log_i("字符类型设置:%s",t_str.c_str());
380
+//					t_item->setValue(t_str);
381
+//				}
382
+//			}
320 383
 		}
321 384
 		else if (t_sDataType == "UI")
322 385
 		{
323
-			log_i("数据类型为UI,调用merge16函数");
386
+			//log_i("数据类型为UI,调用merge16函数");
324 387
 			t_item->setValue(merge16(buf, t_sByteOrder));
325 388
 		}
326 389
 		else if (t_sDataType == "B")
327 390
 		{
391
+
328 392
 			int index = t_sBits.find(".");
393
+			//log_i("t_sBits =%s, index = %d",t_sBits.c_str(),index);
329 394
 			if (index != -1)
330 395
 			{
331 396
 				string t_sBitnum = t_sBits.substr(0,index);
332 397
 				int t_iBitnum =(int) (strtol(t_sBitnum.c_str(),NULL, 10));	//----位起始地址
333 398
 				int t_Bits =(int) (strtol(t_sBits.substr(index + 1).c_str(),NULL, 10));
399
+				//log_i("数据类型为B,调用merge16函数");
334 400
 				unsigned short t_usdata =(unsigned short) (strtol(merge16_u(buf,t_sByteOrder).c_str(),NULL, 10));
401
+				//log_i("位起始地址t_iBitnum = %d, t_Bits= %d, t_usdata = %d",t_iBitnum,t_Bits,t_usdata);
335 402
 				unsigned short bits = 0;
336 403
 				for (int i = 0; i < t_Bits; i++)
337 404
 				{
@@ -341,6 +408,7 @@ void ProtocolImpl::setData(Item *pitem,int16_t pbuf,int receiveData,int paraData
341 408
 				snprintf(t, 256, "%d", bits);
342 409
 				t_str = t;
343 410
 				t_item->setValue(t_str);
411
+				//log_i("字符类型设置:%s",t_str.c_str());
344 412
 			}
345 413
 		}
346 414
 	}
@@ -416,7 +484,7 @@ void ProtocolImpl::proceessReplyFrame(int frame,int datas)
416 484
 	  		idSuccessFlag = 0;
417 485
 	        log_i("收到未知的CAN帧");
418 486
 	    }
419
-	  	if(idSuccessFlag == 2)
487
+	  	if(idSuccessFlag == deviceCounts)
420 488
 	  	{
421 489
 	  		 findAddrFlag = 1;
422 490
 	  		 currentId = 1;
@@ -434,7 +502,7 @@ void  ProtocolImpl::sendAllocFrame(PBYTE pbuf, int id, int &len)
434 502
 	pbuf[2] = 0x07;
435 503
 	pbuf[3] = 0xf0;
436 504
 	pbuf[4] = static_cast<uint8_t>(id); // 转换为字节
437
-	pbuf[5] = static_cast<uint8_t>(maxId); // 转换为字节
505
+	pbuf[5] = static_cast<uint8_t>(deviceCounts); // 转换为字节
438 506
 	pbuf[6] = static_cast<uint8_t>(orderMode); // 转换为字节
439 507
 	pbuf[7] = static_cast<uint8_t>(orderState); // 转换为字节
440 508
 //	pbuf[8] = 0;
@@ -544,7 +612,7 @@ HRESULT ProtocolImpl::onRead(Device *pdevice, Packet *ppacket, Item *pitem,
544 612
 {
545 613
 
546 614
 	log_i("onRead");
547
-	int deviceCounts;
615
+
548 616
 	Channel *pC = pdevice->getParent();
549 617
 
550 618
 	log_i("Channel pointer: %p", pC);
@@ -556,8 +624,8 @@ HRESULT ProtocolImpl::onRead(Device *pdevice, Packet *ppacket, Item *pitem,
556 624
 	if (m_tmpItems.count(pC->getBase().getObjid().toString()) == 0)
557 625
 	{
558 626
 		deviceCounts = pC->getDeviceCount();
559
-		//maxId = deviceCounts;
560
-		maxId = 2;
627
+		maxId = deviceCounts;
628
+		//maxId = 2;
561 629
 
562 630
 		log_i("maxid =%d",maxId);
563 631
 	}
@@ -565,12 +633,28 @@ HRESULT ProtocolImpl::onRead(Device *pdevice, Packet *ppacket, Item *pitem,
565 633
 	if(currentState == INIT)
566 634
 	{
567 635
 		initIO();
568
-		sendDelay = 0;
569 636
 	}
570 637
 
571 638
 	if(currentState == SEND)
572 639
 	{
573
-		sendAllocFrame(pbuf,currentId , len);
640
+		log_i("currenntId = %d",currentId);
641
+		if(currentId != 1)
642
+		{
643
+			log_i("发送延迟计数:%d",sendDelayCout);
644
+			if(sendDelayCout > 3)
645
+			{
646
+
647
+				sendAllocFrame(pbuf,currentId , len);
648
+				//sendDelayCout = 0;
649
+
650
+
651
+			}
652
+			sendDelayCout ++;
653
+		}
654
+		else
655
+		{
656
+			sendAllocFrame(pbuf,currentId , len);
657
+		}
574 658
 		timeOut = 0;
575 659
 		//pdevice->getBase().setRwstate(READ_WAIT);
576 660
 		pdevice->getBase().setRwstate(WRITE_WAIT);
@@ -601,37 +685,33 @@ HRESULT ProtocolImpl::onRead(Device *pdevice, Packet *ppacket, Item *pitem,
601 685
 			return S_FALSE;
602 686
 		}
603 687
 
604
-		pdevice->getBase().setRwstate(READ_WAIT);	//----设置设备状态
688
+		//pdevice->getBase().setRwstate(READ_WAIT);	//----设置设备状态
605 689
 
606 690
 		std::string sParaId;
607 691
 		int iParaId;
608 692
 		Base &t_base = ppacket->getVitem().at(0)->getBase();
609 693
 		sParaId = t_base.getParam("功能码");
610
-		log_i("Parameter ID: %s", sParaId.c_str());
694
+		log_i("sParaId =%s,对应的实际名称=%s",sParaId.c_str() ,t_base.getName().c_str());
611 695
 		iParaId = strtol(sParaId.c_str(), NULL, HEX);
612
-		log_i("Parameter ID: %s", sParaId.c_str());
613
-		log_i("Converted Parameter ID: 0x%x", iParaId);
696
+		string t_saddr;
697
+		t_saddr = pdevice->getBase().getParam("站地址");
698
+		int t_iaddr = 0;
699
+		t_iaddr = strtol(t_saddr.c_str(), NULL, HEX);
614 700
 		int frameFuncCode  = (iParaId & 0x070000);
615
-		log_i("frameFuncCode = 0x%x",frameFuncCode);
701
+		log_i("stationAddr = %d,iParaId=0x%x,frameFuncCode = 0x%x",t_iaddr,iParaId,frameFuncCode);
616 702
 		if (ppacket == nullptr || pitem == nullptr)
617 703
 		{
618 704
 			log_i("Invalid packet or item");
619 705
 			return S_FALSE;
620 706
 		}
621
-		string t_saddr;
622
-		t_saddr = pdevice->getBase().getParam("站地址");
623
-		int t_iaddr = 0;
624
-		t_iaddr = strtol(t_saddr.c_str(), NULL, HEX);
625
-		log_i("stationAddr = %d",t_iaddr);
626
-		iParaId |=  (t_iaddr << 19);
627
-		log_i("onread -addStation-id = 0x%x",iParaId);
628 707
 
708
+		//iParaId |=  (t_iaddr << 19);
629 709
 		//判断寄存器是读寄存器
630 710
 		if(frameFuncCode == 0x030000)
631 711
 		{
632 712
 			int maxRegAddr = getRegAddrMax(ppacket, pitem);
633 713
 			int minRegAddr = getRegAddrMin(ppacket, pitem);
634
-			log_i("MaxRegAddr: 0x%x, MinRegAddr: 0x%x", maxRegAddr, minRegAddr);
714
+			log_i("onRead中判断出寄存器是读寄存器MaxRegAddr: 0x%x, MinRegAddr: 0x%x", maxRegAddr, minRegAddr);
635 715
 
636 716
 			if (maxRegAddr < minRegAddr)
637 717
 			{
@@ -639,7 +719,7 @@ HRESULT ProtocolImpl::onRead(Device *pdevice, Packet *ppacket, Item *pitem,
639 719
 				return S_FALSE;
640 720
 			}
641 721
 			int regNum = maxRegAddr - minRegAddr + 1;
642
-
722
+			iParaId |=  (t_iaddr << 19);
643 723
 			pbuf[0] = retBigEndian(iParaId + minRegAddr, 24);
644 724
 			pbuf[1] = retBigEndian(iParaId + minRegAddr, 16);
645 725
 			pbuf[2] = retBigEndian(iParaId + minRegAddr, 8);
@@ -653,10 +733,16 @@ HRESULT ProtocolImpl::onRead(Device *pdevice, Packet *ppacket, Item *pitem,
653 733
 				log_i("pbuf is null");
654 734
 				return S_FALSE;
655 735
 			}
656
-			for (int i = 0; i < 6; i++)
736
+			for (int i = 0; i < len; i++)
657 737
 			{
658 738
 				log_i("onRead-读数据:pbuf[%d]:0x%x", i, pbuf[i]);
659 739
 			}
740
+			pdevice->getBase().setRwstate(READ_WAIT);	//----设置设备状态
741
+		}
742
+		//判断寄存器是读写寄存器
743
+		if(frameFuncCode == 0x010000)
744
+		{
745
+			pdevice->getBase().setRwstate(WRITE_WAIT);
660 746
 		}
661 747
 
662 748
 		//初始化数据库容器
@@ -664,7 +750,7 @@ HRESULT ProtocolImpl::onRead(Device *pdevice, Packet *ppacket, Item *pitem,
664 750
 		{
665 751
 			if (m_tmpItems.count(pC->getBase().getObjid().toString()) == 0)
666 752
 			{
667
-				log_i("Temporary items not found for Channel ID: %s",
753
+				log_i("初始化ZLink数据Temporary items not found for Channel ID: %s",
668 754
 						pC->getBase().getObjid().toString().c_str());
669 755
 				int ccount = pC->getDeviceCount();
670 756
 				log_i("Channel device count: %d", ccount);
@@ -679,13 +765,13 @@ HRESULT ProtocolImpl::onRead(Device *pdevice, Packet *ppacket, Item *pitem,
679 765
 						Base &t_base =
680 766
 								pC->getVDevice().at(i)->getVitem().at(j)->getBase();
681 767
 						t_sId = t_base.getParam("写功能码");
682
-						log_i("写功能码: %s", t_sId.c_str());
768
+						//log_i("写功能码: %s", t_sId.c_str());
683 769
 						int t_iId = 0;
684 770
 						t_iId = strtol(t_sId.c_str(), NULL, HEX);	//----获取帧ID
685
-						log_i("功能码转换为整数: 0x%x", t_iId);
771
+						//log_i("功能码转换为整数: 0x%x", t_iId);
686 772
 						string t_stype;
687 773
 						t_stype = t_base.getParam("帧类型");
688
-						log_i("帧类型: %s", t_stype.c_str());
774
+						//log_i("帧类型: %s", t_stype.c_str());
689 775
 
690 776
 						if (t_stype == "1")
691 777
 						{
@@ -696,21 +782,23 @@ HRESULT ProtocolImpl::onRead(Device *pdevice, Packet *ppacket, Item *pitem,
696 782
 						string sStartAddr;
697 783
 						//sStartAddr = pC->getVDevice().at(i)->getBase().getParam("起始地址");
698 784
 						sStartAddr = t_base.getParam("起始地址");
699
-						log_i("起始地址 =%s", sStartAddr.c_str());
785
+						//log_i("起始地址 =%s", sStartAddr.c_str());
700 786
 
701 787
 						int iStartAddr = 0;
702 788
 						iStartAddr = strtol(sStartAddr.c_str(), NULL, HEX);
703
-						log_i("起始地址转换为整数: 0x%x", iStartAddr);
789
+						//log_i("起始地址转换为整数: 0x%x", iStartAddr);
704 790
 						t_iId += iStartAddr;
705 791
 						string t_saddr;
706 792
 						t_saddr = pC->getVDevice().at(i)->getBase().getParam("站地址");
707 793
 						int t_iaddr = 0;
708 794
 						t_iaddr = strtol(t_saddr.c_str(), NULL, HEX);
795
+						//log_i("站地址:%d",t_iaddr);
709 796
 						t_iId |=  (t_iaddr << 19);
797
+						//log_i("t_Id = %d",t_iId);
710 798
 						tmpItems.insert(
711 799
 								make_pair(t_iId,
712 800
 										pC->getVDevice().at(i)->getVitem().at(j)));
713
-						log_i("插入临时条目: ID=0x%x", t_iId);
801
+						//log_i("插入临时条目: ID=0x%x", t_iId);
714 802
 					}
715 803
 				}
716 804
 
@@ -789,12 +877,7 @@ HRESULT ProtocolImpl::onWrite(Device *pdevice, Packet *ppacket, Item *pitem,
789 877
 
790 878
 		*(pbuf + (t_len++)) = t_data.data0 & 0x00ff;
791 879
 		*(pbuf + (t_len++)) = t_data.data1 & 0x00ff;
792
-//		*(pbuf + (t_len++)) = t_data.data2 & 0x00ff;
793
-//		*(pbuf + (t_len++)) = t_data.data3 & 0x00ff;
794
-//		*(pbuf + (t_len++)) = t_data.data4 & 0x00ff;
795
-//		*(pbuf + (t_len++)) = t_data.data5 & 0x00ff;
796
-//		*(pbuf + (t_len++)) = t_data.data6 & 0x00ff;
797
-//		*(pbuf + (t_len++)) = t_data.data7 & 0x00ff;
880
+
798 881
 
799 882
 		string t_sDataType;	//----数据类型
800 883
 		t_sDataType = base.getParam("数据类型");
@@ -876,8 +959,9 @@ HRESULT ProtocolImpl::isResponseOK(Device *pdevice, Packet *ppacket,
876 959
 	{
877 960
 	case READ_O:
878 961
 	case WRITE_WAIT:
962
+		return S_OK;
879 963
 	case READ_WAIT:
880
-		ret = retStateResponse(ppacket, pitem, len);
964
+		ret = retStateResponse(ppacket, pitem, len,pbuf);
881 965
 		log_i("isResponseOK返回值:%d", ret);
882 966
 		if (ret == S_OK)
883 967
 		{
@@ -904,17 +988,21 @@ HRESULT ProtocolImpl::isResponseOK(Device *pdevice, Packet *ppacket,
904 988
 //寻址未成功,解析寻址报文
905 989
 void ProtocolImpl::ioResponse(Packet *ppacket, Item *pitem,PBYTE pbuf, const int len)
906 990
 {
991
+	// jisuan
907 992
 	std::map<int, int> dataSets; // 存储寄存器地址和值
908
-	int id;
909
-	int16_t buf;
910 993
 	dataSets=retIoDataSet(ppacket, pitem, pbuf, len);
911 994
 	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]);
912 995
 
996
+	// second,
913 997
 	for (const auto &entry : dataSets)
914 998
 	{
915 999
 		log_i("addrFrame = 0x%x, value = 0x%x", entry.first,
916 1000
 				entry.second);
917 1001
 	}
1002
+
1003
+	int id;
1004
+	int16_t buf;
1005
+
918 1006
 	for (const auto &dataPair : dataSets)
919 1007
 	{
920 1008
 		log_i("addData.size=%d", dataSets.size());
@@ -932,6 +1020,44 @@ void ProtocolImpl::ioResponse(Packet *ppacket, Item *pitem,PBYTE pbuf, const int
932 1020
 
933 1021
 }
934 1022
 
1023
+void ProtocolImpl::stringOutput(PBYTE pbuf, int data_len)
1024
+{
1025
+	// 删除strlen计算,直接使用传入的data_len
1026
+	    int groups = data_len / 12;
1027
+	    int remainder = data_len % 12;
1028
+	    int total_bytes = (groups + (remainder != 0)) * 12;
1029
+
1030
+	    char *result = (char *)malloc(total_bytes);
1031
+	    if (!result) {
1032
+	        fprintf(stderr, "内存分配失败\n");
1033
+	        return;
1034
+	    }
1035
+	    memset(result, 0, total_bytes);  // 更规范的写法
1036
+
1037
+	    int pos = 0;
1038
+	    for (int i = 0; i < data_len; i += 12) {
1039
+	        int bytes_to_copy = std::min(12, data_len - i);
1040
+	        memcpy(result + pos, pbuf + i, bytes_to_copy);
1041
+	        pos += 12;
1042
+	    }
1043
+
1044
+	    // 以HEX格式打印二进制数据
1045
+	    for (int i = 0; i < total_bytes; i += 12) {
1046
+	        std::string hexStr;
1047
+	        for (int j = 0; j < 12; ++j) {
1048
+	            char buf[3];
1049
+	            snprintf(buf, sizeof(buf), "%02X", static_cast<unsigned char>(result[i + j]));
1050
+	            hexStr += buf;
1051
+	            hexStr += " ";  // 可选:增加空格提高可读性
1052
+	        }
1053
+	        log_i("接收到的数据展示:%s", hexStr.c_str());
1054
+	    }
1055
+
1056
+	    free(result);
1057
+
1058
+
1059
+}
1060
+
935 1061
 //寻址成功,解析BMS传的数据
936 1062
 int ProtocolImpl::normalDatasResponse(Device* pdevice,Packet* ppacket,Item* pitem,PBYTE pbuf,const int len)
937 1063
 {
@@ -945,52 +1071,54 @@ int ProtocolImpl::normalDatasResponse(Device* pdevice,Packet* ppacket,Item* pite
945 1071
 		log_e("%s, 父通道为空, 无法进行数据解析", pdevice->getBase().getName().c_str());
946 1072
 		return S_FALSE;
947 1073
 	}
1074
+	//stringOutput(pbuf,len);
948 1075
 	dataSet = retDataSet(ppacket, pitem,pbuf,len);
949
-	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]);
950 1076
 
951
-	for (const auto &entry : dataSet)
952
-	{
953
-		log_i("addrFrame = 0x%x, value = 0x%x", entry.first,
954
-				entry.second);
955
-	}
1077
+	//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]);
1078
+
1079
+//	for (const auto &entry : dataSet)
1080
+//	{
1081
+//		log_i("dataSet填充返回值:addrFrame = 0x%x, value = 0x%x", entry.first,
1082
+//				entry.second);
1083
+//	}
956 1084
 	for (const auto &dataPair : dataSet)
957 1085
 	{
958
-		log_i("addData.size=%d", dataSet.size());
1086
+		//log_i("addData.size=%d", dataSet.size());
959 1087
 		id = dataPair.first;
960 1088
 
961 1089
 		id = (0x7fffffff) & id;
962 1090
 		buf = dataPair.second;
963 1091
 
964
-		log_i("Processing data pair with ID: 0x%x", id);
1092
+		//log_i("Processing data pair with ID: 0x%x", id);
965 1093
 
966 1094
 		if (m_tmpItems.count(pC->getBase().getObjid().toString()) > 0)
967 1095
 		{
968 1096
 
969
-			log_i("Found temporary items for Channel ID: %s",
970
-					pC->getBase().getObjid().toString().c_str());
1097
+			//log_i("Found temporary items for Channel ID: %s",
1098
+					//pC->getBase().getObjid().toString().c_str());
971 1099
 			std::multimap<int, Item*> tmpItms = m_tmpItems.find(
972 1100
 					pC->getBase().getObjid().toString())->second; //是否有效
973
-			for (const auto &data : tmpItms)
974
-			{
975
-				log_i("缓存条目中的缓存地址:0x%x,0x%x", data.first, data.second);
976
-			}
977
-			log_i("Temporary items size: %zu", tmpItms.size());
1101
+//			for (const auto &data : tmpItms)
1102
+//			{
1103
+//				log_i("缓存条目中的缓存地址:0x%x,0x%x", data.first, data.second);
1104
+//			}
1105
+			//log_i("Temporary items size: %zu", tmpItms.size());
978 1106
 
979 1107
 			std::multimap<int, Item*>::size_type cnt = tmpItms.count(
980 1108
 					id);
981
-			log_i("Number of items with ID 0x%x: %zu", id, cnt);
1109
+			//log_i("Number of items with ID 0x%x: %zu", id, cnt);
982 1110
 			std::multimap<int, Item*>::iterator iter = tmpItms.find(id);
983 1111
 			if (iter != tmpItms.end())
984 1112
 			{
985 1113
 				for (; cnt > 0; cnt--, iter++)
986 1114
 				{
987
-					log_i("Processing item %zu for ID 0x%x", cnt, id);
1115
+					//log_i("Processing item %zu for ID 0x%x", cnt, id);
988 1116
 					Item *t_item = iter->second;
989 1117
 					//Device *ptdevice = (Device*) (t_item->getParent());
990 1118
 
991 1119
 					if (t_item != NULL && pdevice != NULL)
992 1120
 					{
993
-						log_i("Item and Device are valid");
1121
+
994 1122
 						int iId = iGetItemParaConfig(t_item,"写功能码");
995 1123
 						int iStartAddr = iGetItemParaConfig(t_item,"起始地址");
996 1124
 						string t_stype = sGetItemParaConfig(t_item,"帧类型");
@@ -1002,11 +1130,11 @@ int ProtocolImpl::normalDatasResponse(Device* pdevice,Packet* ppacket,Item* pite
1002 1130
 						t_saddr = pdevice->getBase().getParam("站地址");
1003 1131
 						int t_iaddr = 0;
1004 1132
 						t_iaddr = strtol(t_saddr.c_str(), NULL, HEX);
1005
-						log_i("t_iId = 0x%x,t_iaddr =%d,",iId,t_iaddr);
1133
+						//log_i("t_iId = 0x%x,t_iaddr =%d,",iId,t_iaddr);
1006 1134
 
1007 1135
 						iId |=  (t_iaddr << 19);
1008 1136
 
1009
-						log_i("设置的数据buf=%d,paraID = 0x%x",buf,iId);
1137
+						//log_i("设置的数据buf=%d,paraID = 0x%x",buf,iId);
1010 1138
 						setData(t_item, buf,id,iId,t_sDataType,t_sByteOrder,t_sBits);
1011 1139
 					}
1012 1140
 
@@ -1030,10 +1158,6 @@ int ProtocolImpl::normalDatasResponse(Device* pdevice,Packet* ppacket,Item* pite
1030 1158
 	return S_OK;
1031 1159
 }
1032 1160
 
1033
-
1034
-
1035
-
1036
-
1037 1161
 /**
1038 1162
  * @content:解析数据
1039 1163
  * @time:2016-9-9

+ 17
- 15
service/impl/ProtocolImpl.h 查看文件

@@ -119,18 +119,13 @@ private:
119 119
 //	std::map<std::string, std::multimap<int, Packet*> > m_tmppackets;
120 120
 	std::map<std::string, std::multimap<int, Item*> > m_tmpItems;
121 121
 	const ULONG VERSION;
122
-	int findAddrFlag = 0;
123
-	int idSuccessFlag = 0;
124
-	int sendCurrentId = 0;
125
-	int currentId = 1; //分配ID编号
122
+	static int findAddrFlag ;
123
+	static int idSuccessFlag ;
124
+	static int currentId ; //分配ID编号
126 125
 	int maxId = 0;    //最大分配ID
127 126
 	int orderMode = 0; //分配模式
128 127
 	int orderState  = 0; //分配状态
129
-
130
-	int firstResponSuccessFlag = 0;
131
-	int timeOut = 0;
132
-
133
-
128
+	static int timeOut ;
134 129
 	std::string devPath = "/sys/class/gpio/gpio436/value"; //输入输出电平值设备
135 130
 	std::string exportPath = "/sys/class/gpio/export";    //GPIO设备导出设备
136 131
 	std::string directPath = "/sys/class/gpio/gpio436/direction"; //GPIO输入输出控制设备
@@ -139,10 +134,13 @@ private:
139 134
 	std::string gpio = "436";
140 135
 	std::string highLevel = "1";
141 136
 	std::string lowLevel  = "0";
142
-	int count  = 0;
137
+	static int count ;
138
+	static int currentState ;
139
+	int initIOFlag = 0;
140
+	static int sendDelayCout ;
141
+	static int flag ;
142
+	static int deviceCounts;
143 143
 
144
-	int currentState = 0;
145
-	int sendDelay = 0;
146 144
 
147 145
 
148 146
 
@@ -157,7 +155,7 @@ private:
157 155
 	unsigned short GetCRC(PBYTE pchMsg, unsigned short usDataLen);
158 156
 
159 157
 	uint8_t retBigEndian(uint32_t value, int offset);
160
-	int retStateResponse(Packet* ppacket,Item* pitem,const int len);
158
+	int retStateResponse(Packet* ppacket,Item* pitem,const int len,PBYTE pbuf);
161 159
 	int getRealFrame (PBYTE pbuf,int count);
162 160
 	int getRegAddrMax(Packet* ppacket,Item* pitem);
163 161
 	int getRegAddrMin(Packet* ppacket,Item* pitem);
@@ -185,6 +183,8 @@ private:
185 183
 
186 184
 	std::map<int,int> retIoDataSet(Packet *ppacket, Item *pitem,PBYTE pbuf,int len);
187 185
 	std::map<int,int> handleIoData(PBYTE pbuf);
186
+	int filterErrFrame(PBYTE pbuf,int len);
187
+    void stringOutput(PBYTE pbuf,int data_len);
188 188
 
189 189
 	std::vector<uint8_t> intToBytes(int value);
190 190
 	inline std::string merge8(PBYTE pbuf, std::string byteorder)
@@ -231,7 +231,7 @@ private:
231 231
 			snprintf(t, 256, "%d", t_sdata);
232 232
 
233 233
 			t_str = t;
234
-			log_i("BA-merge16 t_str= %s", t_str.c_str());
234
+			//log_i("BA-merge16 t_str= %s", t_str.c_str());
235 235
 }
236 236
 		else if (byteorder == "AB")
237 237
 		{
@@ -245,7 +245,7 @@ private:
245 245
 			snprintf(t, 256, "%d", t_sdata);
246 246
 
247 247
 			t_str = t;
248
-			log_i("AB-merge16 t_str= %s", t_str.c_str());
248
+			//log_i("AB-merge16 t_str= %s", t_str.c_str());
249 249
 		}
250 250
 
251 251
 		return t_str;
@@ -254,6 +254,7 @@ private:
254 254
 	//inline std::string merge16_u(PBYTE pbuf, std::string byteorder)
255 255
 	inline std::string merge16_u(int16_t pbuf, std::string byteorder)
256 256
 	{
257
+		//log_i("merge16_u pbuf:%d,byteorder = %s",pbuf,byteorder.c_str());
257 258
 		unsigned short t_sdata = 0;
258 259
 		char t[256];
259 260
 		std::string t_str;
@@ -271,6 +272,7 @@ private:
271 272
 			snprintf(t, 256, "%d", t_sdata);
272 273
 			t_str = t;
273 274
 		}
275
+		//log_i("merge16_u ret = %s",t_str.c_str());
274 276
 		return t_str;
275 277
 	}
276 278
 

正在加载...
取消
保存