📄 libipvlbi_board.h
字号:
//printf("after c2,rv3,rv2 %d %d %d\n",c2,rv3,rv2); // debug c2=rv3; //printf("final c2,rv3,rv2 %d %d %d\n",c2,rv3,rv2); //debug } } else { if(mode !=7 ){ rv2=write(fd_out_tmp,buf,rv); } } } if(endflg == 1) goto loopexit; }loopexit: /* sampling stop */ rv = ioctl(fd_in, TDSIO_SAMPLING_STOP); if (rv == -1) { perror("ioctl() stop sampling failed "); goto end; } else { printf("ioctl() stop sampling succeed\n"); } end: close(fd_in); if(mode !=7 ){ if(fmode == 2 || fmode ==3 || fmode == 4){ if(fvalid == 1){ fpos=nfile-1; if(fpos < 0) fpos=fpos+nfilemax; /* if(remove(filetable[fpos]) <0) perror("File remove error"); */ /* just rename */ // if (rename(tmplast,filetable[fpos]) <0) // perror("Rename tmplast error"); /* Copy file to leave last file name */ fd_out = open(filetable[fpos], O_WRONLY | O_CREAT | O_TRUNC, ofilemode); if (fd_out == -1) { perror("output file open() error"); goto end2; } if((fin = fopen(tmplast, "rb"))==NULL){ perror("Data file open error!\n"); goto end2; } while(1){ rv = fread(buf,1,nbyte,fin); if (rv == -1) { perror("read() failed "); goto end2; } rv2=write(fd_out,buf,rv); if (feof(fin) !=0) // Reach EOF { fclose(fin); goto exit2; } } exit2:; close(fd_out); } } } //close(fd_out); //close(fd_out_tmp); //if (rename(tmpfile1,tmpfile2) <0) // perror("Rename error"); /* 1sec data file merges into one file big file */ if(fmode == 4 || mode ==5) { printf("File merging .....\n"); if(mode == 5) { // for debug strcpy(outfile2,dirtemp); strcat(outfile2,fdebug); } else { strcpy(outfile2,outfile); } printf("File merging into %s\n",outfile2); fd_out = open(outfile2, O_WRONLY | O_CREAT | O_TRUNC, ofilemode); if (fd_out == -1) { perror("output file open() error"); goto end2; } for(i=0; i<nfile; i++){ strcpy(tmpfile1,filetable[i]); printf("%d %d : %s\n",nfile,i+1,tmpfile1); if((fin = fopen(tmpfile1, "rb"))==NULL){ perror("Data file open error!\n"); goto end2; } while(1){ rv = fread(buf,1,nbyte,fin); if (rv == -1) { perror("read() failed "); goto end2; } rv2=write(fd_out,buf,rv); if (feof(fin) !=0) // Reach EOF { fclose(fin); goto exit1; } } exit1:; } close(fd_out); } end2:; return rv;}int sampleget3(char* outfile, int span, int sfreq, int adbit, int numch, char* dirtemp, int mode){/* sampleget2丂偺夵椙斉 mode -- 僼傽僀儖弌椡儌乕僪 1: 堦妵僼傽僀儖偺傒丂乮僨僼僅儖僩乯 2: 侾昩僨乕僞僼傽僀儖偺傒 3: 堦妵僼傽僀儖亄侾昩偛偲僼傽僀儖摨帪 4: 侾昩僨乕僞僼傽僀儖弌椡丄娤應屻堦妵僼傽僀儖嶌惉 5: 3+4 for debug purpose 堦妵僼傽僀儖柤偼 debug.dat 嶌惉 6: 侾昩僨乕僞僼傽僀儖偺傒 (帪娫惂尷柍偟乯 7: 俇偲摨偠乮偨偩偟僼傽僀儖弌椡柍偟乯僔僗僥儉僠僃僢僋梡*/ char dev[] = "/dev/tds0"; int fd_in; int fd_out, fd_out_tmp; mode_t ofilemode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH; int rv, rv2, rv3, rv2s; unsigned int time,day,sec; // 僨僶僀僗偐傜撉傒崬傫偩尰嵼偺帪崗忣曬傪奿擺 int i,h,m,s; int secforce; int headbit=64; // header bit number for a new sampler board //int headbit=32; // header bit number for a old sampler board long int allbyte; // 僒儞僾儕儞僌憤僶僀僩悢 long int bitsec; // 侾昩偁偨傝偺價僢僩悢丂乮僿僢僟乕俇係價僢僩傪娷傓乯 long int bytesec; // 侾昩偁偨傝偺僶僀僩悢丂乮僿僢僟乕俇係價僢僩傪娷傓乯 long int c, c2; unsigned int sampling1, freq, width; int ch; int sec_counter; // second counter for tempolary file creation int sec_counter_max; int fmode; // file out mode int endflg, nbyte, nfile; char* namnow="sampler.now"; //1sec file name sampling out now char* namlast="sampler.dat"; //1sec file name sampling out just finished char tmpnow[80],tmplast[80]; char tmpfile1[80],tmpfile2[80], outfile2[80]; char fname1[20],fname2[20]; char filetable[1000][80]; // 1sec file name container int nfilemax; int fvalid; // 1 sec file number exceeds 3 then set 1 int fpos; int ierr; unsigned char bit64[8]; // for header check int hspatn, hiok, hsmplbit, hsfreq, hnumch, hhh,hmm,hss; long int hseconds; char* fdebug="debug.dat"; FILE *fin; u_int8_t sfreq_t, sbit_t, numch_t; u_int8_t sampling; u_int32_t buf[BUFSIZ * 2]; nfilemax=1000; sec_counter_max=1000; // sec_counter reset if exceeds this value fvalid=0; //printf("BUFSIZ %d\n",BUFSIZ); // BUFSIZ偼1024偱偟偨両/* printf(" span = %d\n",span); // debug printf(" sfreq= %d\n",sfreq); // debug printf(" adbit= %d\n",adbit); // debug printf(" numch= %d\n",numch); // debug*/ /* File out mode check */ if(mode <=0 || mode > 4) { fmode=1; } else { fmode=mode; } if(mode == 5) { fmode=3; } if(mode == 6 || mode ==7 ) { fmode=2; nfilemax=10; sec_counter_max=10; } /* sampling duration check */ if (span <= 0) { printf(" parameter span (duration in sec) should be positive!\n"); // nothing to do return -1; } /* output file open */ if(fmode == 1 || fmode ==3){ fd_out = open(outfile, O_WRONLY | O_CREAT | O_TRUNC, ofilemode); if (fd_out == -1) { perror("output file open() error"); goto end; } } sec_counter=0; /* tempolary file name creation */ strcpy(tmpnow,dirtemp); strcpy(tmplast,dirtemp); strcat(tmpnow,namnow); strcat(tmplast,namlast); //sprintf(fname1,"smp%04d.now\0",sec_counter); sprintf(fname2,"smp%04d.dat\0",sec_counter); //strcat(tmpfile1,fname1); strcpy(tmpfile2,dirtemp); strcat(tmpfile2,fname2); /* tempolary output file open */ if(mode !=7 ){ if(fmode == 2 || fmode ==3 || fmode == 4){ fd_out_tmp = open(tmpnow, O_WRONLY | O_CREAT | O_TRUNC, ofilemode); if (fd_out_tmp == -1) { perror("temprary output file open() error"); goto end; } } } /* sampling set and start sampling */ if((rv=ini_and_start_sampler(&fd_in, sfreq, adbit, numch, &bitsec))<0) { goto end2; } //if((rv=ini_sampler(&fd_in, sfreq, adbit, numch, &bitsec))<0) { // goto end2; //} bytesec=bitsec/8; allbyte=bytesec*span; /* File maximum bytes check */ if(mode !=6 && mode !=7 ){ if (allbyte > FILEMAXSIZE || allbyte < 0){ allbyte= FILEMAXSIZE; secforce=allbyte/bytesec; allbyte=secforce*bytesec; printf(" # of bytes force to set to %d Bytes because it exceeds %d Bytes\n", allbyte,FILEMAXSIZE); // debug printf(" Obserbasation span (sec) reduced to %d\n",secforce); // debug } } printf(" bitsec = %d\n",bitsec); // debug printf(" bytesec = %d\n",bytesec); // debug printf(" allbyte = %d\n",allbyte); // debug //rv=start_sampler(&fd_in); //if(rv < 0) goto end; c = 0; c2= 0; endflg=0; nfile=0; nbyte = sizeof(buf); // nbyte = 8192 //while (c < BUFSIZ * BUFSIZ) { while (1) { rv = read(fd_in, buf, nbyte); if (rv == -1) { perror("read() failed "); goto end; } if(rv != nbyte) {printf("read byte # difference %d\n",rv);} //printf(" nbyte = %d\n",nbyte); // debug /* monitor 1st 64bit data */ if(c == 0) { printf("%x %x ",*(buf), *(buf+1)); headerchkn((unsigned char*)(buf),&hiok,&hspatn,&hsmplbit,&hsfreq,&hnumch, &hseconds,&hhh,&hmm,&hss); if (hiok == 0) { // sync detection printf("OK "); printf("%dbit %dch %dkHz",hsmplbit,hnumch, hsfreq); printf(" %02d:%02d:%02d %d\n",hhh,hmm,hss,hseconds); } else { printf("First 64bit is not header block!\n"); } } c += rv; // already read byte number //printf(" nbyte,rv,c %d %d %d\n",nbyte,rv,c); // debug if(mode !=6 && mode !=7 ){ if( c >= allbyte ){ rv3=c-allbyte; rv2=rv-rv3; endflg=1; // end flag set if(fmode == 1 || fmode ==3){ rv2 = write(fd_out, buf, rv2); close(fd_out); } } else { if(fmode == 1 || fmode ==3){ rv = write(fd_out, buf, rv); } } } if(fmode == 2 || fmode ==3 || fmode == 4){ c2+=rv; if(c2 >= bytesec) { rv3=c2-bytesec; rv2=rv-rv3; //printf("1st c2,rv3,rv2 %d %d %d\n",c2,rv3,rv2); // debug if(mode !=7 ){ rv2=write(fd_out_tmp,buf,rv2); /* tempolary file update */ close(fd_out_tmp); if(fvalid == 1){ fpos=nfile-1; if(fpos < 0){ fpos=fpos+nfilemax;} //printf("%d %s\n",fpos,filetable[fpos]); /* folowing is not necessary for FreeBSD */ /* if((ierr=remove(filetable[fpos])) <0){ if(ierr != -1 ) perror("File remove error"); } */ if (rename(tmplast,filetable[fpos]) <0) { perror("Rename tmplast error"); } }else{ if(nfile >=0) fvalid=1; } /* folowing (remove) is not necessary for FreeBSD */ /* if((ierr=remove(tmplast)) <0){ if(ierr != -1 ) perror("File remove error"); } */ if (rename(tmpnow,tmplast) <0){ perror("File tmpnow rename error"); } } strcpy(filetable[nfile],tmpfile2); // save file name info //printf("nfile %d %s %s\n",nfile,filetable[nfile],tmpfile2); nfile++; if(nfile >= nfilemax) nfile=0; /* new templary file nmae creation */ sec_counter++; if(sec_counter >= sec_counter_max) sec_counter=0; //sprintf(fname1,"smp%04d.now\0",sec_counter); sprintf(fname2,"smp%04d.dat\0",sec_counter); //strcpy(tmpfile1,dirtemp); //strcat(tmpfile1,fname1); strcpy(tmpfile2,dirtemp); strcat(tmpfile2,fname2); if(endflg !=1 ){ if(mode !=7 ){ /* tempolary output file open */ //fd_out_tmp = open(tmpfile1, O_WRONLY | O_CREAT | O_TRUNC, ofilemode); fd_out_tmp = open(tmpnow, O_WRONLY | O_CREAT | O_TRUNC, ofilemode); if (fd_out_tmp == -1) { perror("temprary output file open() error"); goto end; } } /* rest of data write */ if(rv3 > 0) { rv2s=rv2/sizeof(u_int32_t); if(mode !=7 ){ rv3=write(fd_out_tmp,buf+rv2s,rv3); } /* monitor 1st 64bit of second data */ printf("%x %x ",*(buf+rv2s), *(buf+rv2s+1)); /* synck pattern check */ //strncpy(bit64,(char *)(buf+rv2s),8); //headerchkn(bit64,&hiok,&hspatn,&hsmplbit,&hsfreq,&hnumch, headerchkn((unsigned char*)(buf+rv2s),&hiok,&hspatn,&hsmplbit,&hsfreq,&hnumch, &hseconds,&hhh,&hmm,&hss); if (hiok == 0) { // sync detection printf("OK "); printf("%dbit %dch %dkHz",hsmplbit,hnumch, hsfreq); printf(" %02d:%02d:%02d %d\n",hhh,hmm,hss,hseconds); } else { printf("First 64bit is not header block!\n"); /* check previous data at least 10 times*/ } } //printf("after c2,rv3,rv2 %d %d %d\n",c2,rv3,rv2); // debug c2=rv3; //printf("final c2,rv3,rv2 %d %d %d\n",c2,rv3,rv2); //debug } } else { if(mode !=7 ){ rv2=write(fd_out_tmp,buf,rv); } } } if(endflg == 1) goto loopexit; }loopexit: /* sampling stop */end: rv = stop_sampler(&fd_in); if(mode !=7 ){ if(fmode == 2 || fmode ==3 || fmode == 4){ if(fvalid == 1){ fpos=nfile-1; if(fpos < 0) fpos=fpos+nfilemax; /* if(remove(filetable[fpos]) <0) perror("File remove error"); */ /* just rename */ // if (rename(tmplast,filetable[fpos]) <0) // perror("Rename tmplast error"); /* Copy file to leave last file name */ fd_out = open(filetable[fpos], O_WRONLY | O_CREAT | O_TRUNC, ofilemode); if (fd_out == -1) { perror("output file open() error"); goto end2; } if((fin = fopen(tmplast, "rb"))==NULL){ perror("Data file open error!\n"); goto end2; } while(1){ rv = fread(buf,1,nbyte,fin); if (rv == -1) { perror("read() failed "); goto end2; } rv2=write(fd_out,buf,rv); if (feof(fin) !=0) // Reach EOF { fclose(fin); goto exit2; } } exit2:; close(fd_out); } } } //close(fd_out); //close(fd_out_tmp); //if (rename(tmpfile1,tmpfile2) <0) // perror("Rename error"); /* 1sec data file merges into one file big file */ if(fmode == 4 || mode ==5) { printf("File merging .....\n"); if(mode == 5) { // for debug strcpy(outfile2,dirtemp); strcat(outfile2,fdebug); } else { strcpy(outfile2,outfile); } printf("File merging into %s\n",outfile2); fd_out = open(outfile2, O_WRONLY | O_CREAT | O_TRUNC, ofilemode); if (fd_out == -1) { perror("output file open() error"); goto end2; } for(i=0; i<nfile; i++){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -