⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 netbios.cpp

📁 这是一个变电站的监控程序
💻 CPP
📖 第 1 页 / 共 3 页
字号:
				for (int i=0;i<Rtus[FreqSendRtuNo].FreqNum;i++)
					*FreqBuf++=Rtus[FreqSendRtuNo].Freq[i];
				Buf = (char*)FreqBuf;

				if (++FreqSendRtuNo>=SystemCount.RtuNum) {
					FreqSendRtuNo=0;
					pHead->Length=Len;
					if (mainfertid==ThisNodeId) {
						XmitDataGram ();
//						if (sockflag1)
//							TcpXmitDataGram (0);
//						if (sockflag2)
//							TcpXmitDataGram (1);
					}
					return;
				}
			}
			else {
				pHead->Length=Len;
				if (mainfertid==ThisNodeId) {
					XmitDataGram ();
//					if (sockflag1)
//						TcpXmitDataGram (0);
//					if (sockflag2)
//						TcpXmitDataGram (1);
				}
				return;
			}

		}						
		else {
			if (++FreqSendRtuNo>=SystemCount.RtuNum) {
				FreqSendRtuNo=0;
				pHead->Length=Len;
				if (mainfertid==ThisNodeId) {
					XmitDataGram ();
//					if (sockflag1)
//						TcpXmitDataGram (0);
//					if (sockflag2)
//						TcpXmitDataGram (1);
				}
				return;

			}
		}
	}
}

//*********************************************************************
//处理前置机数据包
void NetProcess::FertGram(LPSTR ProBuf)
{
	WORD 		*pp;
	int RtuNo,ChNo,i,l;
//	WORD yxstatus;
	bool InitChannel = false;
	char ChangeFlag=0;
	LPSTR	rxbuf = (LPSTR)(ProBuf + sizeof(GRAMHEAD));
	GRAMHEAD *gramhead;
	gramhead = (GRAMHEAD *)ProBuf;
	switch (gramhead->CmdCode)
	{
		case ATB:								// 平安报文
			break;
		case SENDSYSPARA:				//系统参数 
			
			pp=(WORD *)rxbuf;
			if (SystemCount.RtuNum!=*pp) {
				ChangeFlag=1;
			}
			if (SystemCount.ChNum!=*(pp+1)) {
				ChangeFlag=1;
			}
/*			if (SystemCount.EventNum!=*(pp+2)) {
				ChangeFlag=1;
			}
			if (SystemCount.RtuYcNum!=*(pp+3)) {
				ChangeFlag=1; 
			}
			if (SystemCount.RtuYxNum!=*(pp+4)) {
				ChangeFlag=1;
			}
			if (SystemCount.RtuKwhNum!=*(pp+5)) {
				ChangeFlag=1;
			}
*/
			if (ChangeFlag) {
				Beep(500,500);
				SystemCount.RtuNum=*pp;
				SystemCount.ChNum=*(pp+1);
/*				SystemCount.EventNum=*(pp+2);
				SystemCount.RtuYcNum=*(pp+3);
				SystemCount.RtuYxNum=*(pp+4);
				SystemCount.RtuKwhNum=*(pp+5);*/
				theApp.WriteSystemCount();
//				SaveSystemPara(&SystemCount);
			}
			break;

		case SENDRTUPARA:				//RTU参数 
			RtuNo=*(rxbuf++);
	
			Rtus[RtuNo].Addr = *(rxbuf++);
			Rtus[RtuNo].Flag = *(rxbuf++);
			Rtus[RtuNo].TimeForbit = *(rxbuf++);
			Rtus[RtuNo].RtuChNo = *(rxbuf++);
			Rtus[RtuNo].WaitTime = *(rxbuf++);
			Rtus[RtuNo].AnswerWaitTime = *(rxbuf++);
			Rtus[RtuNo].DeadArea = *(rxbuf++);
			Rtus[RtuNo].FreqNum = *(rxbuf++);
			Rtus[RtuNo].WaterNum = *(rxbuf++);
			Rtus[RtuNo].Protocol = *(rxbuf++);
			Rtus[RtuNo].SoeOnYxFlag = *(rxbuf++);

			pp=(WORD *)rxbuf;
			Rtus[RtuNo].YcNum = *(pp++);
			Rtus[RtuNo].YxNum = *(pp++);
			Rtus[RtuNo].KwhNum = *(pp++);
			Rtus[RtuNo].OtherNum = *(pp++);
			rxbuf +=8;
			for (i=0;i<16;i++) Rtus[RtuNo].RRC[i]=*(rxbuf++);
			for (i=0;i<20;i++) Rtus[RtuNo].Name[i]=*(rxbuf++);
			for (i=0;i<10;i++) Rtus[RtuNo].ProtocolStamp[i]=*(rxbuf++);
			for (i=0;i<7;i++) Rtus[RtuNo].SynWord[i] = *(rxbuf++);
			theApp.WriteARtuPara(RtuNo);
//			SaveARtuPara(-1);
			break;
		case CHANNELPARA:				//CHANNEL参数 
			ChNo=*(rxbuf++);
			if (Channels[ChNo].Flag != *rxbuf) InitChannel = true;
	
			Channels[ChNo].Flag=*(rxbuf++);
			if (Channels[ChNo].SorA != *rxbuf) InitChannel = true;
			Channels[ChNo].SorA=*(rxbuf++);
			if (Channels[ChNo].Baud != *rxbuf) InitChannel =true;
			Channels[ChNo].Baud=*(rxbuf++);
			if (Channels[ChNo].EndBit != *rxbuf) InitChannel = true;
			Channels[ChNo].EndBit=*(rxbuf++);
			if ((Channels[ChNo].ParityBit != *rxbuf) || (Channels[ChNo].DataBit != *(rxbuf+2)))//校验为不需处理,总是一位
				InitChannel = true;
			Channels[ChNo].ParityBit=*(rxbuf++);
			Channels[ChNo].CheckBit=*(rxbuf++);
			Channels[ChNo].DataBit=*(rxbuf++);
			Channels[ChNo].Protocol=*(rxbuf++);
			Channels[ChNo].ChRtuNum=*(rxbuf++);
			Channels[ChNo].SynWord = *((WORD*)rxbuf);
			rxbuf += 2;
//			Channels[ChNo].BasePort=*((WORD*)rxbuf);
//			rxbuf += 2;
			
			if (InitChannel)
			{
				if(Channels[ChNo].Flag) 
					if (NetProcess1.ThisNodeId == NetProcess1.mainfertid)
						theApp.InitChannel(ChNo,1);
				else
					theApp.CloseChannel(ChNo);
			}
			for (i=0;i<Channels[ChNo].ChRtuNum;i++) 
			{
				Channels[ChNo].ChRtuNo[i]=*(rxbuf+i);
				Rtus[Channels[ChNo].ChRtuNo[i]].RtuChNo = ChNo;
			}
			for (i=0;i<SystemCount.RtuNum;i++)
			{
				if (Rtus[i].Flag)
				{
					for (int k=0;k<Channels[Rtus[i].RtuChNo].ChRtuNum;k++)
						if (i == Channels[Rtus[i].RtuChNo].ChRtuNo[k]) break;
					if ((k>=Channels[Rtus[i].RtuChNo].ChRtuNum) || !Channels[Rtus[i].RtuChNo].Flag)
					{
						Rtus[i].Flag = false;
					}
				} 
			}
			theApp.WriteAChannelPara(ChNo);
//			SaveARtuPara(-1);
//			SaveAChPara(ChNo);
			break;
		case YXDRF:
			if (ThisNodeId == mainfertid) break;
			for (l=0;l<gramhead->Length;)
			{
				BYTE *YxBuf;
				RtuNo = rxbuf[l];
				if (RtuNo>=MAX_RTU_NUM) break;
				WORD YxNum = *(WORD*)&rxbuf[l+1] * 8;
				if (YxNum > Rtus[RtuNo].YxNum) YxNum = Rtus[RtuNo].YxNum;				
				YxBuf = (BYTE*)&rxbuf[l+3];
			    for (int j=0;j<YxNum;j++)
					Rtus[RtuNo].YxValue[j] = (YxBuf[j/8] & (1<<(j%8))) ?1:0;
				l += *((WORD*)&rxbuf[l+1])+3;
			}
			break;
		case YCDRF:
			if (ThisNodeId == mainfertid) break;
			for (l=0;l<gramhead->Length;)
			{
				WORD *YcBuf;
				RtuNo = rxbuf[l+2];
				if (RtuNo>=MAX_RTU_NUM ) break;
				WORD YcNum = *(WORD*)&rxbuf[l+3]/2;
				if (YcNum > Rtus[RtuNo].YcNum) YcNum = Rtus[RtuNo].YcNum;
				YcBuf = (WORD*)&rxbuf[l+5];
				Rtus[RtuNo].Freq[0] = *((WORD*)(rxbuf+l));
				for (int j=0;j<YcNum;j++)
					Rtus[RtuNo].YcValue[j] = YcBuf[j];
				l += *((WORD*)(rxbuf+l+3)) + 5;
			}
			break;
		case KWHDRF:
			if (ThisNodeId == mainfertid) break;
			for (l=0;l<gramhead->Length;)
			{
				DWORD *KWHBuf;
				RtuNo = rxbuf[l];
				if (RtuNo>63) break;
				WORD KwhNum = *(WORD*)&rxbuf[l+1]/4;
				if (KwhNum > Rtus[RtuNo].KwhNum) KwhNum = Rtus[RtuNo].KwhNum;
				KWHBuf = (DWORD*)&rxbuf[l+3];
				for (int j=0;j<KwhNum;j++)
					Rtus[RtuNo].KwhValue[j] = KWHBuf[j];
				l += *((WORD*)(rxbuf+l+1)) + 3; 
			}
			break;
		case FRQDRF:
			if (ThisNodeId == mainfertid) break;
			for (l=0;l<gramhead->Length;)
			{
				float *FreqBuf;
				RtuNo = rxbuf[l];
				if (RtuNo>63) break;
				WORD FreqNum = *(WORD*)&rxbuf[l+1]/sizeof(float); 
				if (FreqNum > Rtus[RtuNo].KwhNum) FreqNum = Rtus[RtuNo].KwhNum;
				FreqBuf = (float*)&rxbuf[l+3];
				for (int j=0;j<FreqNum;j++)
					Rtus[RtuNo].Freq[j] = FreqBuf[j];
				l += *((WORD*)(rxbuf+1+l)) + 3;
			}
			break;
		case WTRDRF:
			if (ThisNodeId == mainfertid) break;
			for (l=0;l<gramhead->Length;)
			{
				float *WaterBuf;
				RtuNo = rxbuf[l];
				if (RtuNo>63) break;
				WORD WaterNum = *(WORD*)&rxbuf[l+1]/sizeof(float);
				if (WaterNum > Rtus[RtuNo].WaterNum) WaterNum = Rtus[RtuNo].WaterNum;
				WaterBuf = (float*)&rxbuf[l+3];
				for (int j=0;j<WaterNum;j++)
					Rtus[RtuNo].Water[j] = WaterBuf[j];
				int a = *((WORD*)(rxbuf+l+1)) + 3;
				l += a;
			}
			break;
		case SST:
			if (ThisNodeId != mainfertid)
			{
				SYSTEMTIME SysTime;
				WORD *SysTimeWord = (WORD *)rxbuf;
				SysTime.wYear = SysTimeWord[0];
				SysTime.wMonth = SysTimeWord[1];
				SysTime.wDayOfWeek = SysTimeWord[2];
				SysTime.wDay = SysTimeWord[3];
				SysTime.wHour = SysTimeWord[4];
				SysTime.wMinute = SysTimeWord[5];
				SysTime.wSecond = SysTimeWord[6];
				SysTime.wMilliseconds = SysTimeWord[7];
				SetLocalTime(&SysTime);
			}
			break;
		case COMMSTATE:
			if (ThisNodeId != mainfertid)
			{
				int RtuNo;
				ProgramStartTime = *(long*)rxbuf;
				rxbuf += 4;
				// Modify by lqs on 2000.12.20
				RtuNo = *rxbuf++;
				Rtus[RtuNo].bFault = (*rxbuf++)?false:true;
				Rtus[RtuNo].FaultTime = *(DWORD*)rxbuf;
				rxbuf += 4;
				Rtus[RtuNo].RecFrameSum = *(DWORD*)rxbuf;
				rxbuf += 4;
				Rtus[RtuNo].ErrFrameSum = *(DWORD*)rxbuf;
				rxbuf += 4;
				for(int k=0;k<25;k++)
				{
				  Rtus[RtuNo].ErrRate[k]=*(float*)rxbuf;
			      rxbuf += 4;
				}
				// Modify by lqs on 2000.12.20
			}
			break;
	}

}

//*********************************************************************
//处理后台机数据包
void NetProcess::HostGram (LPSTR ProBuf)
{
	GRAMHEAD 	*gh;
	CREGISTER 	*cRegister;
	int RtuNo,i;
	BYTE OldMainFertId;
	NODEMSG *nodemsg1;
	char ChangeFlag=0;
	gh = (GRAMHEAD *)ProBuf;

	LPSTR	rxbuf = (LPSTR)(ProBuf + sizeof(GRAMHEAD));
	cRegister = (CREGISTER *)rxbuf;

	switch (gh->CmdCode) {
		//Add by yzw 2001.7.18
		case RET:
			{
				if (mainfertid != ThisNodeId)
					break;
				BYTE Temp = rxbuf[0];
				if (Temp == 0)
				{
					RtuNo = rxbuf[1] + rxbuf[2] * 256;
					Rtus[RtuNo].bRet = true;
				}
				else
				{
					for (int i = 0;i < SystemCount.RtuNum;i++)
					{
						RtuNo = i;
						Rtus[RtuNo].bRet = true;
					}
				}
			}
			break;
		///////////////////////////
		case YK:
 			memcpy(YKReserved,rxbuf,8);	// 0----11/22/33h
										//1.2---rtuno
										//3.4---ykno;3-控分闸点号,4-控合闸点号
										//5.6---yxno
										//7-----33/cch(分/合)
			YKReserved[11]=YK;
			RtuNo=(int)(*((WORD *)(YKReserved+1)));
			if (mainfertid == ThisNodeId)
			{
				if (Channels[Rtus[RtuNo].RtuChNo].Flag )
				{
					if (!(Channels[Rtus[RtuNo].RtuChNo].ChStatus & 0x100))
						Channels[Rtus[RtuNo].RtuChNo].ChStatus |= 0x100;
				}
			}
			break;
		case YT:
			memcpy(YKReserved,rxbuf,8);	// 0----11/22/33/55h
										//1.2---rtuno
										//3.4---ykno
										//5.6---yxno
										//7-----33/cch(分/合)
			YKReserved[11]=YT;
			RtuNo=(int)(*((WORD *)(YKReserved+1)));
			if (mainfertid == ThisNodeId)
			{
				if (Channels[Rtus[RtuNo].RtuChNo].Flag )
				{
					if (!(Channels[Rtus[RtuNo].RtuChNo].ChStatus & 0x100))
						Channels[Rtus[RtuNo].RtuChNo].ChStatus |= 0x100;
				}
			}
			break;
		case SETCLK:							// 修改时钟命令
			break;
		case ATB:// 平安报文
			if ((gh->SourceType == 'H') && (gh->SourceId == mainhostid))
			{
				if (Receive_ATB_Number<10000)
					Receive_ATB_Number++;
				else Receive_ATB_Number = 1;
			}
			break;
		case SYSSTATE:							// 节点参数
			OldMainFertId = mainfertid;
			mainfertid = *(rxbuf+1) & 0xdf;
			mainfertno = *(rxbuf+2);
			mainhostid = *(rxbuf+3) & 0xdf;
			mainhostno = *(rxbuf+4);
			rxbuf += 8;
			nodemsg1 = (NODEMSG far *)rxbuf;
			for ( i=0; i<(int)((gh->Length-8)/sizeof(NODEMSG)); i++ )
			{
				if (( nodemsg1->GetType() =='H') && (nodemsg1->GetId()=='A')) {
					NodeMsg1 = *nodemsg1;
				}
				else if (( nodemsg1->GetType() =='H') && (nodemsg1->GetId()=='B')) {
					NodeMsg2 = *nodemsg1;
				}
				else if (( nodemsg1->GetType() =='F') && (nodemsg1->GetId()=='B')) {
					NodeMsgFert = *nodemsg1;
				}
				nodemsg1++;
			}

			break;
		case SENDSYSPARA:				//系统参数 
			
			break;

		case SENDRTUPARA:				//RTU参数 
			
			break;
		case SENDZFYC:
			{
				for (int i = 0;i < SystemCount.RtuNum;i++)
				{
					if (Rtus[i].Flag && ((Rtus[i].Protocol == 4) || (Rtus[i].Protocol == 5))) //zf protocol
					{
						ZFRtuNo = i;
						break;
					}
				}
				if (ZFRtuNo < 0 || ZFRtuNo >= SystemCount.RtuNum ) break;
				WORD* wp = (WORD*)rxbuf;
				
				Rtus[ZFRtuNo].YcNum = *wp;
				if (Rtus[ZFRtuNo].YcNum < RTU_MAX_YC_NUM)
				{
					for (int i = 0;i < Rtus[ZFRtuNo].YcNum ;i++)
					{
						wp++;
						int value = 0;
						WORD wValue=*wp;
						if (wValue & 0x8000) 
						{// have error!
							wValue=~wValue+1;
							value =-wValue;
						}
						else
							value =wValue;
						Rtus[ZFRtuNo].YcValueOld[i] = Rtus[ZFRtuNo].YcValue [i];
						Rtus[ZFRtuNo].YcValue [i] = (float)value / 10;
					}
				}
			}
			break;
		case SENDZFYX:
			{
				for (int i = 0;i < SystemCount.RtuNum;i++)
				{
					if (Rtus[i].Flag && ((Rtus[i].Protocol == 4) || (Rtus[i].Protocol == 5))) //zf protocol
					{
						ZFRtuNo = i;
						break;
					}
				}
				if (ZFRtuNo < 0 || ZFRtuNo >= SystemCount.RtuNum ) break;
				WORD* wp = (WORD*)rxbuf;
				Rtus[ZFRtuNo].YxwNum = *wp;
				if (Rtus[ZFRtuNo].YxwNum < RTU_MAX_YXW_NUM)
				{
					for (int i = 0;i <Rtus[ZFRtuNo].YxwNum ;i++)
					{
						wp++;
						for (int k=0;k<16;k++)
							Rtus[ZFRtuNo].YxValue[i*16+k] = ((*wp) & (1<<k)) ? 1:0;
					}
				}
			}
			break;
	}
}


//***********************************************************************
//基于UDP的广播报文接收处理线程


void NetProcess::UdpRxPro(void)
{
	while(1)
	{	
		Sleep(5);
		if (bStopFlag) 
		{
			SetEvent(RecSendThread1.g_hEndEventUdpRXProc);
			return;
		}

		if( NetRcv[UdpBufProNo].rxfinishflag !=1 )
		{
			Sleep(5);
			continue;
		}
	
		if (UdpBufProNo!=UdpBufNo) 
		{
			GRAMHEAD far *gHead = (GRAMHEAD far *)NetRcv[UdpBufProNo].RXBUF_addr;

			if ( gHead->SourceType == 'F' )
			{
				if (gHead->TxCount == NetFertRXCount)
				{
					UdpBufProNo = (++UdpBufProNo) % 10;
					Sleep(5);
					continue;
				}
				else 
					NetFertRXCount = gHead->TxCount;
				if (gHead->SourceId!=ThisNodeId) 
					FertGram(NetRcv[UdpBufProNo].RXBUF_addr );
			}
			else// if (gHead->SourceType == 'H')
			{
				if (gHead->TxCount == NetHostRXCount)
				{
					UdpBufProNo = (++UdpBufProNo) % 10;
					Sleep(5);
					continue;
				}
				else 
					NetHostRXCount = gHead->TxCount;
				HostGram(NetRcv[UdpBufProNo].RXBUF_addr );
			}

			NetRcv[UdpBufProNo].rxfinishflag = 0;
			UdpBufProNo++;
			if(UdpBufProNo>9) UdpBufProNo=0;
		}
		else
			Sleep(20);
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -