Requesting help on intricacies of duplicated file descriptors.
Carlo Zinato
czinato at inwind.it
Tue Jul 25 07:11:08 UTC 2006
This is how I open a serial port at 115200 baud and successfully use it
in raw mode: select(...) system call wakes up correctly when serial
receives one byte. No preprocessing is done. Also write(...) system call
puts chars to serial without postprocessing (notice c_iflag and c_oflag).
///////// CODE SNIPPET /////////////
int fd_ser;
struct termios options;
if((fd_ser = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY)) == -1){
printf("Could not open serial 1\n");
return -1;
}
if(fcntl(fd_ser, F_SETFL, fcntl(fd_ser, F_GETFL) | O_ASYNC | O_NONBLOCK)
== -1){
printf("Could not set serial port flags \n");
return -1;
}
tcgetattr(fd_ser, &options);
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CLOCAL | CREAD;
options.c_cflag |= CS8;
options.c_iflag = 0;
options.c_oflag = 0;
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
options.c_cc[VMIN] = 1;
options.c_cc[VTIME] = 0;
cfsetispeed(&options, B115200);
cfsetospeed(&options, B115200);
tcflush(fd_ser, TCIFLUSH);
tcsetattr(fd_ser, TCSANOW, &options);
///////// END CODE SNIPPET /////////////
If you want to empty the serial input fifo, do something like:
/////////CODE SNIPPET /////////////
fd_set input_fd_set;
char char_in;
FD_ZERO(&input_fd_set);
FD_SET(fd_ser, &input_fd_set);
while(1){
fd_ready = select(fd_ser + 1, &input_fd_set, NULL, NULL, NULL);
if(fd_ready == -1){
switch(errno){
case EBADF:
printf("Bad file descriptor\n");
return -1;
case EINTR:
printf("Signal Interruption\n");
return -1;
}
}
else if(fd_ready == 0){
printf("No file descriptor ready\n");
return -1;
}
else{
if(read(fd_ser, &char_in, 1) < 1){
printf("Couldn't read byte\n");
return -1;
}
}
}
Hope this will be useful...
Carlo
Joakim Tjernlund ha scritto:
>
>
>> -----Original Message-----
>> From: uclibc-bounces at uclibc.org
>> [mailto:uclibc-bounces at uclibc.org] On Behalf Of Garrett Kajmowicz
>> Sent: den 23 juli 2006 20:51
>> To: uclibc at uclibc.org
>> Cc: Rich Felker
>> Subject: Re: Requesting help on intricacies of duplicated
>> file descriptors.
>>
>> On Sunday 23 July 2006 03:35, Rich Felker wrote:
>>
>> <snip>
>>
>>> Also it's a bad idea to use non-blocking io with stdio streams (FILE
>>> *) unless you really know what you're doing. If no input is
>> available,
>>> the read operation will return with error and set the error flag for
>>> the stream. It can be cleared with fclearerr() and
>> presumably you can
>>> continue reading then, but nonblocking io is really the
>> most useful in
>>> connection with the non-buffering unix-style fd io functions.
>> Thanks for the primer. The case I need to address is reading
>> a minimum of one
>> character from a stream (blocking until the character is available or
>> returning error if we are EOF). At the same time, I want to
>> read as many
>> characters as possible (up to a limit) to go into some
>> buffering code. Yes,
>> stdio does buffering, however I need to do my own limited
>> buffering for
>> uClibc++. The current implementation switches between blocking and
>> non-blocking when doing reads, but I take it that this isn't
>> a good idea.
>>
>> Instead, what is my best approach to read a minimum of 1 byte
>> from the stream,
>> blocking only if needed to get that first byte?
>
> hmm, check termios(VMIN and VTIME)
>
> Jocke
>
> _______________________________________________
> uClibc mailing list
> uClibc at uclibc.org
> http://busybox.net/cgi-bin/mailman/listinfo/uclibc
>
>
--
No virus found in this outgoing message.
Checked by AVG Free Edition.
Version: 7.1.394 / Virus Database: 268.10.4/396 - Release Date: 24/07/2006
More information about the uClibc
mailing list