select에서 timeout 조정이 되질 않습니다.
글쓴이: leolo / 작성시간: 화, 2004/05/25 - 9:46오전
여기서 select에서 timeout을 하면 블럭이 되어야 하는데 안됩니다.
open 시 플레그에서 설정을 하여야하는 것 같기도 한는데..
O_NONBLOCK 대신 일단.. 타임아웃 동안은 블럭되게 좀 만들어주세요..
#include <stdio.h>
#include <unistd.h>
#include <termios.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/select.h>
#include <time.h>
#include <sys/time.h>
#define COM1 "/dev/ttyS0"
#define BAUDRATE 4800
#define FLOW 1
#define HWCONTROL 1
int serialOpen()
{
long baudrate;
struct termios tio;
int fd = open(COM1, O_RDWR | O_NOCTTY | O_NONBLOCK, 0644);
if(fd < 0)
{
fprintf(stderr, "Can't open device %s\n", COM1);
return -1;
}
else
{
bzero(&tio, sizeof(tio));
tio.c_cflag &= ~CSIZE;
tio.c_cflag |= CS8; /* 8 data bit */
tio.c_cflag &= ~CSTOPB; /* 1 stop bit */
tio.c_cflag &= ~(PARENB | PARODD); /* NO PARITY BIT */
tio.c_cflag &= ~CRTSCTS; /* NOT FLOW CONTROL */
}
baudrate = B4800;
if((cfsetispeed(&tio, baudrate) < 0 || cfsetospeed(&tio, baudrate) < 0)) /* baudrate set */
{
fprintf(stderr, "Failed baudrate set.\n");
close(fd);
return -1;
}
else /* SUCCESS */
{
if(tcsetattr(fd, TCSANOW, &tio) < 0) /* termios set */
{
printf("Can't set Termios struct.\n");
close(fd);
return -1;
}
else
{
if(tcflush(fd, TCIOFLUSH) < 0)
{
close(fd);
return -1;
}
}
}
printf("fd '%d' open\n", fd);
return fd;
}
int main(int argc, char* argv[])
{
int terminated = 0;
struct timeval tv;
fd_set rfd, wfd;
int max_fd, result;
int fd = serialOpen();
if(fd < 0)
{
printf("File open '%d'\n", fd);
close(fd);
exit (1);
}
while(!terminated)
{
FD_ZERO(&rfd);
FD_ZERO(&wfd);
FD_SET(fd, &rfd);
FD_SET(fd, &wfd);
tv.tv_sec = 4;
tv.tv_usec = 0;
result = select(fd + 1, &rfd, &wfd, (fd_set*)0, &tv);
if(result < 0)
{
if(errno != EINTR)
{
perror("select");
}
fprintf(stderr, "select error : %s(%d)\n", strerror(errno), errno);
continue;
}
else if(result == 0)
fprintf(stderr, "select timeout\n");
printf("result '%d'\n", result);
Forums:


해당 fd 의 writeable은 대부분 거의 (항상이라고 할만큼) 가능
해당 fd 의 writeable은 대부분 거의 (항상이라고 할만큼) 가능합니다. (아.. 시리얼이라면 좀 상황이 다를 수 있지만...)
당연히 기다리지 않고 "나 쓸 수 있어요" 하고는 select 빠져 나올텐데요....
readable 하고 writeable 하고는 성격이 좀 다르니까요. 필요할 때에만 writable 비트를 켜서 조사하는 것이 일반적이지 않을까요?
그리고 select 타임아웃하고 블러킹/넌블러킹하고는 상관이 없죠?
댓글 달기