aio_read vs read

New Message Reply Date view Thread view Subject view Author view

Dorrie Hall (dorrie++at++beamish.mit.edu)
Mon, 16 Jan 95 17:22:17 -0500


        It may be old news, but I was so pleased with the performance
increase when I replaced the read calls in our code to get data from
a Polhemus fastrak with aio_read that I thought I'd post this little
piece of code to get data from a fastrak in ascii polled mode. (We're
running 5.2)

#include <termio.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <aio.h>

#define TEST_CYCLES 1000

/* cc asciipoll.c -o asciipoll */
/* get data from Polhemus fastrak in ascii polled mode */int desc;

static struct termio termconf;
void send_fastrak_cmd(int, char *);
void InitFastrak(void);
int ret;
char buf[50];
char oc,er;
int st;
int bad;
float x,y,z,a,e,r;
int count;
long EndTime, BeginTime;
aiocb_t aio_stuff;

main(){
int i;

count = 0;
bad = 0;
InitFastrak();

aio_stuff.aio_fildes = desc;
aio_stuff.aio_nbytes = 47;
aio_stuff.aio_buf = malloc(50);

time(&BeginTime);

for (i = 0; i < TEST_CYCLES ; i++){

/*fast way */
     send_fastrak_cmd(desc,"P");
     aio_read(&aio_stuff);
     strcpy(buf,aio_stuff.aio_buf);
     

/* slow way
     send_fastrak_cmd(desc,"P");
     ret=read(desc,buf,47);
*/

    if( buf[0] == '0' && buf[1] == '1' && buf[46] == '\n'){
    count++;
   }
   else{ bad++; printf("i = %d\n",i);}
/*
    sscanf(buf,"%c%i%c%f%f%f%f%f%f",&oc,&st,&er,&x,&y,&z,&a,&e,&r);
    printf("%f %f %f %f %f %f\n",x,y,z,a,e,r);
    printf(buf);
*/

}/*do for TEST_CYCLES*/

time(&EndTime);

fprintf(stderr, "AsciiPolled elapsed sec for %d calls = %ld\n",
                    i, EndTime - BeginTime);
fprintf(stderr,
         "%f reads per sec\n",(float)i / (float)( EndTime - BeginTime ) );
printf("%d bad record(s)\n",bad);

printf("%d good records\n",count);
}/* end main */
/*********************************/
void InitFastrak() {

  desc = open("/dev/ttyd3",O_RDWR );

  if ( desc >= 0)
    {
      printf("Port /dev/ttyd3 open.\n");
    }
  else
    perror("Open port:");
   
  termconf.c_iflag = 0;
  termconf.c_oflag = 0;
  termconf.c_cflag = B38400 | CS8 | CREAD | CLOCAL;
  termconf.c_lflag = ICANON;
  termconf.c_line = 0;
  termconf.c_cc[VTIME] = 0;
  termconf.c_cc[VMIN] = 47; /* max packet size */

  if (ioctl(desc, TCSETAW, &termconf) == -1)
    {
      perror("Tracker-TermSetup");
      exit(0);
    }
  /* transmitter suspended from ceiling */
  send_fastrak_cmd(desc,"H1,0,0,-1");
  send_fastrak_cmd(desc,"F"); /*ascii*/
  send_fastrak_cmd(desc,"c"); /* non-continuous polling */
}/* end InitFastrak */
/**************************************/
void send_fastrak_cmd(int desc, char * cmd_buf)
{
  char crlf[3];
   
  /* code to add a CR-LF pair to the end of a command */
  /* if it's needed */

  sprintf(crlf,"\r\n");

  switch ((int) cmd_buf[0]){
  case 'P':
  case 'C':
  case 'c':
    write(desc,cmd_buf,1);
    break;
  default:
    strcat(cmd_buf,crlf);
    write(desc, cmd_buf, strlen(cmd_buf));
  }
}/* end send_fastrak */
/***************************************/
        dorrie++at++beamish.mit.edu


New Message Reply Date view Thread view Subject view Author view

This archive was generated by hypermail 2.0b2 on Mon Aug 10 1998 - 17:50:52 PDT

This message has been cleansed for anti-spam protection. Replace '++at++' in any mail addresses with the '@' symbol.