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

[ale] Autodetecting bps

I haven't done this sort of thing myself, but it seems that there might
be more to autodetecting a serial connection than what appears at
first glance.  How you write an autodetection procedure depends
heavily on your presumptions about the remote device.  The fewer
presumptions you make, the more complicated it becomes.

Maybe you've thought about these things already, and maybe you know
some things about the remote device that simplifies the task.  But in
case you've overlooked something:

You are assuming the remote device is configured to echo everything it
receives.  But do you know what the remote device will do if it
receives data with framing or parity errors?  If it discards them, you
will be waiting for an echoed character that will never be sent.  Or
maybe the remote device receives the data, and because of timing  
errors, interprets it as multiple characters and echoes them all.  You
would want to flush the received data each time before testing with
new data.  You probably would also want to be checking for framing
errors on received data.

The speeds you are testing seem unusual.  As written, it tests 50,
100, 200, 400, 800, 1600, 3200, 6400, 12,800, 25,600, 51,200....  Most
of these rates are not common, and most of the common ones are skipped
over.  Also, since this sequence will never equal 921600, the loop
will never end.

Speed is not the only thing that might be of concern.  There's also
the number of data bits and stop bits, whether there is a parity bit,
and whether parity is even, odd, mark or space.  If, for example, the
remote device only accepts odd parity, then you will fail to
communicate unless you match it.  Also, the failure may be
intermittent.  If you are sending 8 bit data with no parity and the
remote is expecting 7 bit data plus 1 odd parity bit, then the remote
device would accept your 'a' but drop the letter 'b'.  Sending more than
just a single character might be a better test(comparable to how a
modem uses the two character sequence 'at' as a command prefix to
detect the computer's baud, # of data bits, and parity).

There's probably numerous other pitfalls that I haven't thought of.
It's hard to say what your specific problem might be without getting
into the details of your hardware setup and program.


-----Original Message-----
From:	Chris Fowler [SMTP:ChrisF at computone.com]
Sent:	Monday, May 07, 2001 11:16 AM
To:	'ale at ale.org'
Subject:	[ale] Autodetecting bps

Interesting problem I'm working on right now.  I'm trying to write simple
code that attempts to autodetect the serial speed of
anyhting attached to it.  I'm attempting to do this by writing an 'a' out
the port and tryting to see if an 'a' returns.

Simple code that is shortened

open("/dev/ttyS0, O_NOCTTY, O_RDWR)

set termios
for (speed = 50; speed != 921600; speed *= 2)
	set symbolic speed
	write 'a'
	read 'a'
	if 'a' = 'a'
	  	return MATCH

It seems that I'm blocking at th read after the write.  Is there a better
way I can do this?


To unsubscribe: mail majordomo at ale.org with "unsubscribe ale" in message body.