自己近期写的一个串口通信的小程序

源代码在线查看: file.c

软件大小: 40 K
上传用户: xiaotiao
关键词: 串口通信 程序
下载地址: 免注册下载 普通下载 VIP

相关代码

				#include 
				#include 
				#include 
				
				void test()
				{
				
				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 ";
				
					/*if( NULL != (ini  = fopen( "boot.ini", "r" )) )
				    {
				  printf( "File  opened\n" ); 
				fclose(ini);
				}
				else
				{
					printf( "File could not be opened\n" );
				}*/
				
				if( NULL != (stream  = fopen( "boot.ini", "r" )) )
				{
				     printf( "File(read)  opened\n" ); 
				     
				     numread=fread(gpara,sizeof(char),180,stream);
				     printf( "Number of items read = %d\n", numread );
				     printf( "Contents of buffer = %.125s\n", gpara );
				
				
				
				     /*格式转换*/
				     /*  [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
				
						  answer 192.168.1.105:255.255.255.0,192.168.2.105:255.255.0,1\0
					 */
				       fclose( stream );
				}
				else
				     printf( "File could not be opened\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" );
				
				
				
				
				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)							 {
							 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" );
				
				
				
				}
							

相关资源