LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-25-2003, 06:09 AM   #1
George2
Member
 
Registered: Oct 2003
Posts: 354

Rep: Reputation: 30
Unhappy How to set timeout value when reading from a file descriptor.


Hello, everyone!


I want to set the reading timeout value to 5 second when using file descriptor reading from serial port. But I find my program never exits after 5 second idle time. Here is the source codes I am using under Red Hat Linux 8.0 environment.

The program never exits if I input nothing from serial port.

Source Code:

----------
#include <termios.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/signal.h>

#define BAUDRATE B38400
#define MODEMDEVICE "/dev/ttyS0"

#define FALSE 0
#define TRUE 1

int main (int argc, char** argv)
{
int fd,c;
struct termios oldtio,newtio,oldstdtio,newstdtio;
struct sigaction sa;

fd = open (MODEMDEVICE, O_RDWR | O_NOCTTY);
if (fd < 0)
{
perror (MODEMDEVICE);
exit (-1);
}

tcgetattr (fd, &oldtio);

newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;

newtio.c_iflag = IGNPAR;

newtio.c_oflag = 0;

newtio.c_lflag = 0;

newtio.c_cc [VMIN] = 1;
newtio.c_cc [VTIME] = 5;

tcflush (fd, TCIFLUSH);
tcsetattr (fd, TCSANOW, &newtio);

tcsetattr (1, TCSANOW, &newtio);

while (1)
{
if(!read (fd, &c, 1))
{
break;
}
write (1, &c, 1);
}

return 1;
}
----------

Can anyone help?


Thanks in advance,
Geo
 
Old 10-25-2003, 07:20 AM   #2
dorian33
Member
 
Registered: Jan 2003
Location: Poland, Warsaw
Distribution: LFS, Gentoo
Posts: 591

Rep: Reputation: 32
Read this. Very good documentation.
 
Old 10-25-2003, 08:27 AM   #3
George2
Member
 
Registered: Oct 2003
Posts: 354

Original Poster
Rep: Reputation: 30
Unhappy

Thanks, dorian33 buddy!

I have read this article, and I can not find the explanation of how to use newtio.c_cc [VMIN] and newtio.c_cc [VTIME] together. My purpose is, when a char is read I process this char and when 5 second serial input idle time is met with, the program is terminated.

I think I should use them together and I can not find how to use the two parameters together.

Can you help with my special case?


Best regards,
Geo


Quote:
Originally posted by dorian33
Read this. Very good documentation.
 
Old 10-25-2003, 09:34 AM   #4
silatts
LQ Newbie
 
Registered: Oct 2003
Posts: 19

Rep: Reputation: 0
in general an alarm call might work with a signal catcher,. but I don't know for sure,

man alarm

?

Sil
 
Old 10-25-2003, 09:42 AM   #5
George2
Member
 
Registered: Oct 2003
Posts: 354

Original Poster
Rep: Reputation: 30
Unhappy

Thanks, silatts buddy!

I can not image how to use a timer to do timeout enabled read task. I think select is better.

Can you show me your idea in detail or sample with signal alarm.


Best regards,
Geo


Quote:
Originally posted by silatts
in general an alarm call might work with a signal catcher,. but I don't know for sure,

man alarm

?

Sil
 
Old 10-25-2003, 09:45 AM   #6
silatts
LQ Newbie
 
Registered: Oct 2003
Posts: 19

Rep: Reputation: 0
ah - you are right - select is better

Sil
 
Old 10-26-2003, 01:09 AM   #7
infamous41md
Member
 
Registered: Mar 2003
Posts: 804

Rep: Reputation: 30
hi, maybe this helps:
Code:
#include <stdio.h>
#include <unistd.h>
#include <sys/select.h>

#ifndef ERR_LIB
        void perr_quit(char *x){
                perror(x);
                exit(1);
        };
#endif

int main(void)
{
        fd_set  rset;
        int     fd = 0, ret = 0;
        char    buf[100];
        struct timeval tv;

        tv.tv_sec = 5;
        tv.tv_usec = 0;

        //for simplicity this references stdin
        if( (fd = dup(STDIN_FILENO)) < 0 )
                perr_quit("dup");

        //always need to zero it first, then add our new descripto
        FD_ZERO(&rset);
        FD_SET(fd, &rset);

        //wait for 5 seconds
        if( (ret = select(fd + 1, &rset, NULL, NULL, &tv)) < 0)
                perr_quit("select");
        else if(ret == 0){
                puts("timed out");
                return 1;
        }

        //ret must be positive
        if(FD_ISSET(fd, &rset)){
                read(fd, buf, 99);
                return 0;
        }

        puts("some error");
        return 1;
}
 
Old 10-26-2003, 01:15 AM   #8
infamous41md
Member
 
Registered: Mar 2003
Posts: 804

Rep: Reputation: 30
also, you i believe you can fix this much simpler now that i look closer. shouldn't this:
newtio.c_cc [VMIN] = 1;
newtio.c_cc [VTIME] = 5;
be
newtio.c_cc [VMIN] = 0;
newtio.c_cc [VTIME] = 5;

when VMIN = 1, you have to wait for at least 1 byte of data, then you wait up to 5 seconds
between future byte of data. when it is 0 you will wait at most 5 seconds for the first byte, and then
5 seconds between future bytes. this is covered in stevens(APUE) term i/o section.

Last edited by infamous41md; 10-26-2003 at 01:16 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Get the absolute path of file using file descriptor. appas Programming 7 01-19-2012 11:47 AM
Bad File Descriptor tech_user Linux - Hardware 0 10-14-2005 03:21 PM
apt-file returns nothing; 'bad file descriptor' overbored Debian 3 10-03-2004 09:13 PM
[c++] How can I get a file descriptor from a c++ i/o stream? chuanyung Programming 8 03-08-2004 11:23 PM
File descriptor lido Linux - Newbie 5 07-17-2003 11:58 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:22 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration