- 论坛徽章:
- 0
|
int fd;
fd = open("/dev/ttyS0",O_RDWR|O_NOCTTY);
if (-1 == fd)
{
perror("open error");
}
struct termios Attr;
struct timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 0;
tcgetattr(fd,&Attr);
Attr.c_lflag &= ~(ECHO|ICANON|IEXTEN|ISIG);
//Attr.c_lflag |= ICANON;
Attr.c_iflag &= ~(BRKINT|ICRNL|INPCK|ISTRIP|IXON);
Attr.c_cflag &= ~(CSIZE|PARENB);
Attr.c_cflag |= CS8;
Attr.c_oflag &= ~(OPOST);
Attr.c_cc[VMIN] = 30;
Attr.c_cc[VTIME] = 0;
cfsetispeed(&Attr,B115200);
cfsetospeed(&Attr,B115200);
tcsetattr(fd,TCSANOW,&Attr);
//---------------------------------------------------
char buf[35]={'\0'};
memset(buf,'\0',sizeof(buf));
int w_num;
char w_buf[35];
int RD_NUM=34;
int count = 0;
int s_fd,d_fd;
char accel_buf[150]={0};
d_fd = open("data",O_RDWR|O_TRUNC|O_CREAT);
if (-1==d_fd)
perror("cant oepn");
s_fd = open("seriel",O_RDWR|O_TRUNC|O_CREAT);
if (-1==s_fd)
perror("cant oepn");
//---------------------------------------------------------
while( 1 )
{
fd_set fds;
FD_ZERO( &fds );
FD_SET( fd, &fds );
tcflush(fd,TCIOFLUSH);
int rc = select( fd+1, &fds, 0, 0, 0);
if( rc < 0 )
{
perror( "select" );
return true;
}
if( FD_ISSET( fd, &fds ) )
{
read(fd,buf,RD_NUM);
}
wreite(d_fd,buf,RD_NUM); |
|