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]

Can't set CREAD flag on serial port?


I'm trying to write an application to access a serial port on a
Windows 2000 box, but the CREAD flag doesn't stay set when I do
`tcsetattr' and check the values with `tcgetattr'.  Trying to read
from the port returns a ``Resource temporarily unavailable'' error (as
generated from perror).  What would cause an app to fail to read from
the port, or is this a Win2K issue (permissions or something)?

One thing I should mention--this is a null modem cable attached to the
box.  Would that have a bearing on the issue?

The following is the relevant code:

int
main ( void ) {

    char buf[32];
    int n;
    struct termios newtio;
    struct termios oldtio;
    int tty_fd;

    tty_fd = open("/dev/com2",O_RDWR|O_NDELAY|O_BINARY);

    tcgetattr(tty_fd,&oldtio);

    memcpy( &newtio, &oldtio, sizeof(newtio) );

    newtio.c_cflag &= ~(CSIZE|CBAUD|CRTSCTS|PARENB|CSTOPB);
    newtio.c_cflag |= (CLOCAL|CREAD|CS8);
    cfsetispeed(&newtio,tty_speed);
    cfsetospeed(&newtio,tty_speed);

    newtio.c_lflag &= ~(ICANON|ECHO|ECHOE|ISIG);
    newtio.c_lflag |= IEXTEN;

    newtio.c_iflag &= ~(INPCK|ISTRIP|IXON|IXOFF|IXANY|BRKINT|ICRNL|INLCR|IGNCR|IUCLC);

    newtio.c_oflag &= ~OPOST;

    newtio.c_cc[VMIN] = 0;
    newtio.c_cc[VTIME] = 0;

    printf("+++ setting termios: cflag=%08X lflag=%08X iflag=%08X oflag=%08X ispeed=%d/%08X ospeed=%d/%08X\n",
           newtio.c_cflag,
           newtio.c_lflag,
           newtio.c_iflag,
           newtio.c_oflag,
           newtio.c_ispeed,
           newtio.c_ispeed,
           newtio.c_ospeed,
           newtio.c_ospeed);
 
    tcsetattr(tty_fd,TCSANOW,&newtio);

    /* *** At this point the CREAD flag is no longer set */
    tcgetattr(tty_fd,&newtio);
    printf("+++ updated termios: cflag=%08X lflag=%08X iflag=%08X oflag=%08X ispeed=%d/%08X ospeed=%d/%08X\n",
           newtio.c_cflag,
           newtio.c_lflag,
           newtio.c_iflag,
           newtio.c_oflag,
           newtio.c_ispeed,
           newtio.c_ispeed,
           newtio.c_ospeed,
           newtio.c_ospeed);

    while ( 1 ) {
        /* *** `read' fails at this point */
        n = read( tty_fd, buf, sizeof(buf) );
        if ( n < 0 ) {
            perror("read");
            exit(2);
            }
        if ( n == 0 )
            break;
        write(1,buf,n);
        }

    tcsetattr(tty_fd,TCSANOW,&oldtio);

    close(tty_fd);

    return 0;

    }

-- 
Michael J. Barillier               | ``Those who make peaceful
email: mbarilli(at)midsouth.rr.com | revolution impossible will make
web: <http://shadizar.dyndns.org/> | violent revolution inevitable.''
Public key available on request.   |     -- John F. Kennedy

--
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]