Hello.
Excuse me for my English.
I am Russian.
I have read serial programming manual heare
http://www.faqs.org/docs/Linux-HOWTO/Serial-Programming-HOWTO.html and I have some questions.
First question about select system call. In example
http://www.faqs.org/docs/Linux-HOWTO/Serial-Programming-HOWTO.html#AEN110
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
main()
{
int fd1, fd2; /* 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_input_source("/dev/ttyS1"); /* COM2 */
if (fd1<0) exit(0);
fd2 = open_input_source("/dev/ttyS2"); /* COM3 */
if (fd2<0) exit(0);
maxfd = MAX (fd1, fd2)+1; /* maximum bit entry (fd) to test */
/* loop for input */
while (loop) {
FD_SET(fd1, &readfs); /* set testing for source 1 */
FD_SET(fd2, &readfs); /* set testing for source 2 */
/* block until input becomes available */
select(maxfd, &readfs, NULL, NULL, NULL);
if (FD_ISSET(fd1)) /* input from source 1 available */
handle_input_from_source1();
if (FD_ISSET(fd2)) /* input from source 2 available */
handle_input_from_source2();
}
}
|
From this code I understand that open_input_source is a function where we do:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NONBLOCK);
if (fd <0) {perror(MODEMDEVICE); exit(-1); }
tcgetattr(fd,&oldtio); /* save current port settings */
/* set new port settings for canonical input processing */
newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;
newtio.c_iflag = IGNPAR | ICRNL;
newtio.c_oflag = 0;
newtio.c_lflag = ICANON;
newtio.c_cc[VMIN]=1;
newtio.c_cc[VTIME]=0;
tcflush(fd, TCIFLUSH);
tcsetattr(fd,TCSANOW,&newtio);
return fd;
|
All in all, this is an initialization of com. And I can't understand how works this code:
1 2
|
maxfd = MAX (fd1, fd2)+1; /* maximum bit entry (fd) to test */
select(maxfd, &readfs, NULL, NULL, NULL);
|
If fd1 will be 1518 and fd2 1357 that maxfd will be 1519. It is not imaginable! For what? For what it is possible give 1519 for select? It will be listen all file descriptors lower than 1519?
It is question because in another example I have seen that if we want to listen only one com-port we can call select so: select(1, &readfs, NULL, NULL, NULL) and when we have listen only two ports we can call so: select(2, &readfs, NULL, NULL, NULL).
Please, answer me where is true? I want to listen one com-port and I think that I must call select so: select(fd+1, ...)
And second question, about reading data when select returns upper than 0 (when we have data for reading).
Can I call read function so
1 2 3
|
res = read(fd,buf,255); /* returns after 5 chars have been input */
buf[res]=0; /* so we can printf... */
printf(":%s:%d\n", buf, res);
|
?
Thank you very match.