Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 15 Jan 2003 13:20:42 +0200
From:      Giorgos Keramidas <keramida@ceid.upatras.gr>
To:        Paul Hamilton <paul@computerwest.com.au>
Cc:        freebsd-questions@FreeBSD.ORG
Subject:   Re: How do I monitor the serial CTS line via C programmatically?
Message-ID:  <20030115112042.GA26041@gothmog.gr>
In-Reply-To: <AGEHIFHGNEMPFNCPLONMCEMNEBAA.paul@compwest.com.au>
References:  <AGEHIFHGNEMPFNCPLONMCEMNEBAA.paul@compwest.com.au>

next in thread | previous in thread | raw e-mail | index | archive | help
On 2003-01-15 18:26, paul@computerwest.com.au (Paul Hamilton) wrote:
> I have been trying to monitor the serial CTS line.  I would like to
> log the fact that the CTS has changed state (active/non-active).
>
> I have been trying to adapt some Linux programs, but am having
> problems with adapting the POSIX TIOCMGET function.
>
> Is there a FreeBSD way?

You'd have to poll using ioctl(fd, TIOCMGET, &int) and detect CTS
changes by checking the TIOCM_CTS bit in the returned value.  The
following (untested) sample program should be easy to adapt to your
needs.  When TIOCM_CTS changes from on to off or vice versa, the
program should print the change.

    #include <sys/ioctl.h>

    #include <stdio.h>
    #include <unistd.h>

    int
    main(void)
    {
	    int bits;
	    int last;

	    if (ioctl(0, TIOCMGET, &bits) == -1)
		    err(1, "ioctl");
	    last = bits & TIOCM_CTS;
	    printf("CTS %s.\n", last ? "on" : "off");

	    while (1) {
		    if (ioctl(0, TIOCMGET, &bits) == -1)
			    err(1, "ioctl");
		    if ((last ^ (bits & TIOCM_CTS)) != 0) {
			    last = bits & TIOCM_CTS;
			    printf("CTS %s.\n", last ? "on" : "off");
		    }
		    usleep(100000);
	    }

	    return (0);
    }


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




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20030115112042.GA26041>