Subject: RE: Blocked I/O on serial ports
To: Ruschmeyer, John <jruschme@att.com>
From: Steve Woodford <swoodfor@bluews.com>
List: tech-kern
Date: 08/07/1998 16:30:04
Hi John,

The following code fragment contains the relevant bits you need:

    int fd;
    struct termios ttySave;
    struct termios ttyRaw;

    /*
     * Open it. This must use O_NONBLOCK otherwise the open call would
     * hang until the serial port's DCD line was asserted.
     */
    if ( (fd = open(line, O_RDWR | O_NONBLOCK)) < 0 ) {
        perror(line);
        return 1;
    }

    /*
     * Get current mode attributes for tty line
     */
    if ( tcgetattr(fd, &ttySave) < 0 ) {
        perror("tcgetattr");
        return 1;
    }

    /*
     * Copy them to the raw structure, and modify them to switch to raw
mode
     */
    ttyRaw = ttySave;
    cfmakeraw(&ttyRaw);

    /*
     * Modify them further to select the specified baud rate
     */
    if ( cfsetispeed(&ttyRaw, baud) < 0 ||
         cfsetospeed(&ttyRaw, baud) < 0 ) {
        perror("baudrate");
        return 1;
    }

    /*
     * We don't want to know about BREAK, we don't want to know about
     * reaching the end of a line, or about parity errors
     */
    ttyRaw.c_iflag |= IGNBRK;
    ttyRaw.c_iflag &= ~(IMAXBEL | IGNPAR);

    /*
     * We'd like to ignore DCD
     */
    ttyRaw.c_cflag |= CRTSCTS;

    /*
     * Set the tty line according to the selected attributes
     */
    if ( tcsetattr(fd, TCSANOW, &ttyRaw) < 0 ) {
        perror("tcsetattr");
        return 1;
    }

    /*
     * Now is a good time to ensure the serial port's DTR line is
asserted
     */
    if ( ioctl(fd, TIOCSDTR, 0) < 0 ) {
        perror("ioctl TIOCSDTR");
        return 1;
    }


The important line is:

    ttyRaw.c_cflag |= CRTSCTS;

Hope this helps!

Cheers, Steve