⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 libipvlbi_board.h

📁 实现ipvlbi数据记录
💻 H
📖 第 1 页 / 共 4 页
字号:
			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 sampleget4(char* outfile, int span, int sfreq, int adbit, int numch, char* dirtemp, int mode){/*      sampleget3 偺夵椙斉  (愨偊偢僔儞僋僷僞乕儞偺専弌傪峴偆乯                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;	long int byteinter;   // inter header byte counter	int rv32;	int bytepos; //  byte position for start of sync pattern	int fmode;         // file out mode	int endflg, nbyte, nfile;	int flag1sec;     // 1sec (sync) detection flag  1:detected	int flag1st;	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, seconds, secold;	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], lastbuf;		nfilemax=1000;	sec_counter_max=1000;  // sec_counter reset if exceeds this value	fvalid=0;	flag1sec=0;	flag1st=0;	bytepos=0;	byteinter=0;	//lastbuf = (u_int32_t *)malloc(sizeof(u_int32_t));	//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 (1 big 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 (for 1 sec data write 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;    }     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;   // byte counter for one big file	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				/* sync pattern check */		rv32=rv/sizeof(u_int32_t);		if(c > 0) {			if(lastbuf==0xFFFFFFFF && ((buf[0] & 0xFF000000)==0x8B000000)) { // this is sync pattern				seconds=(long int)(buf[0] & 0x1FFFF);				printf("Sync detected  byteinter %d  seconds %d\n",byteinter, seconds);				if( secold+1 != seconds){ // time error					if((secold != 86399) && (seconds != 0)){					     printf("Time increment error!\n");					}				}				if(bytesec != byteinter){					printf("%d bytes data missed\n",bytesec-byteinter);				}				bytepos=-4;				flag1sec=1;				secold=seconds;				byteinter=0;			} 			byteinter+=4;		}					for(i=0; i<rv32-1; i++){			if(buf[i]==0xFFFFFFFF && ((buf[i+1] & 0xFF000000)==0x8B000000)) { // this is sync pattern				seconds=(long int)(buf[i+1] & 0x1FFFF);				printf("Sync detected  byteinter %d  seconds %d \n",byteinter, seconds);				if( c > 0 && ( secold+1 != seconds)){ // time error					if((secold != 86399) && (seconds != 0)){						printf("Time increment error!\n");					}				}				if(c > 0 && (bytesec != byteinter)){					printf("%d bytes data missed\n",bytesec-byteinter);				}				bytepos=i*sizeof(u_int32_t);				flag1sec=1;				secold=seconds;				byteinter=0;			} 			byteinter+=4;		}		lastbuf=buf[rv32-1];		/*		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);				}			}		} else {			c=1;   // for inifinite operation for mode=6 or =7		}				/* every 1 sec file creation */		if(fmode == 2 || fmode ==3 || fmode == 4){			if(flag1st ==1 && flag1sec ==1 ) {				rv3=rv-bytepos;				rv2=bytepos;				//printf("1st c2,rv3,rv2   %d %d %d\n",c2,rv3,rv2);  // debug				if(mode !=7){					if(bytepos > -4 ) {						if(flag1st != 0) c2=write(fd_out_tmp,&lastbuf,4);					}					if(bytepos > 0 ) {					     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(bytepos > 0) {						rv2s=rv2/sizeof(u_int32_t);						if(mode !=7 ){							rv3=write(fd_out_tmp,buf+rv2s,rv3-4);						}					} else {						if(mode !=7 ){							rv3=write(fd_out_tmp,buf,rv-4);						}					}						//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 ){					if(flag1st !=0) c2=write(fd_out_tmp,&lastbuf,4);					rv2=write(fd_out_tmp,buf,rv-4);				}			}			flag1sec=0; //flag reset		}		flag1st=1;		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++){			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 ip_board_time(unsigned int *day, unsigned int *sec, int *hh, int *mm, int *ss){     int fd_in,rv;	 unsigned int time;	 fd_in = open("/dev/tds0", O_RDONLY);  //僨僶僀僗偺僆乕僾儞	 if (fd_in == -1) {		perror("device open() error ");		return -1;	 }	 //僨僶僀僗偐傜帪崗忣曬偺庢摼 	 rv = ioctl(fd_in, TDSIO_GET_TIME, &time);     if (rv == -1) {		 perror("ioctl() time get failed ");		 return -1;	 }     *day=TDS_GET_DAYS(time);     *sec=TDS_GET_SEC(time);     sec2hms(*sec,&(*hh),&(*mm),&(*ss));	close(fd_in);  // 俬俹亅倁俴俛俬儃乕僪僋儘乕僘	return 0;}#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -