【发布时间】:2011-02-04 15:42:41
【问题描述】:
在以下用于嵌入式设备的 C 程序中,每次用户在通过串行电缆连接到我的设备的远程计算机上在其终端程序上输入一些字符时,我都会尝试显示一个点(“.”)并按 ENTER 键。
我看到的是,一旦检测到第一个回车,printf 就会在无限循环中显示点。我期待 FD_ZERO 和 FD_CLR 来“重置”等待条件。
怎么做?
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
main()
{
int fd1; /* Input sources 1 and 2 */
fd_set readfs; /* File descriptor set */
int maxfd; /* Maximum file desciptor used */
int loop=1; /* Loop while TRUE */
/*
open_input_source opens a device, sets the port correctly, and
returns a file descriptor.
*/
fd1 = open("/dev/ttyS2", O_RDWR | O_NOCTTY | O_NONBLOCK);
if (fd1<0)
{
exit(0);
}
maxfd =fd1+1; /* Maximum bit entry (fd) to test. */
/* Loop for input */
while (loop)
{
FD_SET(fd1, &readfs); /* Set testing for source 1. */
/* Block until input becomes available. */
select(maxfd, &readfs, NULL, NULL, NULL);
if (FD_ISSET(fd1, &readfs))
{
/* input from source 1 available */
printf(".");
FD_CLR(fd1, &readfs);
FD_ZERO( &readfs);
}
}
}
【问题讨论】:
-
你试过查看
select()的返回值吗?
标签: linux embedded serial-port