select读主要实现的功能是,在一定时间内不停地看串口有没有数据,有数据则进行读,当时间过去后还没有数据,则返回超时错误。
具体的函数如下;
int read_datas_tty(int fd,char *rcv_buf,int
sec,int usec)
{
int retval;
unsigned char tempchar2;
fd_set rfds;
struct timeval tv;
int ret,pos;
tv.tv_sec = sec;//set the rcv wait time
tv.tv_usec = usec;//100000us = 0.1s
while(1){
FD_ZERO(&rfds);
FD_SET(fd,&rfds);
retval =
select(fd+1,&rfds,NULL,NULL,&tv);
if(retval ==-1)
{
perror("select()");
break;
}
else if(retval)
{
ret=
read(fd,rcv_buf,1);
tempchar2 = rcv_buf;
printf("rcv_buf is %s\n",rcv_buf);
}
else
{
break;
}
}
return 1;
}
在前面的普通读写里面加上这个函数就可以了
它的调用方式为:
read_datas_tty(fd,buff,10,10);
这就表示等待时间为10S+10us
Linux下直接用read读串口可能会造成堵塞,或数据读出错误。然而用select先查询com口,再用read去读就可以避免,并且当com口延时时,程序可以退出,这样就不至于由于com口堵塞,程序就死了。我的代码如下:
bool ReadDevice( int hComm, unsigned long uLen,
char* pData )
{
int nread
= 0;
char inbuf[uLen];
char buff[uLen];
memset(
inbuff, '\0', uLen );
memset( buff, '\0', uLen
);
fd_set readset;
struct timeval tv;
int MaxFd = 0;
int c = 0;
int z;
do
{
FD_ZERO( &readset );
if( hComm >= 0 )
FD_SET( hComm, &readset );
MaxFd = hComm + 1;
tv.tv_sec = 0;
tv.tv_usec = 500000;
do
{
z = select( MaxFd, &readset, 0, 0,
&tv);
}while(
z==-1 && errno==EINTR );
if(
z == -1 )
printf("select(2)\n");
if(
z == 0 )
{
hComm
= -1;
}
if(
hComm>=0 &&
FD_ISSET(hComm, &readset) )
{
z = read( hComm, buff, uLen - c );
c += z;
if( z == -1 )
{
hComm = -1;
}
if( z > 0 )
{
buff[
z + 1 ] = '\0';
strcat(
inbuff, buff );
memset(
buff, 0x00, uLen );
}
else
{
hComm = -1;
}
}
}while( hComm
>= 0 );
memcpy( pData, inbuff, c
);
return true;
}
阅读(386) | 评论(0) | 转发(0) |