This is the mail archive of the cygwin mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: select call does block unless data arrives at socket (when waiting for serial port and ip socket)


On Wed, 29 Sep 2004, Stefan Mahr wrote:

> I want to use select() to wait for a serial port and a ip socket.
> Following problem:
> If data arrives the serial port, select() works as aspected and returns 1.
> If data arrives the ip socket, select() doesn't return.
> If data arrives the serial port and before there was some data at the ip
> socket,
> select() returns 2. Both file descriptors are set in "fd_set input", and
> all data
> can be read by the next functions.
>
> I am using the cygwin1.dll  version 1.5.11 with Windows XP.
> (The same problem with previous versions of cygwin1.dll.)
>
> Sample sourcecode:
> /*********************************************/
>
> /* ipserial.c */
> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>
> #include <unistd.h>
> #include <fcntl.h>
> #include <termios.h>
> #include <sys/select.h>
> #include <sys/socket.h>
> #include <netinet/in.h>
> #include <netdb.h>
>
> /*****************************************************************************/
>
> #define HOSTNAME "localhost"
> #define HOSTMODEPORT 5000
> #define MAXDATASIZE 256
> #define SERIALPORT "com5"

If you want POSIX behavior from the serial port (ie. termios, etc.),
please see:

http://cygwin.com/cygwin-ug-net/using-specialnames.html#AEN825

and use the proper POSIX device (ie. /dev/ttyS4; /dev/com5 may also
work, but is not suggested).

>/*****************************************************************************/
>
> int open_port(void)
> {
>  int fd;
>
>  fd = open(SERIALPORT, O_RDWR | O_NOCTTY);
>  if (fd == -1) {
>    perror("open serial port");
>  } else
>    fcntl(fd, F_SETFL, 0);

What are you trying to accomplish here?  One would normally do a get,
modify, set like:

{
    int flags = fcntl(fd, F_GETFL);

    if (flags < 0 || fcntl(fd, F_SETFL, flags|O_NONBLOCK) < 0)
    {
        perror("fcntl(O_NONBLOCK) failed");
        exit(1);
    }
}

>  return (fd);
> }
>
> /*****************************************************************************/
>
> int init_port(int fd)
> {
>  struct termios options;
>
>  tcgetattr(fd, &options);
>
>  cfsetispeed(&options, B19200);
>  cfsetospeed(&options, B19200);
>
>  options.c_cflag |= (CLOCAL | CREAD);
>  options.c_cflag &= ~CSTOPB;
>  options.c_cflag &= ~CRTSCTS;
>  options.c_iflag &= ~(IXON | IXOFF | IXANY);
>
>  //cfmakeraw(&options);
>  options.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
>  options.c_oflag &= ~OPOST;
>  options.c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
>  options.c_cflag &= ~(CSIZE|PARENB);
>  options.c_cflag |= CS8;
>
>  options.c_cc[VMIN]  = 0;
>  options.c_cc[VTIME] = 10;
>
>  tcsetattr(fd, TCSANOW, &options);
>
>  return(0);
> }
>
> /*****************************************************************************/
>
> int main(void)
> {
>  int err;
>  int numbytes;
>
>  char serialbuf[MAXDATASIZE];
>  char socketbuf[MAXDATASIZE];
>
>  int            serialfd,socketfd;
>  int            max_fd;
>  fd_set         input;
>
>  struct hostent *he;
>  struct sockaddr_in their_addr;
>
>  if ((he=gethostbyname(HOSTNAME)) == NULL) {
>      perror("gethostbyname");
>      exit(1);
>  }
>  if ((socketfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
>      perror("socket");
>      exit(1);
>  }
>
>  their_addr.sin_family = AF_INET;
>  their_addr.sin_port = htons(HOSTMODEPORT);
>  their_addr.sin_addr = *((struct in_addr *)he->h_addr);
>  memset(&(their_addr.sin_zero), '\0', 8);
                                        ^ sizeof(their_addr.sin_zero) ;-)?

You might also try poll, or debugging it yourself as CGF suggested.  I
don't have time to look into it right now :-(.  Sorry.

Since the problem appears to be with the socket, can you get two sockets
to have this behavior?

-- 
Brian Ford
Senior Realtime Software Engineer
VITAL - Visual Simulation Systems
FlightSafety International
the best safety device in any aircraft is a well-trained pilot...

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]