/* ################# config.c ################# copyrights: co. Elmar Benninghaus 78628 Rottweil create: 10.02.2004 last work: 10.02.2004 Version: 1.0 */ #include #include #include #include #include #include #include #include #include #include #include #include #include "setbaud.c" unsigned char *buffer = NULL; FILE *infile = NULL; FILE *outfile = NULL; int serialdevice = 0; long filelen = 0L; unsigned char serinbuf[132]; unsigned char seroutbuf[132]; void waitmillisecs( long ms ) { struct timeb tpa; long t; time_t sec; unsigned short msec; if(ms <= 0){ return; } ftime(&tpa); sec = tpa.time; msec = tpa.millitm; t = 0; while( t < ms ){ ftime(&tpa); if( ( msec != tpa.millitm ) || ( sec != tpa.time ) ){ t++; sec = tpa.time; msec = tpa.millitm; } } } void endAll(char *msg) { if(buffer != NULL)free(buffer); if(infile != NULL)fclose(infile); if(outfile != NULL)fclose(outfile); if(serialdevice > 0 )close( serialdevice ); if(msg != NULL)fprintf(stdout,"\n%s",msg); fprintf(stderr,"\nSERCTRL - END\n"); exit(0); } long getFileLen(FILE *fp) { long len; rewind(fp); fseek(fp,0L,SEEK_END); len = ftell(fp); return(len); } void serAck( void ) { // waitmillisecs(10); seroutbuf[0] = 0x0a; seroutbuf[1] = 0; while(write(serialdevice,seroutbuf,1)<= 0){ ; } } void serOut( void ) { int len,ct,flag; // waitmillisecs(10); ct = 0; flag = 0; while( ct < 130 ){ if( seroutbuf[ct] == 0x22 ){ if( flag ){ flag = 0; } else { flag = 1; } ct++; continue; } if( seroutbuf[ct] == 0x20 ){ if( ! flag ){ ct++; continue; } } if( seroutbuf[ct] == 0x0d){ seroutbuf[ct] = 0x0a; } if( seroutbuf[ct] == 0x00){ seroutbuf[ct] = 0x0a; } while(write(serialdevice,&seroutbuf[ct],1)<= 0){ ; } if( seroutbuf[ct] == 0x0a){ break; } ct++; } } /* if func returns 1, only an acknowledge ( 0x0a ) is returned, otherwise the len of received bytes, including the 0x0a at end. */ int serIn( void ) { int ct; ct = 0; while( ct < 130 ) { while( read(serialdevice,&serinbuf[ct],1) <= 0 ){ ; } if( serinbuf[ct] == 0x0a ){ ct++; serinbuf[ct] = 0; return(ct); } ct++; } serinbuf[ct] = 0; } int main(int argc, char *argv[]) { int nr,hexfile; if(argc < 3){ fprintf(stdout,"\nserctrl devicename baud filename [outfilename]\n"); exit(0); } fprintf(stderr,"\nSERCTRL STARTS\n"); if( ( serialdevice = serialopen( argv[1], argv[2], 0 ) ) == 0 ){ endAll("Cannot open device"); } if( ( infile = fopen( argv[3],"rt" ) ) == NULL ){ endAll("Cannot open infile"); } if( argv[4] != NULL ){ if( ( outfile = fopen( argv[4],"wt" ) ) == NULL ){ endAll("Cannot open outfile"); } } hexfile = 0; while( feof( infile ) == 0 ){ if(fgets( seroutbuf,130,infile ) == NULL){ break; } if( seroutbuf[0] == ':' ){ hexfile = 1; } if( hexfile ){ if( seroutbuf[0] != ':' ){ break; } } if( seroutbuf[0] == ';' ){ continue; } if( seroutbuf[0] == '#' ){ continue; } if( seroutbuf[0] == 'X'){ serOut(); endAll("READY"); } if( seroutbuf[0] == 'x'){ serOut(); continue; } serOut(); while( serIn() > 1 ){ if( outfile != NULL ){ fprintf( outfile, serinbuf ); } serAck(); } } endAll("READY"); exit(0); }