Parcourir la source

fixed:RW功能修改

develop
qi-qin il y a 1 an
Parent
révision
2b71030f8f
3 fichiers modifiés avec 129 ajouts et 96 suppressions
  1. BIN
      Release/libcan_BMSer.so
  2. BIN
      Release/service/impl/ProtocolImpl.o
  3. 129
    96
      service/impl/ProtocolImpl.cpp

BIN
Release/libcan_BMSer.so Voir le fichier


BIN
Release/service/impl/ProtocolImpl.o Voir le fichier


+ 129
- 96
service/impl/ProtocolImpl.cpp Voir le fichier

@@ -231,6 +231,7 @@ std::map<int, int> ProtocolImpl::handleDataLess(PBYTE pbuf,int len,int regNum,in
231 231
 std::map<int,int> ProtocolImpl::handleDataGreater(PBYTE pbuf,int len,int regNum,int datasLen)
232 232
 {
233 233
 	log_i("handleDataGreater:收到实际数据长度:%d,请求采集个数:%d,理想情况下收到数据长度:%d", len, regNum, datasLen);
234
+	log_i("进入handDataGreater时间");
234 235
 	std::map<int, int> dataSet;
235 236
 
236 237
 	for (int count = 0; (count < len) && (len - count >= 12); count += 12)
@@ -261,7 +262,7 @@ std::map<int,int> ProtocolImpl::handleDataGreater(PBYTE pbuf,int len,int regNum,
261 262
 		// 每个块的数据部分为8字节(从count+4到count+11)
262 263
 
263 264
 	}
264
-
265
+	log_i("出去handDataGreater时间");
265 266
 	return dataSet;
266 267
 
267 268
 }
@@ -343,48 +344,26 @@ std::string  ProtocolImpl::sGetItemParaConfig(Item *pitem, std::string attribute
343 344
 
344 345
 void ProtocolImpl::setData(Item *pitem,int16_t pbuf,int receiveData,int paraData,std::string t_sDataType,std::string t_sByteOrder,string t_sBits)
345 346
 {
347
+
346 348
 	Item* t_item = pitem;
347 349
 	int16_t buf = pbuf;
348 350
 	string t_str;
349 351
 	char t[256];
350
-	//log_i("receiveData:0x%x,paraData:0x%x",receiveData,paraData);
351 352
 	if(receiveData == paraData)
352 353
 	{
354
+		log_i("当前时间,比对成功,设置数据");
353 355
 		//log_i("ID匹配,处理数据");
354 356
 		if (t_sDataType == "I")
355 357
 		{
356 358
 			//log_i("数据类型为I,调用merge16函数,buf=0x%x",buf);
357 359
 			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
-//			}
360
+
383 361
 		}
384 362
 		else if (t_sDataType == "UI")
385 363
 		{
386 364
 			//log_i("数据类型为UI,调用merge16函数");
387 365
 			t_item->setValue(merge16(buf, t_sByteOrder));
366
+			log_i("当前时间,设置数据成功,数据类型UI");
388 367
 		}
389 368
 		else if (t_sDataType == "B")
390 369
 		{
@@ -408,8 +387,9 @@ void ProtocolImpl::setData(Item *pitem,int16_t pbuf,int receiveData,int paraData
408 387
 				snprintf(t, 256, "%d", bits);
409 388
 				t_str = t;
410 389
 				t_item->setValue(t_str);
411
-				//log_i("字符类型设置:%s",t_str.c_str());
390
+
412 391
 			}
392
+			log_i("当前时间,设置数据成功,数据类型B");
413 393
 		}
414 394
 	}
415 395
 }
@@ -614,8 +594,6 @@ HRESULT ProtocolImpl::onRead(Device *pdevice, Packet *ppacket, Item *pitem,
614 594
 	log_i("onRead");
615 595
 
616 596
 	Channel *pC = pdevice->getParent();
617
-
618
-	log_i("Channel pointer: %p", pC);
619 597
 	if (pC == nullptr)
620 598
 	{
621 599
 		log_i("Channel pointer is null");
@@ -641,30 +619,30 @@ HRESULT ProtocolImpl::onRead(Device *pdevice, Packet *ppacket, Item *pitem,
641 619
 		if(currentId != 1)
642 620
 		{
643 621
 			log_i("发送延迟计数:%d",sendDelayCout);
644
-			if(sendDelayCout > 3)
622
+			if(sendDelayCout > 2)
645 623
 			{
646
-
647 624
 				sendAllocFrame(pbuf,currentId , len);
648 625
 				//sendDelayCout = 0;
649
-
650
-
651 626
 			}
652
-			sendDelayCout ++;
627
+			sendDelayCout++;
628
+
653 629
 		}
654 630
 		else
655 631
 		{
656 632
 			sendAllocFrame(pbuf,currentId , len);
633
+			timeOut = 0;
657 634
 		}
658
-		timeOut = 0;
635
+
659 636
 		//pdevice->getBase().setRwstate(READ_WAIT);
660 637
 		pdevice->getBase().setRwstate(WRITE_WAIT);
661 638
 	}
662 639
 
663 640
 	if(currentState == RECEIVED)
664 641
 	{
665
-		if(timeOut > 30)
642
+		if(timeOut > 10)
666 643
 		{
667 644
 			currentState = INIT;
645
+			sendDelayCout = 0;
668 646
 		}
669 647
 		timeOut ++;
670 648
 	}
@@ -691,6 +669,7 @@ HRESULT ProtocolImpl::onRead(Device *pdevice, Packet *ppacket, Item *pitem,
691 669
 		int iParaId;
692 670
 		Base &t_base = ppacket->getVitem().at(0)->getBase();
693 671
 		sParaId = t_base.getParam("功能码");
672
+
694 673
 		log_i("sParaId =%s,对应的实际名称=%s",sParaId.c_str() ,t_base.getName().c_str());
695 674
 		iParaId = strtol(sParaId.c_str(), NULL, HEX);
696 675
 		string t_saddr;
@@ -704,8 +683,6 @@ HRESULT ProtocolImpl::onRead(Device *pdevice, Packet *ppacket, Item *pitem,
704 683
 			log_i("Invalid packet or item");
705 684
 			return S_FALSE;
706 685
 		}
707
-
708
-		//iParaId |=  (t_iaddr << 19);
709 686
 		//判断寄存器是读寄存器
710 687
 		if(frameFuncCode == 0x030000)
711 688
 		{
@@ -739,12 +716,11 @@ HRESULT ProtocolImpl::onRead(Device *pdevice, Packet *ppacket, Item *pitem,
739 716
 			}
740 717
 			pdevice->getBase().setRwstate(READ_WAIT);	//----设置设备状态
741 718
 		}
742
-		//判断寄存器是读写寄存器
743
-		if(frameFuncCode == 0x010000)
744
-		{
745
-			pdevice->getBase().setRwstate(WRITE_WAIT);
746
-		}
747
-
719
+		//寄存器是读写寄存器
720
+//		if(frameFuncCode == 0x010000 )
721
+//		{
722
+//			pdevice->getBase().setRwstate(WRITE_O);
723
+//		}
748 724
 		//初始化数据库容器
749 725
 		if (pC != NULL)
750 726
 		{
@@ -762,39 +738,78 @@ HRESULT ProtocolImpl::onRead(Device *pdevice, Packet *ppacket, Item *pitem,
762 738
 							j < pC->getVDevice().at(i)->getVitem().size(); j++)
763 739
 					{
764 740
 						string t_sId;
741
+						string rsId;
765 742
 						Base &t_base =
766 743
 								pC->getVDevice().at(i)->getVitem().at(j)->getBase();
767 744
 						t_sId = t_base.getParam("写功能码");
768
-						//log_i("写功能码: %s", t_sId.c_str());
769 745
 						int t_iId = 0;
770 746
 						t_iId = strtol(t_sId.c_str(), NULL, HEX);	//----获取帧ID
771
-						//log_i("功能码转换为整数: 0x%x", t_iId);
772
-						string t_stype;
773
-						t_stype = t_base.getParam("帧类型");
774
-						//log_i("帧类型: %s", t_stype.c_str());
775 747
 
776
-						if (t_stype == "1")
748
+						//写功能是读寄存器响应
749
+						if(t_iId == 0x04040000)
777 750
 						{
778
-							t_iId |= 0x80000000;
779
-							log_i("帧类型为1,功能码更新为: 0x%x", t_iId);
751
+							string t_stype;
752
+							t_stype = t_base.getParam("帧类型");
753
+							//log_i("帧类型: %s", t_stype.c_str());
754
+
755
+							if (t_stype == "1")
756
+							{
757
+								t_iId |= 0x80000000;
758
+								log_i("帧类型为1,功能码更新为: 0x%x", t_iId);
759
+							}
760
+
761
+							string sStartAddr;
762
+							//sStartAddr = pC->getVDevice().at(i)->getBase().getParam("起始地址");
763
+							sStartAddr = t_base.getParam("起始地址");
764
+							//log_i("起始地址 =%s", sStartAddr.c_str());
765
+
766
+							int iStartAddr = 0;
767
+							iStartAddr = strtol(sStartAddr.c_str(), NULL, HEX);
768
+							//log_i("起始地址转换为整数: 0x%x", iStartAddr);
769
+							t_iId += iStartAddr;
770
+							string t_saddr;
771
+							t_saddr = pC->getVDevice().at(i)->getBase().getParam("站地址");
772
+							int t_iaddr = 0;
773
+							t_iaddr = strtol(t_saddr.c_str(), NULL, HEX);
774
+							//log_i("站地址:%d",t_iaddr);
775
+							t_iId |=  (t_iaddr << 19);
776
+							//log_i("t_Id = %d",t_iId);
777
+						}
778
+						else
779
+						{
780
+							if(t_iId == 0x04010000)
781
+							{
782
+								t_iId = 0x04040000;
783
+								//log_i("2.t_iId = 0x%x",t_iId);
784
+								string t_stype;
785
+								t_stype = t_base.getParam("帧类型");
786
+								//log_i("帧类型: %s", t_stype.c_str());
787
+
788
+								if (t_stype == "1")
789
+								{
790
+									t_iId |= 0x80000000;
791
+									log_i("帧类型为1,功能码更新为: 0x%x", t_iId);
792
+								}
793
+
794
+								string sStartAddr;
795
+								//sStartAddr = pC->getVDevice().at(i)->getBase().getParam("起始地址");
796
+								sStartAddr = t_base.getParam("起始地址");
797
+								//log_i("起始地址 =%s", sStartAddr.c_str());
798
+
799
+								int iStartAddr = 0;
800
+								iStartAddr = strtol(sStartAddr.c_str(), NULL, HEX);
801
+								//log_i("起始地址转换为整数: 0x%x", iStartAddr);
802
+								t_iId += iStartAddr;
803
+								string t_saddr;
804
+								t_saddr = pC->getVDevice().at(i)->getBase().getParam("站地址");
805
+								int t_iaddr = 0;
806
+								t_iaddr = strtol(t_saddr.c_str(), NULL, HEX);
807
+								//log_i("站地址:%d",t_iaddr);
808
+								t_iId |=  (t_iaddr << 19);
809
+								//log_i("3.t_iId = 0x%x",t_iId);
810
+
811
+							}
780 812
 						}
781
-
782
-						string sStartAddr;
783
-						//sStartAddr = pC->getVDevice().at(i)->getBase().getParam("起始地址");
784
-						sStartAddr = t_base.getParam("起始地址");
785
-						//log_i("起始地址 =%s", sStartAddr.c_str());
786
-
787
-						int iStartAddr = 0;
788
-						iStartAddr = strtol(sStartAddr.c_str(), NULL, HEX);
789
-						//log_i("起始地址转换为整数: 0x%x", iStartAddr);
790
-						t_iId += iStartAddr;
791
-						string t_saddr;
792
-						t_saddr = pC->getVDevice().at(i)->getBase().getParam("站地址");
793
-						int t_iaddr = 0;
794
-						t_iaddr = strtol(t_saddr.c_str(), NULL, HEX);
795
-						//log_i("站地址:%d",t_iaddr);
796
-						t_iId |=  (t_iaddr << 19);
797
-						//log_i("t_Id = %d",t_iId);
798 813
 						tmpItems.insert(
799 814
 								make_pair(t_iId,
800 815
 										pC->getVDevice().at(i)->getVitem().at(j)));
@@ -844,7 +859,7 @@ HRESULT ProtocolImpl::onWrite(Device *pdevice, Packet *ppacket, Item *pitem,
844 859
 	}
845 860
 
846 861
 	string t_sFuncCode;
847
-	t_sFuncCode = base.getParam("功能码");
862
+	t_sFuncCode = base.getParam("功能码");
848 863
 	int t_iFuncCode = 0;
849 864
 	t_iFuncCode = strtol(t_sFuncCode.c_str(), NULL, HEX);	//----获取帧ID
850 865
 
@@ -859,7 +874,7 @@ HRESULT ProtocolImpl::onWrite(Device *pdevice, Packet *ppacket, Item *pitem,
859 874
 	int t_iaddr = 0;
860 875
 	t_iaddr = strtol(t_saddr.c_str(), NULL, HEX);
861 876
 	t_iFuncCode |=  (t_iaddr << 19);
862
-
877
+    log_i("采集点名称是:%s,站地址=%d,功能码=0x%x",base.getName().c_str(),t_iaddr,t_iFuncCode);
863 878
 
864 879
 	*(pbuf + (t_len++)) = HHByte(t_iFuncCode);
865 880
 	*(pbuf + (t_len++)) = HLByte(t_iFuncCode);
@@ -927,10 +942,10 @@ HRESULT ProtocolImpl::onWrite(Device *pdevice, Packet *ppacket, Item *pitem,
927 942
 	}
928 943
 	log_i( "待发送数据为: %s", sbuf.c_str());
929 944
 
930
-	log_i( "%%x, %x", data.data0, data.data1);
945
+	log_i( "0x%x, 0x%x", data.data0, data.data1);
931 946
 	m_tmpwritedata[t_sFuncCode] = data;
932 947
 
933
-	pitem->setValue(swritebuf);
948
+	//pitem->setValue(swritebuf);
934 949
 
935 950
 //	pdevice->getBase().setRwstate(WRITE_O);
936 951
 	pdevice->getBase().setRwstate(WRITE_WAIT);
@@ -959,10 +974,18 @@ HRESULT ProtocolImpl::isResponseOK(Device *pdevice, Packet *ppacket,
959 974
 	{
960 975
 	case READ_O:
961 976
 	case WRITE_WAIT:
977
+//		return S_OK;
978
+		//ret = retStateResponse(ppacket, pitem, len,pbuf);
979
+//		log_i("写等待操作,isResponse 返回值 =%d",ret);
980
+//		if(ret == S_OK)
981
+//		{
982
+//			return S_OK;
983
+//		}
984
+//		return S_FALSE;
962 985
 		return S_OK;
963 986
 	case READ_WAIT:
964 987
 		ret = retStateResponse(ppacket, pitem, len,pbuf);
965
-		log_i("isResponseOK返回值:%d", ret);
988
+		log_i("读等待操作,isResponseOK返回值:%d", ret);
966 989
 		if (ret == S_OK)
967 990
 		{
968 991
 			return S_OK;
@@ -977,6 +1000,7 @@ HRESULT ProtocolImpl::isResponseOK(Device *pdevice, Packet *ppacket,
977 1000
 		return S_FALSE;
978 1001
 	case WRITE_O:
979 1002
 	//case WRITE_WAIT:
1003
+		log_i("只写操作");
980 1004
 
981 1005
 		return S_OK;
982 1006
 	}
@@ -1074,13 +1098,12 @@ int ProtocolImpl::normalDatasResponse(Device* pdevice,Packet* ppacket,Item* pite
1074 1098
 	//stringOutput(pbuf,len);
1075 1099
 	dataSet = retDataSet(ppacket, pitem,pbuf,len);
1076 1100
 
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 1101
 //	for (const auto &entry : dataSet)
1080 1102
 //	{
1081 1103
 //		log_i("dataSet填充返回值:addrFrame = 0x%x, value = 0x%x", entry.first,
1082 1104
 //				entry.second);
1083 1105
 //	}
1106
+	log_i("数据集开始时间");
1084 1107
 	for (const auto &dataPair : dataSet)
1085 1108
 	{
1086 1109
 		//log_i("addData.size=%d", dataSet.size());
@@ -1089,27 +1112,19 @@ int ProtocolImpl::normalDatasResponse(Device* pdevice,Packet* ppacket,Item* pite
1089 1112
 		id = (0x7fffffff) & id;
1090 1113
 		buf = dataPair.second;
1091 1114
 
1092
-		//log_i("Processing data pair with ID: 0x%x", id);
1115
+		log_i("当前时间:Processing data pair with ID: 0x%x", id);
1093 1116
 
1094 1117
 		if (m_tmpItems.count(pC->getBase().getObjid().toString()) > 0)
1095 1118
 		{
1096 1119
 
1097
-			//log_i("Found temporary items for Channel ID: %s",
1098
-					//pC->getBase().getObjid().toString().c_str());
1099 1120
 			std::multimap<int, Item*> tmpItms = m_tmpItems.find(
1100 1121
 					pC->getBase().getObjid().toString())->second; //是否有效
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());
1106
-
1107
-			std::multimap<int, Item*>::size_type cnt = tmpItms.count(
1108
-					id);
1109
-			//log_i("Number of items with ID 0x%x: %zu", id, cnt);
1122
+
1123
+			std::multimap<int, Item*>::size_type cnt = tmpItms.count(id);
1110 1124
 			std::multimap<int, Item*>::iterator iter = tmpItms.find(id);
1111 1125
 			if (iter != tmpItms.end())
1112 1126
 			{
1127
+				log_i("遍历容器条目开始时间,容器中含有条目当前0x%x,%d个",id,cnt);
1113 1128
 				for (; cnt > 0; cnt--, iter++)
1114 1129
 				{
1115 1130
 					//log_i("Processing item %zu for ID 0x%x", cnt, id);
@@ -1118,24 +1133,39 @@ int ProtocolImpl::normalDatasResponse(Device* pdevice,Packet* ppacket,Item* pite
1118 1133
 
1119 1134
 					if (t_item != NULL && pdevice != NULL)
1120 1135
 					{
1121
-
1122
-						int iId = iGetItemParaConfig(t_item,"写功能码");
1136
+						int iwId = iGetItemParaConfig(t_item,"写功能码");
1123 1137
 						int iStartAddr = iGetItemParaConfig(t_item,"起始地址");
1124 1138
 						string t_stype = sGetItemParaConfig(t_item,"帧类型");
1125 1139
 						string t_sDataType = sGetItemParaConfig(t_item,"数据类型");
1126 1140
 						string t_sByteOrder = sGetItemParaConfig(t_item,"字节序");
1127 1141
 						string t_sBits= sGetItemParaConfig(t_item ,"位地址");
1128
-						iId += iStartAddr;
1142
+						iwId += iStartAddr;
1129 1143
 						string t_saddr;
1130 1144
 						t_saddr = pdevice->getBase().getParam("站地址");
1131 1145
 						int t_iaddr = 0;
1132 1146
 						t_iaddr = strtol(t_saddr.c_str(), NULL, HEX);
1133 1147
 						//log_i("t_iId = 0x%x,t_iaddr =%d,",iId,t_iaddr);
1148
+						int itemFuncCode  = (iwId & 0x070000);
1149
+						//条目写功能码解析是读响应
1150
+						if(itemFuncCode == 0x040000)
1151
+						{
1134 1152
 
1135
-						iId |=  (t_iaddr << 19);
1136
-
1137
-						//log_i("设置的数据buf=%d,paraID = 0x%x",buf,iId);
1138
-						setData(t_item, buf,id,iId,t_sDataType,t_sByteOrder,t_sBits);
1153
+							iwId |=  (t_iaddr << 19);
1154
+							//log_i("设置的数据buf=%d,paraID = 0x%x",buf,iId);
1155
+							log_i("进入设置函数");
1156
+							setData(t_item, buf,id,iwId,t_sDataType,t_sByteOrder,t_sBits);
1157
+						}
1158
+						else
1159
+						{
1160
+							//条目写功能码解析是写功能码
1161
+							if(itemFuncCode == 0x010000)
1162
+							{
1163
+                                iwId = iwId  | 0x00040000;
1164
+                                iwId = iwId  & 0xfff4ffff;
1165
+                                iwId |=  (t_iaddr << 19);
1166
+								setData(t_item, buf,id,iwId,t_sDataType,t_sByteOrder,t_sBits);
1167
+							}
1168
+						}
1139 1169
 					}
1140 1170
 
1141 1171
 					else
@@ -1147,6 +1177,7 @@ int ProtocolImpl::normalDatasResponse(Device* pdevice,Packet* ppacket,Item* pite
1147 1177
 						return S_FALSE;
1148 1178
 					}
1149 1179
 				}
1180
+				log_i("遍历容器条目结束时间,容器中含有条目当前0x%x,%d个",id,cnt);
1150 1181
 			}
1151 1182
 		}
1152 1183
 		else
@@ -1154,7 +1185,9 @@ int ProtocolImpl::normalDatasResponse(Device* pdevice,Packet* ppacket,Item* pite
1154 1185
 			log_i("m_tmpItems.count(pC->getBase().getObjid().toString()) <0");
1155 1186
 			return S_FALSE;
1156 1187
 		}
1188
+
1157 1189
 	}
1190
+	log_i("数据集结束时间");
1158 1191
 	return S_OK;
1159 1192
 }
1160 1193
 

Chargement…
Annuler
Enregistrer