/* acq.c */ /* copied from scope.c so that might explain some weird cruft */ /* This is meant to connect to a deformatter through the fast * acquisition interface and collect and integrate for a while. * It writes its output to an ascii file to make it easy to * process and plot results in something like scilab. */ #include #include #include #include #include #include #include #include /* data access stuff */ #define RECLEN 4096 int fd; int process( int ); main( int argc, char **argv ) { int status; int i; int channelmode; struct sockaddr_in address; struct hostent *otheraddress; char *there; int nscan; if(argc < 3) { fprintf(stderr, "usage : %s \n"); return 0; } nscan = atoi(argv[2]); if(nscan < 1 || nscan > 10000) return; fd = socket( AF_INET, SOCK_STREAM, 0 ); if( fd < 0 ){ perror( "socket" ); exit( 1 ); } bzero( &address, sizeof(address) ); address.sin_family = AF_INET; address.sin_port = htons( 5001 ); fprintf( stderr, "Looking up %s\n", argv[1] ); otheraddress = gethostbyname( argv[1] ); if( otheraddress == NULL ){ perror( "gethostbyname" ); exit( 1 ); } there = otheraddress->h_addr_list[0]; bcopy( there, (void*)&address.sin_addr.s_addr, otheraddress->h_length ); fprintf( stderr, "Connecting.\n" ); status = connect( fd, (struct sockaddr*)&address, sizeof( address ) ); if( status < 0 ){ perror( "connect" ); exit( 1 ); } if( argc > 2 ){ nscan = strtol( argv[2], NULL, 10 ); } fprintf( stderr, "Starting processing. %d scans.\n", nscan ); process( nscan ); close( fd ); } int process( int nscans ) { int data[RECLEN]; double realpart, imagpart; double mean, t; double max, min; int otypelen; int onpoints; int oreclen; int status; int count; int recvcount; int i; char buffer[RECLEN]; char *readbuf; char channels; int reclen; int typelen; char smallbuf[16]; int hist[257]; for(i = 0; i < 257; i++) hist[i] = 0; while( nscans-- ){ fprintf(stderr, "%d...\n", nscans); *(short*)smallbuf = htons((short)RECLEN); smallbuf[2] = 3; status = write( fd, smallbuf, 3 ); if( status < 0 ){ perror( "write" ); exit( 1 ); } fsync( fd ); status = read( fd, smallbuf, 3 ); channels = smallbuf[2]; typelen = channels&4?2:1; count = ntohs(*(short*)smallbuf); if( typelen != otypelen || count != onpoints ){ /* the data type either changed or this is the first time. */ otypelen = typelen; } reclen = count * typelen; recvcount = 0; readbuf = buffer; do{ status = read( fd, readbuf, reclen ); if( status < 0 ){ perror( "read" ); exit( 1 ); } readbuf += status; recvcount += status; }while( recvcount < reclen ); switch( typelen ){ case 1: for( i = 0 ; i < count ; i++ ) data[i] = ((char*)buffer)[i]; break; case 2: for( i = 0 ; i < count ; i++ ) data[i] = ((short*)buffer)[i]; break; } for(i = 0; i < count; i++) hist[(data[i]+128)]++; } for(i = 0; i < 257; i++) printf("%d %d\n", i-128, hist[i]); return 0; }