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

📄 netbios.cpp

📁 这是一个变电站的监控程序
💻 CPP
📖 第 1 页 / 共 3 页
字号:
#include "stdafx.h"
#include "fert2000.h"
//#include "api232-c.h"
#include "process.h"

extern bool bStopFlag;
extern CHANNEL Channels[MAX_CHANNEL_NUM];
extern RTU Rtus[MAX_RTU_NUM];
extern SYSTEMCOUNT SystemCount;
//extern CDatabase Database;
extern BYTE DebugCommand[0x23];//当前调试的命令数组
extern char DebugRtuNo;
extern EVENT Events;
extern RecSendThread RecSendThread1;
extern long ProgramStartTime;
extern char YKEchoFlag[MAX_RTU_NUM];
extern BYTE YKEcho[MAX_RTU_NUM][6];//?
extern BYTE YKReserved[24];//?
extern unsigned char TBCH0[];   
extern  CFert2000App theApp;
extern NetProcess NetProcess1;
//***********************************************************************


//***********************************************************************
void NetSendFlag() 
{
	Sleep(40);
	NetProcess1.NetSendFlag();

}
void NetSendFrame()
{
	Sleep(40);
	NetProcess1.NetSendFrame();
}
void UdpRx()
{
	Sleep(40);
	NetProcess1.UdpRx();
}
void UdpRxPro()
{
	Sleep(40);
	NetProcess1.UdpRxPro();
}
NetProcess::NetProcess()
{
	UdpBufNo=0;
	TcpBufNo=0;
	FertBufNo=0;
	UdpBufProNo=0;
	TcpBufProNo=0;
	FertBufProNo=0;
	NetFertRXCount=0x999;//?
	NetHostRXCount = 0x999;
	mainhostid='Z';
	mainfertid='Z';
	mainhostno=0;
	mainfertno=0;
	ThisNodeId='Z';
	YcSendRtuNo=0;
	KwhSendRtuNo=0;
	YxwSendRtuNo=0;
	FreqSendRtuNo=0;
	WaterSendRtuNo=0;
	TxCounts=0;
	SendRegisterFlag=1;
	SendEventFlag=0;
	SendYxFlag=0;
	SendATBFlag=0;
	SendYKEchoFlag=0;
	SendYcFlag=0;
	SendKwhFlag=0;
	SendChannelFlag=0;
	SendFreqFlag=0;
	SendWaterFlag=0;
	SendTimeFlag=0;
	SendCancelFlag=0;

    Previous_Receive_ATB_Number = -1;
	Receive_ATB_Number = 0;

	SendSysParaFlag=0;
	SendChannelParaFlag=0;
	SendRtuParaFlag=0;

	YcSendCycle=1;
	YxSendCycle=2;		//
	KwhSendCycle=6;//
	WaterSendCycle=7;//
	FreqSendCycle=5;
	ChannelSendCycle=10;//
	ATBSendCycle=1;
	TimeSendCycle=60;//
	portno=3600;	//boardcast diagram
	ZFRtuNo = -1;
//	InitNet();

}

//***********************************************************************
void NetProcess::XmitDataGram ()
{
	remot_sin_send.sin_family = AF_INET;
	remot_sin_send.sin_addr.s_addr = INADDR_BROADCAST ;
	remot_sin_send.sin_port = htons(portno);   
	remot_sin_send_len = sizeof(remot_sin_send);

	class GRAMHEAD *pHead=(class GRAMHEAD *)NetSend.TXBUF_addr;		
	int len=pHead->Length+sizeof(GRAMHEAD);
	int pp = sendto (sock,NetSend.TXBUF_addr,len,0,
		(struct sockaddr FAR *)&remot_sin_send,remot_sin_send_len);
	NetSend.txfinishflag = TRUE;
	NetSend.TxCount++;
	if (pp == SOCKET_ERROR) 
		CreateBoardSocket();
	return ;

}

void NetProcess::InitNet ()
{
	 NetSend.txfinishflag = TRUE;
	 NetSend.TxCount = 0;
	 for (int i=0; i<NET_RECBUF_NUM; i++) {
		  NetRcv[i].rxfinishflag = FALSE;	//boardcast
		  NetRcv[i].RxCount = 0;
	//	  TcpRcv[i].rxfinishflag = FALSE;//connect
	//	  TcpRcv[i].RxCount = 0;
	 }
	CreateBoardSocket();
//	NodeMsg1.regflag=false;
//	NodeMsg2.regflag=false;
//	NodeMsgFert.regflag=false;

}


//接收广播报文
void NetProcess::UdpRx()
{
	int status;
	while(1)
	{
		if(bStopFlag)
		{
			SetEvent(RecSendThread1.g_hEndEventUdpRX);
			return;
		}
		if ((UdpBufNo+1)%10==UdpBufProNo)
		{
			Sleep(5);
			continue;
		}
		remot_sin_recv.sin_family = AF_INET;
		remot_sin_recv.sin_addr.s_addr = INADDR_BROADCAST ;
		remot_sin_recv.sin_port = htons(portno);   
		remot_sin_recv_len = sizeof(remot_sin_recv);

   		if (sockflag) {
		//	if (NetRcv[UdpBufNo].
			status = recvfrom (sock,NetRcv[UdpBufNo].RXBUF_addr,512,0, 
				(struct sockaddr FAR *)&remot_sin_recv,&remot_sin_recv_len);
 			if (status == SOCKET_ERROR) {
				closesocket( sock );
				sockflag=false;
			}
			else if (status){
				NetRcv[UdpBufNo].RxCount++;
				NetRcv[UdpBufNo].rxfinishflag = 1;
//				SetEvent(g_hEventUdpRX);
				UdpBufNo++;
				if (UdpBufNo>9)
					UdpBufNo=0;
			}
			else {
				closesocket( sock );
				sockflag=false;
			}
		}
		else {
			Sleep(100);
		}
	}

//	return;
}


void NetProcess::CreateBoardSocket()
{
	sock = socket( AF_INET, SOCK_DGRAM, IPPROTO_UDP);
	if (sock == INVALID_SOCKET) {
		::MessageBox(NULL,"socket() failed", "Error", MB_OK);
		closesocket(sock);
		sockflag=false;
		return ;
	}
	local_sin.sin_family = AF_INET;
	local_sin.sin_addr.s_addr = INADDR_ANY;
	local_sin.sin_port = htons(portno);     
	int pp ;
	if ((pp = bind( sock, (struct sockaddr FAR *) &local_sin, sizeof(local_sin))) == SOCKET_ERROR) {
		int pp1 = WSAGetLastError (); 
		closesocket(sock);
		sockflag=false;
		return ;
	}
	pp=1;
	pp=setsockopt(sock,SOL_SOCKET,SO_BROADCAST,(char *)&pp,sizeof(int));
	sockflag=true;
}

void NetProcess::NetSendFrame()
{
	for(;;) {
		Sleep(100);
		if (bStopFlag) {
			SetEvent(RecSendThread1.g_hEndEventNetSendFrame);
			return ;
		}
		if (NetSend.txfinishflag) {
			MakeEventFrame();
			MakeYKEchoFrame();
		}
		if (SendRegisterFlag &&	 NetSend.txfinishflag) {
			MakeRegisterFrame();
			SendRegisterFlag=0;
		}
		if (SendYxFlag && NetSend.txfinishflag) {
			MakeYxFrame();
			SendYxFlag=0;
		}
		if (SendATBFlag &&	 NetSend.txfinishflag) {
			MakeATBFrame();
			SendATBFlag=0;
		}
		if (SendYcFlag &&	 NetSend.txfinishflag) {
			MakeYcFrame();
			SendYcFlag=0;
		}
		if (SendKwhFlag &&	 NetSend.txfinishflag) {
			MakeKwhFrame();
			SendKwhFlag=0;
		}
		if (SendChannelFlag &&	 NetSend.txfinishflag) {
			MakeChannelFrame();
			SendChannelFlag=0;
		}
		if (SendFreqFlag && NetSend.txfinishflag) {
			MakeFreqFrame();
			SendFreqFlag=0;
		}
		if (SendWaterFlag && NetSend.txfinishflag) {
			MakeWaterFrame();
			SendWaterFlag=0;
		}
		if (SendTimeFlag && NetSend.txfinishflag) {
			MakeTimeFrame();
			SendTimeFlag=0;
		}
		if (SendCancelFlag && NetSend.txfinishflag) {
			MakeCancelFrame();
			SendCancelFlag=0;
		}
		if (SendSysParaFlag &&	 NetSend.txfinishflag) {
			MakeSysParaFrame();
			SendSysParaFlag=0;
		}
		if (SendChannelParaFlag &&	 NetSend.txfinishflag) {
			MakeChannelParaFrame();
			SendChannelParaFlag=0;
		}
		if (SendRtuParaFlag && NetSend.txfinishflag){
			MakeRtuParaFrame();
			SendRtuParaFlag=0;
		}
		/*else  if (SendYxParaFlag && NetSend.txfinishflag){
			MakeYxParaFrame();
			SendYxParaFlag=0;
		}
		else  if (SendZFYcParaFlag && NetSend.txfinishflag){
			MakeZFYcParaFrame();
			SendZFYcParaFlag=0;
		}
		else  if (SendZFYxParaFlag && NetSend.txfinishflag){
			MakeZFYxParaFrame();
			SendZFYxParaFlag=0;
		}
		else  if (SendZFKwhParaFlag && NetSend.txfinishflag){
			MakeZFKwhParaFrame();
			SendZFKwhParaFlag=0;
		}*/
	}
}

//*********************************************************************
//cycle=1s 
//向后台机循环发送
void NetProcess::NetSendFlag()
{
	Sleep(100);
	for(;;) {
		Sleep(1000);
		if (bStopFlag) {
			SetEvent(RecSendThread1.g_hEndEventNetSendFlag);
			return ;
		}
		Count++;
		if (mainfertid==ThisNodeId) {
			if (!(Count % YcSendCycle)) {
				SendYcFlag=1;
			}
			if (!(Count % YxSendCycle)) {
				SendYxFlag=1;
			}
			if (!(Count % KwhSendCycle)) {
				SendKwhFlag=1;
			}
			if (!(Count % WaterSendCycle)) {
				SendWaterFlag=1;
			}
			if (!(Count % FreqSendCycle)) {
				SendFreqFlag=1;
			}
			if (!(Count % ChannelSendCycle)) {
				SendChannelFlag=1;
			}
			if (!(Count % TimeSendCycle)) {
				SendTimeFlag=1;			//
			}
		}
		if (!(Count % ATBSendCycle)) {
			SendATBFlag=1;			//平安报文
		}
	}
//	return;
}

//发送遥控返校信息
void NetProcess::MakeYKEchoFrame()
{
	for (int i=0;i<SystemCount.RtuNum;i++) 
		if (YKEchoFlag[i]) 
			break;
	if (i<SystemCount.RtuNum) {
		YKEchoFlag[i]=0;
		class GRAMHEAD *pHead=(class GRAMHEAD *)NetSend.TXBUF_addr;	
		pHead->CmdCode=YKReserved[11];//YK;
		pHead->DestType='G';
		pHead->DestId=mainhostid;
		pHead->SourceType='F';
		pHead->SourceId=ThisNodeId; 
		pHead->SourceNo=ThisNodeId-'A';
		pHead->TxCount=TxCounts++;;
		char * Buf=(char * )(NetSend.TXBUF_addr+sizeof(GRAMHEAD));
		*Buf=0x44;
		*(Buf+1)=0x55;
//		for (int j=0;j<7;j++)
//			*(Buf+2+j)=YKReserved[1+j];//ykecho
		WORD * wp=(WORD *)(YKReserved+1);
		*((WORD *)(Buf+2))=*wp;	//RTU
		*((WORD *)(Buf+4))=*(wp+1);	//YKNO
		*((WORD *)(Buf+6))=*(wp+2);	//YXNO
		*(Buf+7)=YKEcho[i][1];	//0XCC 0X33  0xff
		pHead->Length=9;

		if (mainfertid==ThisNodeId) {
//			if (sockflag1)
//				TcpXmitDataGram (0);
//			if (sockflag2)
//				TcpXmitDataGram (1);
			XmitDataGram();
		}
		
	}
}	
//发送时间信息		
void NetProcess::MakeTimeFrame()
{
	WORD * Buf=(WORD * )(NetSend.TXBUF_addr+sizeof(GRAMHEAD));
	class GRAMHEAD *pHead=(class GRAMHEAD *)NetSend.TXBUF_addr;	
	pHead->CmdCode=SST;
	pHead->DestType='G';
	pHead->DestId=mainfertid;
	pHead->SourceType='F';
	pHead->SourceId=ThisNodeId;
	pHead->SourceNo=ThisNodeId-'A';
	pHead->TxCount=TxCounts++;;
//	pHead->FertStatus=9;
	SYSTEMTIME st;
	GetLocalTime(&st);
	
	*Buf++=st.wYear;
	*Buf++=st.wMonth;
	*Buf++=st.wDayOfWeek;
	*Buf++=st.wDay;
	*Buf++=st.wHour;
	*Buf++=st.wMinute;
	*Buf++=st.wSecond;
	*Buf++=st.wMilliseconds;

	pHead->Length=sizeof(SYSTEMTIME);
	if (mainfertid==ThisNodeId) {
		XmitDataGram ();
	}

}
//发送退出信息
void NetProcess::MakeCancelFrame()
{
	char * Buf=NetSend.TXBUF_addr+sizeof(GRAMHEAD);
	class GRAMHEAD *pHead=(class GRAMHEAD *)NetSend.TXBUF_addr;	
	pHead->CmdCode=CANCEL;
	pHead->DestType='G';
	pHead->DestId=mainfertid;
	pHead->SourceType='F';
	pHead->SourceId=ThisNodeId;
	pHead->SourceNo=ThisNodeId-'A';
	pHead->TxCount=TxCounts++;;
//	pHead->FertStatus=9;
	CREGISTER  *cRegister = (CREGISTER far *)Buf;
	cRegister->NodeId=ThisNodeId;
	cRegister->NodeNo=ThisNodeId-'A';
	cRegister->NodeType='F';
	cRegister->NodeState='N';

	pHead->Length=sizeof(CREGISTER);
//	if (sockflag1)
//		TcpXmitDataGram (0);
//	if (sockflag2)
//		TcpXmitDataGram (1);
	XmitDataGram();


}
//发送系统基本参数修改报文
void NetProcess::MakeSysParaFrame()
{
	char * Buf=NetSend.TXBUF_addr+sizeof(GRAMHEAD);
	class GRAMHEAD *pHead=(class GRAMHEAD *)NetSend.TXBUF_addr;	
	pHead->CmdCode=SENDSYSPARA;
	pHead->DestType='F';
	pHead->DestId=mainfertid;
	pHead->SourceType='F';
	pHead->SourceId=ThisNodeId;
	pHead->SourceNo=ThisNodeId-'A';
	pHead->TxCount=TxCounts++;;
//	pHead->FertStatus=9;

	pHead->Length=4;//12;
	*((WORD *)Buf)=SystemCount.RtuNum;
	*((WORD *)(Buf+2))=SystemCount.ChNum;
/*	*((WORD *)(Buf+4))=SystemCount.EventNum;
	*((WORD *)(Buf+6))=SystemCount.RtuYcNum;
	*((WORD *)(Buf+8))=SystemCount.RtuYxNum;
	*((WORD *)(Buf+10))=SystemCount.RtuKwhNum;
*/
	XmitDataGram ();

}
//发送RTU参数修改报文
void NetProcess::MakeRtuParaFrame()
{
	WORD *wp;
	BYTE RtuNo=ModifyParaNo[1];
	if (RtuNo<SystemCount.RtuNum) 
		SendRtuParaFlag=0;
	else
		return;
	char * Buf=NetSend.TXBUF_addr+sizeof(GRAMHEAD);
	class GRAMHEAD *pHead=(class GRAMHEAD *)NetSend.TXBUF_addr;	
	pHead->CmdCode=SENDRTUPARA;
	pHead->DestType='F';
	pHead->DestId=mainfertid;
	pHead->SourceType='F';
	pHead->SourceId=ThisNodeId;
	pHead->SourceNo=ThisNodeId-'A';
	pHead->TxCount=TxCounts++;;
//	pHead->FertStatus=9;

	pHead->Length=73;//71;
	*(Buf++)=RtuNo;
	*(Buf++)=Rtus[RtuNo].Addr;
	*(Buf++)=Rtus[RtuNo].Flag;
	*(Buf++)=Rtus[RtuNo].TimeForbit;
	*(Buf++)=Rtus[RtuNo].RtuChNo;
	*(Buf++)=Rtus[RtuNo].WaitTime;
	*(Buf++)=Rtus[RtuNo].AnswerWaitTime;
	*(Buf++)=Rtus[RtuNo].DeadArea;
	*(Buf++)=Rtus[RtuNo].FreqNum;
	*(Buf++)=Rtus[RtuNo].WaterNum;
	*(Buf++)=Rtus[RtuNo].Protocol;
	*(Buf++)=Rtus[RtuNo].SoeOnYxFlag;

	wp=(WORD *)Buf;
	*(wp++)=Rtus[RtuNo].YcNum;
	*(wp++)=Rtus[RtuNo].YxNum;
	*(wp++)=Rtus[RtuNo].KwhNum;
	*(wp++)=Rtus[RtuNo].OtherNum;
	Buf +=8;
	for (int i=0;i<16;i++) *(Buf++)=Rtus[RtuNo].RRC[i];
	for (i=0;i<20;i++) *(Buf++)=Rtus[RtuNo].Name[i];
	for (i=0;i<10;i++) *(Buf++)=Rtus[RtuNo].ProtocolStamp[i];
	for (i=0;i<7;i++) *(Buf+i) = Rtus[RtuNo].SynWord[i];
	/*pHead->Length=35;
	*((WORD *)Buf)=rtu;					//rtu
	for (int i=0;i<20;i++)		//name
		*(Buf+2+i) = *(p+2+i);
	*(Buf+22)=*(p+22);			//flag
	*(Buf+23)=*(p+23);			//ChNo
	wp=(WORD *)(p+24);			
	*((WORD *)(Buf+24))=*wp;	//YcNum
	*((WORD *)(Buf+26))=*(wp+1);	//YxwNum
	*((WORD *)(Buf+28))=*(wp+2);	//KwhNum
	*(Buf+30)=*(p+30);			//Freq
	*(Buf+31)=*(p+31);			//Water
	wp=(WORD *)(p+32);			
	*((WORD *)(Buf+32))=*wp;	//OtherNum
	*(Buf+34)=*(p+34);			//Addr
*/
	XmitDataGram ();

}

//发送通道参数修改报文
void NetProcess::MakeChannelParaFrame()
{
	//WORD  *wp;//WORD *)ModifyParaBuf;
	//BYTE  *p;//BYTE *)ModifyParaBuf;

	BYTE chno=ModifyParaNo[0];
	if (chno<SystemCount.ChNum) 
		SendChannelParaFlag=0;

⌨️ 快捷键说明

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