2013年9月2日 星期一

撰寫一個程式使用批次檔自動執行

之前在一家觸控IC的設計公司上班時,寫了一的簡單的程式,用來重新排列IC測試機台所提供資料.

IC測試機台會提供數十個到至數百個.txt文字檔,每個文字檔的內容類似如下所示:

t001_log.txt
*********************************
0x00000000
0x00000001
0x00000002
0x00000003
0x00000000
0x00000001
0x00000002
0x00000003
0x00000000
0x00000001
.......以下還有
*********************************
由上述的範例來說,每筆資料是用16進位表示法的32bit的data,我只要取出每筆資料最後兩個bit的值,之後每10筆資料,在組成一筆data.
之後還要每個.txt檔案須取出第41~1320筆有效資料,第1至40筆資料與已忽略,超過1320筆資料也忽略掉.

1. main.c(主程式,編譯完成的執行檔為adc.exe )
*********************************
#include <stdio.h>
#include <stdlib.h>

char hex2ascii(char Number2)
{
 char outtxt;
 switch(Number2)
 {
 case 0x0:
  outtxt = '0';
  break;
 case 0x1:
  outtxt = '1';
  break;
 case 0x2:
  outtxt = '2';
  break;
 case 0x3:
  outtxt = '3';
  break;
 case 0x4:
  outtxt = '4';
  break;
 case 0x5:
  outtxt = '5';
  break;
 case 0x6:
  outtxt = '6';
  break;
 case 0x7:
  outtxt = '7';
  break;
 case 0x8:
  outtxt = '8';
  break;
 case 0x9:
  outtxt = '9';
  break;
 case 0xA:
  outtxt = 'A';
  break;
 case 0xB:
  outtxt = 'B';
  break;
 case 0xC:
  outtxt = 'C';
  break;
 case 0xD:
  outtxt = 'D';
  break;
 case 0xE:
  outtxt = 'E';
  break;
 case 0xF:
  outtxt = 'F';
  break;
 default:
  printf("?");
  break;
 }
 return outtxt;
}

char ascii2hex(char Number1)
{
 char chbit;
 switch(Number1)
 {
 case '0':
  chbit = 0x0;
  break;
 case '1':
  chbit = 0x1;
  break;
 case '2':
  chbit = 0x2;
  break;
 case '3':
  chbit = 0x3;
  break;
 case '4':
  chbit = 0x4;
  break;
 case '5':
  chbit = 0x5;
  break;
 case '6':
  chbit = 0x6;
  break;
 case '7':
  chbit = 0x7;
  break;
 case '8':
  chbit = 0x8;
  break;
 case '9':
  chbit = 0x9;
  break;
 case 'A':
  chbit = 0xA;
  break;
 case 'B':
  chbit = 0xB;
  break;
 case 'C':
  chbit = 0xC;
  break;
 case 'D':
  chbit = 0xD;
  break;
 case 'E':
  chbit = 0xE;
  break;
 case 'F':
  chbit = 0xF;
  break;
 case 'a':
  chbit = 0xA;
  break;
 case 'b':
  chbit = 0xB;
  break;
 case 'c':
  chbit = 0xC;
  break;
 case 'd':
  chbit = 0xD;
  break;
 case 'e':
  chbit = 0xE;
  break;
 case 'f':
  chbit = 0xF;
  break;
 default:
  printf("?");
  break;
 }
 return chbit;
}

int main(void)
{
 FILE *fs,*fd;
 char sfilename[20],dfilename[20];
 char data;
 char word8[8];
 char conversion[10][2]; //ADC0 ADC1
 unsigned short outword = 0;
 int adc0=0,adc1=0;

 char outputtxt = 0;
 char checkbit = 0;

 char buffer1[5] = {0,0,0,0,0};
 char buffer2[5] = {0,0,0,0,0};

 int filepointer;
 int sdatacount = 0;

 printf("Output source file name:");
 scanf("%s",dfilename);
 if((fd=fopen(dfilename,"w"))==NULL)
 {
  printf("\nObject file unable to open!");
  system("PAUSE");
  return 0;
 }
 
 while(1)
 {
  printf("\nInput source file name:");
  scanf("%s",sfilename);

  if((fs=fopen(sfilename,"r"))==NULL)
  {
   printf("\nSource file unable to open!");
   fclose(fd);
   system("PAUSE");
   return 0;
  }

  while((data=fgetc(fs))!=EOF) //read source file
  {
   int x=0x78;
   if(x != (x & data))
   {
    printf("%c",data);
   }
   else
   {
    printf("%c",data); //'x'
    *(word8+0) = fgetc(fs); 
    printf("%c",*(word8+0));
    *(word8+1) = fgetc(fs); 
    printf("%c",*(word8+1));
    *(word8+2) = fgetc(fs); 
    printf("%c",*(word8+2));
    *(word8+3) = fgetc(fs); 
    printf("%c",*(word8+3));
    *(word8+4) = fgetc(fs); 
    printf("%c",*(word8+4));
    *(word8+5) = fgetc(fs); 
    printf("%c",*(word8+5));
    *(word8+6) = fgetc(fs); 
    printf("%c",*(word8+6));
    *(word8+7) = fgetc(fs);
    printf("%c",*(word8+7));

    checkbit = ascii2hex(*(word8+7));

    if(adc0<10) //to array
    {
     *(*(conversion+adc0)+(adc1+0)) = checkbit & 0x1; //bit_0
     checkbit >>= 1;
     *(*(conversion+adc0)+(adc1+1)) = checkbit & 0x1; //bit_1
     adc1 = 0;
     adc0++;
    }
    sdatacount++;
   }
   if(adc0==10) //move array data
   {
    adc0 = 0;
    if((sdatacount > 40) && (sdatacount < 1321)) //taken out the data(1280)
    {
     //to object DEC file
     outword = *(*(conversion+0)+1) & 0x1; //ADC1
     outword <<= 1;
     outword = outword | (*(*(conversion+1)+1) & 0x1);
     outword <<= 1;
     outword = outword | (*(*(conversion+2)+1) & 0x1);
     outword <<= 1;
     outword = outword | (*(*(conversion+3)+1) & 0x1);
     outword <<= 1;
     outword = outword | (*(*(conversion+4)+1) & 0x1);
     outword <<= 1;
     outword = outword | (*(*(conversion+5)+1) & 0x1);
     outword <<= 1;
     outword = outword | (*(*(conversion+6)+1) & 0x1);
     outword <<= 1;
     outword = outword | (*(*(conversion+7)+1) & 0x1);
     outword <<= 1;
     outword = outword | (*(*(conversion+8)+1) & 0x1);
     outword <<= 1;
     outword = outword | (*(*(conversion+9)+1) & 0x1);
     itoa(outword,buffer1,10); //numerical value to character string
     fseek(fd,filepointer,SEEK_SET); //shift file pointer
     fputc(*(buffer1+0),fd); //word to new file
     fputc(*(buffer1+1),fd);
     fputc(*(buffer1+2),fd);
     fputc(*(buffer1+3),fd);
     fputc(0x09,fd); //Tab

     outword = *(*(conversion+0)+0) & 0x1; //ADC0
     outword <<= 1;
     outword = outword | (*(*(conversion+1)+0) & 0x1);
     outword <<= 1;
     outword = outword | (*(*(conversion+2)+0) & 0x1);
     outword <<= 1;
     outword = outword | (*(*(conversion+3)+0) & 0x1);
     outword <<= 1;
     outword = outword | (*(*(conversion+4)+0) & 0x1);
     outword <<= 1;
     outword = outword | (*(*(conversion+5)+0) & 0x1);
     outword <<= 1;
     outword = outword | (*(*(conversion+6)+0) & 0x1);
     outword <<= 1;
     outword = outword | (*(*(conversion+7)+0) & 0x1);
     outword <<= 1;
     outword = outword | (*(*(conversion+8)+0) & 0x1);
     outword <<= 1;
     outword = outword | (*(*(conversion+9)+0) & 0x1);
     itoa(outword,buffer2,10); //numerical value to character string
     fputc(*(buffer2+0),fd); //word to new file
     fputc(*(buffer2+1),fd);
     fputc(*(buffer2+2),fd);
     fputc(*(buffer2+3),fd);
     fputc(0x0A,fd); //line feed
     *(buffer1+0) = 0; //clean buffer
     *(buffer1+1) = 0;
     *(buffer1+2) = 0;
     *(buffer1+3) = 0;
     *(buffer2+0) = 0;
     *(buffer2+1) = 0;
     *(buffer2+2) = 0;
     *(buffer2+3) = 0;
    }
   }
   filepointer = ftell(fd); //read file pointer
  }
  filepointer = ftell(fd); //read file pointer
  fclose(fs);
  sdatacount = 0;
 }
 fclose(fd);
 printf("\nCopy OK!");
 system("PAUSE");
 return 0;

}
*********************************


2. commamd.txt(給批次檔執行的指令)
*********************************
out.txt
t001_log.txt
t002_log.txt
t003_log.txt
t004_log.txt
t005_log.txt
t006_log.txt
t007_log.txt
t008_log.txt
t009_log.txt
t010_log.txt
t011_log.txt
t012_log.txt
pause
*********************************


3. adc.bat(批次檔)
*********************************
@echo off
adc.exe < command.txt
pause
*********************************

最後只要使用滑鼠點選"adc.bat"檔,批次檔就會依照"commamd.txt"的順序將需要轉換的t00*_log.txt文字檔依序讓adc.exe執行,最後轉換的結果存檔成out.txt.

沒有留言:

張貼留言