[TriLUG] C linux programming question

Scott G. Hall ScottGHall at BellSouth.Net
Sat May 24 15:06:19 EDT 2003


This is most certainly doable in C -- I have done it and use it here
at the house for monitoring various things.

Please note that serial interfaces are not like simple input ports.
A positive value (a '1') is defined as -12v, and a negative is +12v.
The voltage of the value actually is anywhere from 3 to 15 volts; in
other words a positive is -15v to -3v, and a negative is +3v to +15v.
The -3v to +3v range is undefined and is specified to represent no
value change.

That said, the two pins that are most interest for on-off data is CD
(carrier detect, pin 8 on a 25-pin) and CTS (clear to send, pin 5 on
a 25-pin).  These are easy to access in C using the ioctl() function
and in UNIX or Linux adjustments to the /etc/gettydefs (or equiv) file.
(You need to turn on hardware handshaking in the driver to use these.)

The CD value is most used by programs like getty to determine if the
terminal is recycling (this would be the DTR signal from the terminal
connected to both the DSR and CD pins on the host), and when toggled
to positive, usually will cause getty to resend the login.  The CTS
signal is used for hardware handshaking, when toggled to positive it
means that the terminal can receive characters from the host, when
negative it means that the terminal serial buffer is full and to
pause send characters until it can process what it has.

However, both are just values returned by C's ioctl() function, and
you can query them easy enough.

A better solution though may be to use your embedded controller's
parallel port.  Through ioctl() function calls, you can monitor 4 bits
directly, and with a multiplexer up to 16 bits.  On a parallel port,
+5v is a positive, 0v is a negative.

No matter which port you use, you want to buffer your vehicle's
voltage and signals through both a regulator and transistor or OP-AMP
to make sure you don't fry your parallel port.

On Fri, 23 May 2003 21:41:35 "Jon Carnes" <jonc at nc.rr.com> wrote:

>Ed Hill is the guy you want to talk to (see the archives from last week),
>but this is definitely do-able.  I used to do it using assembler but it
>should work fine in C as well (or you can write an assembler subroutine to
>monitor the voltage).
>
>Note: unless you get an A/D card of some kind you are only going to know 0
>or 1 and not any actual voltage values - or guesstamates.
>
>Jon Carnes
>
>----- Original Message -----
>From: "Greg Brown" <gregbrown at mindspring.com>
>To: <trilug at trilug.org>
>Sent: Friday, May 23, 2003 8:57 PM
>Subject: [TriLUG] C linux programming question
>
>> Hey all.  Is it possible, using C, to monitor voltage on one pin of a
>> serial port when voltage is supplied from an outside source or do I
>> have to knuckle under and learn some assembly for this task?  What I'm
>> after here is with an embedded linux machine mounted in a car and I
>> want to have power supplied by an always-on supply in the car.  I'd
>> also like to have a pin on a serial port connected to a "only on when
>> running" wire (somehow).  So when the voltage drops to zero on the
>> "only on when running" pin the embedded device will shut itself down
>> automatically (the device would be turned on manually via an "on"
>> button).
>>
>> Is this theoretically possible using C?
>>
>> Greg
>

-- 
Scott G. Hall,
Raleigh, NC, USA
ScottGHall at BellSouth.Net





More information about the TriLUG mailing list