//设置串口 
int setBaudrateForGPS(int rate) 
{ 
          fd = open(serialDev,O_RDWR | O_NOCTTY | O_NDELAY); 
          if(fd<0) 
          { 
            printf("can not open the serial dev for GPS!\n"); 
            return -1; 
          } 
          gpsFile=fopen("data.txt","a+"); 
          if(gpsFile==NULL) 
          { 
                  printf("can not creat data.txt\n"); 
            return -1; 
          } 
          tcgetattr(fd,&oldtio);//保存原先串口配置 
          tcgetattr(fd,&newtio);//获得原先串口配置 
           
          cfsetispeed(&newtio,B38400);//设置输入的波特率为38400 
          cfsetospeed(&newtio,B38400);//设置输出的波特率为38400 
           
          newtio.c_cflag |= CS8;      //8位数据位,1位停止位 
          newtio.c_cflag &= ~CSTOPB;   
           
          if(tcsetattr(fd,TCSAFLUSH,&newtio)<0)//使设置生效 
          { 
                   printf("tcsetattr failed!\n"); 
                   return -1; 
          } 
           
          return 0; 
} 
//读取串口数据 
void readDataFormGPS(void) 
{    
         fd_set fds;                //新建文件描述符集合 
         FD_ZERO(&fds);             //清空该集合 
   FD_SET(fd,&fds);           //将串口fd加入到fds集合中 
   select(fd+1, &fds, NULL, NULL,NULL);//通过select函数监听fds集合里面的文件是否可读,即串口有数据可读 
                                       //如果fds里面的文件都不可读,那么程序将在此处阻塞 
   printf("ttySAC3 has data!\n"); 
   if(FD_ISSET(fd,&fds))               //检查fd是否在fds可读文件中(该情况可省去,因为ds就一个文件) 
           { 
      int len=read(fd, buffer, 256); 
      printf("length=%d\n",len); 
            fwrite(buffer,1,len,gpsFile); 
          } 
} 
 
 |