当前位置: 技术问答>linux和unix
串口轮询怎么实现啊???
来源: 互联网 发布时间:2015-09-05
本文导语: 各位老大,我在linux下要用轮询的方法实现读写数据,是怎么实现的,有的定时器,有的用select结构 有具体的例程,没有啊,拜托了 | 用select. tv.tv_sec = 10; //timeout 5 seconds tv.tv_usec = 0; FD_ZERO(&wr...
各位老大,我在linux下要用轮询的方法实现读写数据,是怎么实现的,有的定时器,有的用select结构
有具体的例程,没有啊,拜托了
有具体的例程,没有啊,拜托了
|
用select.
tv.tv_sec = 10; //timeout 5 seconds
tv.tv_usec = 0;
FD_ZERO(&writefds);
FD_SET(sockfd, &writefds);
if(select(sockfd + 1, &writefds, NULL, NULL, &tv) > 0)
{
bzero(buffer, sizeof(buffer));
len = recv(sockfd, buffer, sizeof(buffer), 0);
buffer[len] = 0;
if ((len > 0) && (strstr(buffer, "OK") != NULL))
{
printf("buffer = %sn", buffer);
ok++;
break;
}
} else goto sock;
tv.tv_sec = 10; //timeout 5 seconds
tv.tv_usec = 0;
FD_ZERO(&writefds);
FD_SET(sockfd, &writefds);
if(select(sockfd + 1, &writefds, NULL, NULL, &tv) > 0)
{
bzero(buffer, sizeof(buffer));
len = recv(sockfd, buffer, sizeof(buffer), 0);
buffer[len] = 0;
if ((len > 0) && (strstr(buffer, "OK") != NULL))
{
printf("buffer = %sn", buffer);
ok++;
break;
}
} else goto sock;