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

📄 libipvlbi_board.h

📁 实现ipvlbi数据记录
💻 H
📖 第 1 页 / 共 4 页
字号:
		goto end;	}	else {		printf("ioctl() start sampling succeed\n");	}		printf("This is new version (2002-10-21) of sampleget\n");	c = 0;	dc=0.0;   // 2002-10-21	f_c=0;    // 2002-10-21	//while (c <=  allbyte) {	while (1) {                   // 2002-10-21	   int nbyte = sizeof(buf);   // nbyte = 8192	   rv = read(fd_in, buf, nbyte);	   if (rv == -1) {	      perror("read() failed ");	      goto end;	   }          //printf(" nbyte = %d\n",nbyte);   // debug	   rv = write(fd_out, buf, rv);	   c= c+ nbyte;	/* add on 2002-10-21 */		dc+=(double)nbyte;		if( dc > dallbyte) {   // sampling end			goto exithere;		}		if(c >  FILEMAXSIZE) { // newfile creation			c=0;			close(fd_out);			f_c++;			// new file name creation     XXXX.datN   where N=1,2,3......			strcpy(outname2,outfile);			sprintf(cbuf,"%d\0",f_c);			strcat(outname2,cbuf);			printf("outfile %d %s\n",f_c,outname2);  //debug            /* output file open */	        fd_out = open(outname2, O_WRONLY | O_CREAT | O_TRUNC, ofilemode);	        if (fd_out == -1) {		       perror("output file open() error");		       goto end;	       }		}				}exithere:       /* 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");	}	close(fd_out); end:	close(fd_in);	return rv;}						int sampleget2(char* outfile, int span, int sfreq, int adbit, int numch, char* dirtemp, int mode){/*                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;	}	/* sampler open */	fd_in = open(dev, O_RDONLY);	//fd_in = open("/dev/tds0", O_RDONLY);	if (fd_in == -1) {		perror("device open() error ");		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 frequency set */       if      (sfreq ==  40)  { sfreq_t =  TDS_SAMPLING_40KHZ; }       else if (sfreq == 100)  { sfreq_t =  TDS_SAMPLING_100KHZ; }       else if (sfreq == 200)  { sfreq_t =  TDS_SAMPLING_200KHZ; }       else if (sfreq == 500)  { sfreq_t =  TDS_SAMPLING_500KHZ; }       else if (sfreq ==   1)  { sfreq_t =  TDS_SAMPLING_1MHZ; }       else if (sfreq ==   2)  { sfreq_t =  TDS_SAMPLING_2MHZ; }       else if (sfreq ==   4)  { sfreq_t =  TDS_SAMPLING_4MHZ; }       else if (sfreq ==   8)  { sfreq_t =  TDS_SAMPLING_8MHZ; }       else if (sfreq ==  16)  { sfreq_t =  TDS_SAMPLING_16MHZ; }       else { 	   printf(" parameter sfreq  (sampling frequency) must be either one of followings:\n");	   printf("              40,100,200,500 (for kHz)\n");	   printf("              1,2,4,8,16 (for MHz)\n");          return -1;       }             /* AD resolution set */       if      (adbit ==  1)  { sbit_t =  TDS_SAMPLING_1BIT; }       else if (adbit ==  2)  { sbit_t =  TDS_SAMPLING_2BIT; }       else if (adbit ==  4)  { sbit_t =  TDS_SAMPLING_4BIT; }       else if (adbit ==  8)  { sbit_t =  TDS_SAMPLING_8BIT; }       else { 	   printf(" parameter adbit (A/D resolution) must be either one of followings:\n");	   printf("              1,2,4,8\n");          return -1;       }       /* number of channels */       if      (numch ==  1)  { numch_t =  TDS_SAMPLING_1CH; }       else if (numch ==  4)  { numch_t =  TDS_SAMPLING_4CH; }       else { 	   printf(" parameter numch (number of channels) must be either one of followings:\n");	   printf("              1,4\n");          return -1;       }       /* sampling parameters set */       sampling =  sfreq_t | sbit_t | numch_t;       //if (sfreq >= 40) { bitsec=sfreq*1000 + headbit ;}          //else { bitsec=sfreq*1000*1000 + headbit ;}       if (sfreq >= 40) { bitsec=sfreq*1000 ;}          //2002-9-15       else { bitsec=sfreq*1000*1000 ;}                 //2002-9-15     /* sampling byte number calc */		//bitsec=bitsec*adbit*numch;   // bit number per second		bitsec=(bitsec*adbit*numch)+headbit;   // bit number per second  !! 2002-9-15       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	//sampling = TDS_SAMPLING_4MHZ | TDS_SAMPLING_1BIT | TDS_SAMPLING_1CH;	rv = ioctl(fd_in, TDSIO_SET_SAMPLING, &sampling);	if (rv == -1) {	   perror("ioctl() sampling set failed ");	   goto end;	}	else {	   printf("ioctl() sampling set succeed\n");	}       /* get sampling parameters from a board */	rv = ioctl(fd_in,TDSIO_GET_SAMPLING,&sampling1);	if (rv == -1) {		perror("ioctl() get sampling parameter failed ");		goto end;	}	else {		printf("ioctl() get sampling parameter succeed\n");		freq = TDS_GET_SAMP_FREQ(sampling1);		width = TDS_GET_SAMP_WIDTH(sampling1);		ch = TDS_GET_SAMP_CH(sampling1);		if(freq == TDS_SAMPLING_40KHZ)			freq = 40;		if(freq == TDS_SAMPLING_100KHZ)			freq = 100;		if(freq == TDS_SAMPLING_200KHZ)			freq = 200;		if(freq == TDS_SAMPLING_500KHZ)			freq = 500;		if(freq == TDS_SAMPLING_1MHZ)			freq = 1000;		if(freq == TDS_SAMPLING_2MHZ)			freq = 2000;		if(freq == TDS_SAMPLING_4MHZ)			freq = 4000;		if(freq == TDS_SAMPLING_8MHZ)			freq = 8000;		if(freq == TDS_SAMPLING_16MHZ)			freq = 16000;		if(width == TDS_SAMPLING_1BIT)			width = 1;		if(width == TDS_SAMPLING_2BIT)			width = 2;		if(width == TDS_SAMPLING_4BIT)			width = 4;		if(width == TDS_SAMPLING_8BIT)			width = 8;		if(ch == TDS_SAMPLING_4CH)			ch = 4;		else			ch =1;		printf("sampling setting is %d kHz %d BIT %d CH\n",freq,width,ch);	}	       /* board time get */ /*	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,&h,&m,&s);	//printf("get count day from device [%5d]\n",TDS_GET_DAYS(time));	//printf("get count sec from device [%5d]\n",TDS_GET_SEC(time)); 	printf("%03d %02d:%02d:%02d\n",day,h,m,s); */       	rv = ioctl(fd_in, TDSIO_BUFFER_CLEAR);   // 儃乕僪忋偺FIFO偺僋儕傾乕	if (rv == -1) {		perror("ioctl() FIFO clear failed ");		goto end;	}	else {		printf("ioctl() FIFO clear succeed\n");	}/*       	rv = ioctl(fd_in, TDSIO_SYNC_1PPS);           // 侾俹俹俽傊偺摨婜丅怴儃乕僪(2002.3埲崀)偱偼	if (rv == -1) {                               // 枅夞傗傜側偔偰傕幚尡偺嵟弶偵侾夞傗傟偽椙偄		perror("ioctl() 1pps sync failed ");   //  (枅夞傗傜側偄曽偑椙偄乯		goto end;	}	else {		printf("ioctl() 1pps sync succeed\n");	}*//*	rv = ioctl(fd_in, TDSIO_SYNC_TIME);            // 奜晹帪崗擖椡偼巊梡偟側偄	if (rv == -1) {		perror("ioctl() time sync failed ");		goto end;	}	else {		printf("ioctl() time sync succeed\n");	}*/       /* sampling start */	rv = ioctl(fd_in, TDSIO_SAMPLING_START);	if (rv == -1) {		perror("ioctl() start sampling failed ");              printf("Probably time was not set for the IP-VLBI board.\n");              printf("Please set time using timesettk and try again.\n");		goto end;	}	else {		printf("ioctl() start sampling succeed\n");	}	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 \n",*(buf), *(buf+1));		}				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 data */						printf("%x %x \n",*(buf+rv2s), *(buf+rv2s+1));												/* synck pattern check (this is too heavy for sampling gt 1MHz (no more use) */						/*						strncpy(bit64,(char *)(buf+rv2s),8);						headerchkn(bit64,&hiok,&hspatn,&hsmplbit,&hsfreq,&hnumch,							&hseconds,&hhh,&hmm,&hss);						if (hiok == 0) {  // sync detection							printf("Sync OK!!\n");							printf("A/D(bits) %d  CHs %d  SFreq(kHz) %d",hsmplbit,hnumch, hsfreq);							printf("  Time %02d:%02d:%02d  sec %d\n",hhh,hmm,hss,hseconds);						} else {							printf("First 64bit is not header block!\n");						}						*/					}

⌨️ 快捷键说明

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