www.pudn.com > tyRecv.rar > tyRecv.c


#include  
#include  
#include  
#include  
#include  
 
int tyRecv( int fd ) 
{ 
 
FILE *stream; 
int numread,numwritten,k,mark1; 
 
char buf[100]="set 192.168.1.105:255.255.255.0,192.168.2.105:255.255.255.0,0"; 
char wbuf[200]; 
char *bp; 
char *wp; 
 
char para[100]; 
char gpara[200]; 
char *pa; 
char *gp; 
 
char temp[20]; 
 
char writ1[]="[network1]"; 
char writ2[]="\n[network2]"; 
char writ3[]="\n[canconfig]"; 
char show1[]="\n ip = "; 
char show2[]="\n mask = "; 
char show3[]="\n id = "; 
 
char writ0[]="answer "; 
 
 
 
 
 
//char gpara[200]="answer 192.168.1.105:255.255.255.0,192.168.2.105:255.255.0,1"; 
char spara[200]="ok"; 
 
char c; 
int rs; 
 
char *rp; 
//char buf[100]; 
int i,mark; 
 
 
 
 
 
    printf("tyRecv:"); 
	printf("%d\n",fd); 
 
	i =0; 
       mark=0; 
			bzero(buf,sizeof(buf)); 
			c = '\n'; 
			printf("'\n' = %d",c); 
  while(1) 
  { 
	rs = read(fd,&c,1); 
	if(1==rs) 
	{ 
	  if(('g' == c) || ('G'==c)||('s'==c)||('S'==c)) 
	     mark=1; 
	  if(mark==1) 
	  { 
		 while(c != 0) 
		{  
//	 		printf("i= %d,c= %c\n",i,c); 
			buf[i++]= c; 
	//		printf("(if)the string:%s\n",buf); 
	        rs=read(fd,&c,1); 
		} 
		if(c==0) 
		{ 
						printf("i= %d, buf = %s\n",i,buf); 
						i=0; 
			mark=0; 
 
			if((buf[0]=='g') && (buf[1]=='e')&& (buf[2]=='t')) 
			{ 
			 
                /*if( NULL != (stream  = fopen( "boot.ini", "r" ))) 
                { 
					numread=fread(gpara,sizeof(char),200,stream); 
				    printf( "Number of items read = %d\n", numread ); 
                    printf( "Contents of buffer = %.100s\n", gpara ); 
 
                     
 
 
                    fclose( stream ); 
				} 
                else 
                    printf( "File could not be opened\n" );*/ 
				if( NULL != (stream  = fopen( "boot.ini", "r" )) ) 
                { 
                     printf( "read the writed file\n" );  
      
                     numread=fread(gpara,sizeof(char),180,stream); 
                     printf( "Number of items read = %d\n", numread ); 
                     printf( "Contents of buffer = %.150s\n", gpara ); 
 
	                 /*read 格式处理*/ 
                     pa=para; 
	                 gp=gpara; 
	                 strcpy(pa,writ0); 
                     k=0; 
	                 mark1=0; 
	                 while((*gp)!='\0') 
	                 { 
		                gp++; 
		                if((*gp)=='.') 
		                { 
			               temp[k]=*gp; 
			               k++; 
		                } 
 
		                if(mark1==1) 
		                { 
			                if((*gp)>='0'&&(*gp)<='9') 
			                { 
			                    temp[k]=*gp; 
			                    k++; 
		                     } 
 
		                } 
 
		                if(*gp=='m') 
		                { 
                              temp[k]=':'; 
			                  temp[k+1]='\0'; 
			                  strcat(pa,temp); 
			                  k=0; 
		                } 
 
		                if(*gp=='=') 
			                 mark1=1; 
 
		                if(*gp=='[') 
		                { 
			                  temp[k]=','; 
			                  temp[k+1]='\0'; 
			                  strcat(pa,temp); 
			                  k=0; 
			                  mark1=0; 
		                } 
 
	                 } 
	                      temp[k]=*gp; 
	                      strcat(pa,temp); 
 
	                      printf( "Contents of final  = %s\n", para ); 
 
     /*格式转换*/ 
     /*  [network1] 
          ip = 192.168.24.105 
          mask = 255.255.255.0 
         [network2] 
          ip = 192.168.25.105 
          mask = 255.255.255.0 
         [canconfig] 
          id = 0 
	 */ 
                    fclose( stream ); 
                } 
                else 
                    printf( "File could not be opened\n" ); 
 
 
			  
 
			 
				rs =write( fd, para, sizeof(para)); 
	           	printf("rs =%d,the para:%s\n",rs,para); 
			} 
			else if((buf[0]=='s') && (buf[1]=='e')&& (buf[2]=='t')) 
			{ 
				  
                /*if((stream = fopen( "boot.ini", "w" )) != NULL) 
				{ 
				     numwritten = fwrite( buf, sizeof( char ), 100, stream ); 
                     printf( "Wrote %d items\n", numwritten ); 
 
 
                     fclose( stream ); 
 
                } 
				else 
                     printf( "Problem opening the file\n" );*/ 
				if((stream = fopen( "boot.ini", "w" )) != NULL) 
                { 
	                   printf( "File(write)  opened\n" );  
		               //write格式处理 
		               bp=buf; 
		               wp=wbuf; 
         
		               bp=bp+3; 
    	               while((*bp)==' ') 
		                 	bp++; 
		               if((*bp)!=' ') 
	                   { 
			              strcpy(wp,writ1); 
			               //get network1 ip 
			               k=0; 
			              while((*bp)!=':') 
			              { 
				              temp[k]=*bp; 
				              k++; 
				              bp++; 
			              } 
			              bp++; 
			              temp[k]='\0'; 
			              k=0; 
			              strcat(wp,show1); 
			              strcat(wp,temp); 
 
                          //get network1 mask 
                          while((*bp)!=',') 
                          { 
				                temp[k]=*bp; 
				                k++; 
				                bp++; 
			              } 
			              bp++; 
			              temp[k]='\0'; 
			              k=0; 
			              strcat(wp,show2); 
                          strcat(wp,temp); 
 
                          strcat(wp,writ2); 
			              //get network2 ip 
			              while((*bp)!=':') 
			              { 
				             temp[k]=*bp; 
				             k++; 
				             bp++; 
			              } 
			              bp++; 
			              temp[k]='\0'; 
			              k=0; 
                          strcat(wp,show1); 
			              strcat(wp,temp); 
 
                          //get network2 mask 
                          while((*bp)!=',') 
                          { 
				              temp[k]=*bp; 
				              k++; 
				              bp++; 
			              } 
			              bp++; 
			              temp[k]='\0'; 
			              k=0; 
			              strcat(wp,show2); 
                          strcat(wp,temp); 
 
                          strcat(wp,writ3); 
			               //get canconfig id 
			              while((*bp)!='\0') 
			              { 
				             temp[k]=*bp; 
				             k++; 
				             bp++; 
			              } 
			              temp[k]=*bp; 
			              temp[k]='\0'; 
			              k=0; 
			              strcat(wp,show3); 
                          strcat(wp,temp);          
 
		               }	 
		 
		               numwritten = fwrite( wbuf, sizeof( char ), 180, stream ); 
                       printf( "Wrote %d items\n", numwritten ); 
                       // set 192.168.1.105:255.255.255.0,192.168.2.105:255.255.0,0  
         
 
                       fclose( stream ); 
 
                 } 
                else 
                       printf( "Problem opening the file\n" ); 
 
 
 
 
 
					rs =write( fd, spara, sizeof(spara)); 
		           	printf("rs =%d,the spara:%s\n",rs,spara); 
			} 
	  		bzero(buf,sizeof(buf)); 
 
		} 
		if(i>=100) 
		{ 
			 i=0; 
			 mark=0; 
 			bzero(buf,sizeof(buf)); 
		} 
	  } 
	} 
  } 
 
	 
taskDelay(1) ; 
 
} 
 
int testMain() 
{ 
int rs =0; 
int i; 
int fd; 
printf("Com Test:\n"); 
/* Open COM1*/ 
 
fd = open("/tyCo/0",O_RDWR,0) ; 
 
if (ERROR==fd) 
{ 
printf("can not open device!\n") ; 
return 0 ; 
} 
/* Set baudrate*/ 
/*if ( ERROR==ioctl(fd,FIOBAUDRATE,9600) ) 
{ 
printf("can not set BAUDRATE!\n") ; 
return 0 ; 
} 
 
rs = ioctl(fd,FIOSETOPTIONS,OPT_RAW); 
if(rs == ERROR ) 
{ 
printf("can not set OPT_RAW!\n") ; 
return 0 ; 
} 
 
rs = ioctl(fd,SIO_HW_OPTS_SET, CS8 | PARENB | PARODD); 
if(rs == ERROR ) 
{ 
printf("can not set OPTS!\n") ; 
return 0 ; 
}*/ 
 
 /*Start receiving task*/ 
tyRecv( fd ); 
//rs = taskSpawn("tComRecv",60,VX_FP_TASK,1024*40,(FUNCPTR)tyRecv,fd,0,0,0,0,0,0,0,0,0) ; 
 
printf("taskSpawn = %d\n ",rs); 
}