select에서 timeout 조정이 되질 않습니다.

leolo의 이미지

여기서 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);

bugiii의 이미지

해당 fd 의 writeable은 대부분 거의 (항상이라고 할만큼) 가능합니다. (아.. 시리얼이라면 좀 상황이 다를 수 있지만...)

당연히 기다리지 않고 "나 쓸 수 있어요" 하고는 select 빠져 나올텐데요....

readable 하고 writeable 하고는 성격이 좀 다르니까요. 필요할 때에만 writable 비트를 켜서 조사하는 것이 일반적이지 않을까요?

그리고 select 타임아웃하고 블러킹/넌블러킹하고는 상관이 없죠?

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.