tech-userlevel archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: How to set custom baud rates

> On NetBSD, is it possible to set custom baud rates for the serial
> ports from an application written in C?

In general, yes.

Depending on the serial-line interface in use, it may be restricted.
I've seen serial port hardware where the speed is set in hardware and
software cannot affect it (or, I think, even detect it, except by
timing how long it takes to send characters).  Others may be capable of
only a small set of speeds.  But many, probably most, can handle a
large variety of speeds, typically 256 or 512 or some such different
speeds.  (You usually have finer control at the slow end of the
spectrum - it's typically a divider chain with a settable divisor
dividing down a fixed clock.)

If your hardware is properly documented, its section 4 manpage should
describe its capabilities.  For example, the com(4) manpage on one
machine I have handy says

     Input and output for each line may set to one of following baud rates;
     50, 75, 110, 134.5, 150, 300, 600, 1200, 1800, 2400, 4800, 9600, 19200,
     38400, 57600, or 115200, or any other baud rate which is a factor of

This is slightly wrong, since (for example) 110 does not divide 115200,
but the general idea is right; the hardware probably can do "115200
divided by any integer 1..65536" or some such.

In general, the right thing to do is to try to set the rate you want
(since you say "in C", you presumably want an API; termios(4) and
tcsetattr(4) should be enough to get you started).  If you get an
error, the driver refused it (likely because the hardware can't do it);
if you don't get an error, then fetch the speed and see what it
actually got set to - I've seen drivers that will accept anything but
set the closest achievable speed, which (especially near the fast end)
can be significantly different from what you asked for.  Others will
error out on things too far from achievable speeds but will always
report what you set rather than the actual rate.

If it really matters to you, go read the driver for your hardware.

/~\ The ASCII                             Mouse
\ / Ribbon Campaign
 X  Against HTML      
/ \ Email!           7D C8 61 52 5D E7 2D 39  4E F1 31 3E E8 B3 27 4B

Home | Main Index | Thread Index | Old Index