Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 28 Jun 2001 17:58:28 -0400 (EDT)
From:      Jason Borkowsky <jcborkow@tcpns.com>
To:        Chris Faulhaber <jedgar@fxp.org>
Cc:        freebsd-hackers@freebsd.org
Subject:   Re: Serial port control
Message-ID:  <Pine.BSF.4.21.0106281752090.22968-100000@bemused.tcpns.com>
In-Reply-To: <20010628160723.C21188@peitho.fxp.org>

next in thread | previous in thread | raw e-mail | index | archive | help

> > I am looking to find a simple way to control a serial port through BSD
> > (such as raising and lowering DTR for a specified duration). I thought I
> > had it using ioctl() and wrote a simple program to test it, but it seems I
> > don't have a full understanding of ioctl(). Does anyone know of any
> > pre-written utilities I can use? Or where to get some really detailed
> > information about ioctl()? Thanks!
> > 
> 
> Many links at: http://www.stokely.com/unix.serial.port.resources/
> 
> in particular: http://www.easysw.com/~mike/serial/serial.html

I thought I maybe had this under control, but not yet.

I am trying to control the serial control lines on a FreeBSD 4.2 box, and
wrote a simple program (included below). This program polls the serial
controller for its current state, displays the state, and then attempts to
set the state of two of the control lines (lower RTS and DTR). However,
after telling ioctl() to lower RTS and DTR, and then polling the status
again, RTS and DTR are still high. Does anyone know which serial control
lines FreeBSD allows you to manipulate and why this isn't working?

To compile the program, merely save it as text, gcc the text file, and
run. Any insight is greatly appreciated!


#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>

main () {

      int fd; /* File descriptor for serial port */
      int status; /* Serial port status bitmask */
      
      fd = open("/dev/cuaa0", O_RDWR | O_NOCTTY | O_NDELAY);
      if (fd == -1)
         perror("open_port: Unable to open /dev/ttyf1 - ");
      else 
        fcntl(fd, F_SETFL, 0);

/* Get serial line status  bits */

      ioctl(fd,TIOCMGET,&status);

/*    Signal Values, obtained from ttycom.h
 *
 *    1 - DSR (Data Set Ready)
 *    2 - DTR (Data Terminal Ready)
 *    4 - RTS (Request to Send)
 *    8 - ST (Secondary Transmit)
 *    16 - SR (Secondary Receive)
 *    32 - CTS (Clear to Send)
 *    64 - DCD (Data Carrier Detect)
 *    128 - RNG (Ring)
 *    256 - DSR (Data Set Ready)
 */

      printf ("Current Serial Settings:");
       
      if (status >= TIOCM_DSR) {
         status -= TIOCM_DSR;
         printf (" DSR ");
      }
      
      if (status >= TIOCM_RNG) {
         status -= TIOCM_RNG;
         printf (" Ring ");
      }
      
      if (status >= TIOCM_CD) {
         status -= TIOCM_CD;
         printf (" DCD ");
      }
      
      if (status >= TIOCM_CTS) {
         status -= TIOCM_CTS;
         printf (" CTS ");
      }
      
      if (status >= TIOCM_SR) {
         status -= TIOCM_SR;
         printf (" SR ");
      }
      
      if (status >= TIOCM_ST) {
         status -= TIOCM_ST;
         printf (" ST ");
      }                        

      if (status >= TIOCM_RTS) {
         status -= TIOCM_RTS;
         printf (" RTS ");
      }
      
      if (status >= TIOCM_DTR) {
         status -= TIOCM_DTR;
         printf (" DTR ");
      }            
      
      if (status >= TIOCM_LE) {
         status -= TIOCM_LE;
         printf (" DSR ");
      }                  

      printf("\n");      

/* SET STATUS to 1 (DTR and RTS lowered). Previous status was 7 (DSR, DTR,
 * RTS high).
 */
      status = 1;
      
      ioctl(fd,TIOCMSET,status);

/* Check status after setting serial control bits */
      
      ioctl(fd,TIOCMGET,&status);

      printf ("Current Serial Settings:");
       
      if (status >= TIOCM_DSR) {
         status -= TIOCM_DSR;
         printf (" DSR ");
      }
      
      if (status >= TIOCM_RNG) {
         status -= TIOCM_RNG;
         printf (" Ring ");
      }
      
      if (status >= TIOCM_CD) {
         status -= TIOCM_CD;
         printf (" DCD ");
      }
      
      if (status >= TIOCM_CTS) {
         status -= TIOCM_CTS;
         printf (" CTS ");
      }
      
      if (status >= TIOCM_SR) {
         status -= TIOCM_SR;
         printf (" SR ");
      }
      
      if (status >= TIOCM_ST) {
         status -= TIOCM_ST;
         printf (" ST ");
      }                        

      if (status >= TIOCM_RTS) {
         status -= TIOCM_RTS;
         printf (" RTS ");
      }
      
      if (status >= TIOCM_DTR) {
         status -= TIOCM_DTR;
         printf (" DTR ");
      }            
      
      if (status >= TIOCM_LE) {
         status -= TIOCM_LE;
         printf (" DSR ");
      }                  

      printf("\n");      
}


To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-hackers" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?Pine.BSF.4.21.0106281752090.22968-100000>