在以下用於嵌入式設備的C程序中,我試圖每當用戶在通過串行電纜連接到我的設備的遠程計算機上顯示一個點(「。」),在她的終端程序中輸入一些字符並點擊ENTER鍵。Linux串行端口I/O問題
我看到的是,一旦檢測到第一個回車符,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()`的返回值嗎? – 2011-02-04 15:45:12