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

📄 fert.cpp

📁 cdma无线通信转发程序 用于电力调度数据的转发
💻 CPP
📖 第 1 页 / 共 2 页
字号:
    {
		//检查通道状态
		if (intCount%5==1)  //1s
		{
			for( i=0; i<G_Channel.chan_num ; i++)
			{
//				if (loading_flag) continue;
					//CloseAChan(i);
				
				///////////////
//***************************************************************************************

				if(G_Channel.systype==0)
				{
					dev = &G_Channel.chan_data[i].rchanpara;//接收通道
					if (dev->resetf) 
					{						
						CloseAChan(dev);					
						dev->resetf=0;
					}
				
					if (dev->devId<=(int)INVALID_HANDLE_VALUE) 
					{
						OpenAChan(dev);
						G_Channel.chan_data[i].rchanpara.RecCnt=0;
						G_Channel.chan_data[i].rchanpara.SendCnt=0;					
					}
				}
					//fertsysdata->hci.devId[i] = devId[i];
					////////////////////////////////////////////
					///////////////
				else if(G_Channel.systype==1)	
				{
					dev = &G_Channel.chan_data[i].tchanpara; //发送通道
					if (dev->resetf)
					{						
						CloseAChan(dev);   
						dev->resetf=0;
					}
				
				    if (dev->devId<=(int)INVALID_HANDLE_VALUE) 
					{
					  OpenAChan(dev);
					  G_Channel.chan_data[i].tchanpara.SendCnt=0;
					  G_Channel.chan_data[i].tchanpara.RecCnt=0;
					}
				 //fertsysdata->hci.devId[i] = devId[i];
				 ////////////////////////////////////////////
				}
			}
		}   
     
		Sleep(200);
		intCount++;
    }

	id1 = NULL;

    return NULL;
}

UINT  rwproc(void *arg)
{
	int   count=0;
    while (live_flag)
    {
    	for (int i=0;i<G_Channel.chan_num;i++)
		{		
			if (live_flag==FALSE) break;
//***************************************************************************************
            if(G_Channel.systype==0)//*******接收端读取数据至G_Channel.chan_data[i].recrx
			{
			  ReadAChan(i,RECCHAN);
			}
			/*
			else if(G_Channel.systype==1)//*******发送端写入数据至G_Channel.chan_data[i].sentx
			{
              WriteAChan(i,SENDCHAN);
			}
			*/
			/*
			WriteAChan(i,RECCHAN);

			TXBUF * tx;short cnt;
			tx = &G_Channel.chan_data[i].sentx;
			LenTrnQ(tx,&cnt);
            if(cnt>= G_Channel.recTrig || count% (G_Channel.timeTrig/40) ==0)
			{
			    WriteAChan(i,SENDCHAN);			
			}
			*/
		}
        count ++;
		Sleep(40);
    }
    return NULL;
}
//*********************************************************************************************************************
void writefile(int channum,TXBUF *recfilebuff)
{
	CString str;
	UCHAR value;
    char strpath[50];
    GetCurrentDirectory(MAX_PATH,strpath);
    str.Format("%s\\data\\通道%02d%s.txt",strpath,channum,G_Channel.chan_data[channum].chanName);//不能用fopen创建文件夹
    //str.Format("%s\\通道%02d%s.txt",strpath,channum,G_Channel.chan_data[channum].chanName);
	int cnt=0;
	char writebuff[TRNLength];

	while(recfilebuff->tailP != recfilebuff->headP)
	{
		value = recfilebuff->buf[recfilebuff->headP];
	    writebuff[cnt] = value;
		recfilebuff->headP = (recfilebuff->headP + 1) % TRNLength;
		cnt++;
	}

	if (cnt >0) 
	{
		FILE *fp;//声明文件指针对象
		fp=fopen(str,"a+");
		//fp=fopen("C:\\Program Files\\test.txt","r+");
		if(fp!=NULL) 
		{
			fwrite(writebuff,sizeof(char),cnt,fp);
			fclose(fp);
		}		
	}
}
UINT deletefile(void *arg)
{
	char strpath[MAX_PATH], file_name[MAX_PATH];

	struct _stat stat_buf;

	int result;

	while (live_flag)
	{
		for(int i=0;i<G_Channel.chan_num;i++)
		{
			GetCurrentDirectory(MAX_PATH, strpath);

			sprintf (file_name, "%s\\data\\通道%02d%s.txt", strpath, i, G_Channel.chan_data[i].chanName);

			result = _stat( file_name, &stat_buf );

			if (result == 0) {
				if (stat_buf.st_size > 3000) {
					remove(file_name);
				}
			}
/*          CString str;
//			char strpath[50];
//			GetCurrentDirectory(MAX_PATH,strpath);
//			str.Format("%s\\data\\通道%02d%s.txt",strpath,i,G_Channel.chan_data[i].chanName);
//			
            DWORD  filesize=0;
//     
//			BOOL     bIsFinded;   
//			bIsFinded=(BOOL)tempFind.FindFile(str);
//			if(bIsFinded)
//			{
//				bIsFinded = tempFind.FindNextFile();
//				filesize=tempFind.GetLength();
//				if(filesize>3000)
//				{
//					DeleteFile(str);
//				}
//			}
*/
		}
		Sleep(40);
	}
    return NULL;
}
//************************************************************************************************************************
//************************************************************************************************************************

UINT afertproc (void *arg)
{
	uint count[MAXCHANNUM];//
	int fracountsum[MAXCHANNUM];//每次循环中帧的总数
	memset(&count, 0, sizeof(uint) * MAXCHANNUM);
	memset(&fracountsum, 0, sizeof(int) * MAXCHANNUM);
 	while (live_flag)
    {
    	for (int i=0;i<G_Channel.chan_num;i++) 
		{
			if (live_flag==FALSE) break;
            if(G_Channel.systype==0)//接收端处理数据
			{
				RXBUF * recreadbuff; 
				TXBUF  * recfilebuff; 
				int    ptr = 0;
				short   len;
				uchar   val;
                recreadbuff= &G_Channel.chan_data[i].recrx ;
				recfilebuff = &G_Channel.chan_data[i].rectx ;
   				LenRecQ(recreadbuff,&len);
				if(len>6)
				{	
					while (1) {
						LenRecQ(recreadbuff,&len);
						if (len ==0) break;
						
						ptr = recreadbuff->headP;
				
						if (recreadbuff->buf[ptr]   == 0xeb && 
							recreadbuff->buf[ptr+1] == 0x90 &&
                            recreadbuff->buf[ptr+2] == 0xeb &&
							recreadbuff->buf[ptr+3] == 0x90 &&
							recreadbuff->buf[ptr+4] == 0xeb &&
							recreadbuff->buf[ptr+5] == 0x90)
						{
							fracountsum[i]++;
							LenTrnQ(recfilebuff,&len);
							if (fracountsum[i] >3||len >1800) {

								TRACE("写文件!\r\n");
								writefile(i,recfilebuff);
								fracountsum[i] = 0;
								count[i] =0;//超时计数count清零
							}
						}
						else if(recreadbuff->buf[ptr]   == 0xD7 &&
								recreadbuff->buf[ptr+1] == 0x09 &&
								recreadbuff->buf[ptr+2] == 0xD7 &&
								recreadbuff->buf[ptr+3] == 0x09 &&
								recreadbuff->buf[ptr+4] == 0xD7 &&
								recreadbuff->buf[ptr+5] == 0x09)
						{	
							fracountsum[i]++;
							LenTrnQ(recfilebuff,&len);
							if (fracountsum[i] >3||len >1800 ) {
								TRACE("写文件!\r\n");
								writefile(i,recfilebuff);
								fracountsum[i] = 0;
								count[i] =0;
							}
						}
					    GetRecVal(recreadbuff, &val);
					    EnterTrnQ(recfilebuff,val);
					}
				}
				LenTrnQ(recfilebuff,&len);//用来计时的count[i],判断5秒没执行writefile操作。
				if (len >0) count[i]++;
				if (count[i] > 25 *5) { //sleep(40),所以一秒能执行25次,5秒如果没写,就执行writefile
					TRACE("写文件!\r\n");
					writefile(i,recfilebuff);
					fracountsum[i] = 0;
					count[i] =0;
				}
			}
			else if(G_Channel.systype==1)//发送端处理数据(从文件读取数据到G_Channel.chan_data[i].sentx)
			{
				CString str;
				char strpath[50];
			    GetCurrentDirectory(MAX_PATH,strpath);
				str.Format("%s\\data\\通道%02d%s.txt",strpath,i,G_Channel.chan_data[i].chanName);
				char tmpbuff[1024];
				TXBUF	*tx;
				FILE *fp;//声明文件指针对象
				fp=fopen(str,"r+");
				if(fp!=NULL) 
				{
					long file_len = 0, read_len = 0, tmp_len = 0;
					fseek(fp, 0, SEEK_END);
					file_len = ftell(fp);
					if (file_len > 0) 
					{
						fseek(fp, 0, SEEK_SET);
						while (read_len <file_len)  
						{
							tmp_len = fread(tmpbuff, sizeof(char), 512, fp);
							if (tmp_len <= 0) break;
                            for(int t=0;t<tmp_len;t++)
							{
								tx = &G_Channel.chan_data[i].sentx;
                                uchar val;
								val=tmpbuff[t];
								EnterTrnQ(tx,val);//tx->buf[tx->tailP] = val;tx->tailP = (tx->tailP + 1) % TRNLength;
							}
							WriteAChan(i,SENDCHAN);
						    read_len += tmp_len;
							if ((feof(fp) != 0) || (ferror(fp) != 0)) break;
						}
					}
					fclose(fp);
					remove(str);
				}				
			}
		}
		Sleep(40);
    }
    return NULL;
}

int afert_init()
{
 	uint	mainCount=0;

	if( !initData ()) return 0;//初始化结果数据

	id1 = AfxBeginThread(openproc,0);
	id  = AfxBeginThread(rwproc,  0);
	id2  =AfxBeginThread(afertproc,  0);
	id3  =AfxBeginThread(FrontProcess,  0);
    //id4 =AfxBeginThread(deletefile,0);
   	Sleep(1000);
	return 1;
} 

void Exit_Clear()
{
	DWORD code=0;

	if(id && GetExitCodeThread(id->m_hThread,&code)){
		if(code==STILL_ACTIVE) {
			TerminateThread(id->m_hThread,0);
			id = (CWinThread *)NULL;
		}
	}

	if(id1 && GetExitCodeThread(id1->m_hThread,&code)){
		if(code==STILL_ACTIVE) {
			TerminateThread(id1->m_hThread,0);
			id1 = (CWinThread *)NULL;
		}
	}

	if(id2 && GetExitCodeThread(id2->m_hThread,&code)){
		if(code==STILL_ACTIVE) {
			TerminateThread(id2->m_hThread,0);
			id2 = (CWinThread *)NULL;
		}
	}
	if(id3 && GetExitCodeThread(id3->m_hThread,&code)){
		if(code==STILL_ACTIVE) {
			TerminateThread(id3->m_hThread,0);
			id3 = (CWinThread *)NULL;
		}
	}
    if(id4 && GetExitCodeThread(id4->m_hThread,&code)){
		if(code==STILL_ACTIVE) {
			TerminateThread(id4->m_hThread,0);
			id4 = (CWinThread *)NULL;
		}
	}
    CloseAllChan();
}

⌨️ 快捷键说明

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