I've got the microsecond counts of the time the serial line spends down, how do I use this to get the baud rate?
How can I use pulseIn on an arduino to get the baud rate of the serial line being monitored?
I've got the microsecond counts of the time the serial line spends down, how do I use this to get the baud rate?
How can I use pulseIn on an arduino to get the baud rate of the serial line being monitored?
Look for the shortest pulse you can find; apart from glitches, that will generally be the bit period of the line.
tocpcs:
I've got the microsecond counts of the time the serial line spends down, how do I use this to get the baud rate?How can I use pulseIn on an arduino to get the baud rate of the serial line being monitored?
I played around awhile trying to write such a application. I did get it to function pretty well if the 'testing' character was a 'U' or some character that has a valid single space bit enclosed around two mark bits. However many ascii characters won't have a single space bit to measure in the character frame. So unless you have some control over the serial stream you are trying to analyze, you may find this task to be very challenging to say the least, at least I did. Anyway it was fun playing with it and here it is if you wish to experiment with it.
/*
First stab at a auto baudrate detection function. This uses the pulseIn command
to determine what speed an unknown serial link is running at. Detects and sets the standard baud
rates supported by the Arduino IDE serial monitor. Uses character "U" as a test character
I'm sure characters with multible zero bits in a row may fake out proper detection. If data is
garbled in the serial monitor then the auto baud rate function failed.
This is just a demo sketch to show concept. After uploading the sketch, open the serial monitor
and pick a random baudrate and then start sending U charaters. The monitor will print out
what baudrate it detected. It will default to 9600 baud if found a bit
width too long for a standard rate used by the serial monitor. Not that as written, this is a
'blocking' function that will wait forever if no character are being sent.
Note that while the serial monitor has a 300 baud option, the Arduino hardware serial library
does not seem to support that baud rate, at least for version 22, in my testing.
By "retrolefty" 1/22/11
*/
int recPin = 0; //the pin receiving the serial input data
long baudRate; // global in case useful elsewhere in a sketch
long rate=10000;
void setup()
{
pinMode(recPin, INPUT); // make sure serial in is a input pin
digitalWrite (recPin, HIGH); // pull up enabled just for noise protection
baudRate = detRate(recPin); // Function finds a standard baudrate of either
// 1200,2400,4800,9600,14400,19200,28800,38400,57600,115200
// by having sending circuit send "U" characters.
// Returns 0 if none or under 1200 baud
Serial.begin(baudRate);
Serial.println();
delay(100);
Serial.flush();
Serial.print("Detected baudrate at ");
Serial.println(baudRate);
Serial.println();
Serial.print("rate in usec = ");
Serial.println(rate);
}
void loop()
{
if (Serial.available() > 0)
{
// get incoming byte:
int inByte = Serial.read();
Serial.print(inByte,BYTE);
}
}
long detRate(int recpin) // function to return valid received baud rate
// Note that the serial monitor has no 600 baud option and 300 baud
// doesn't seem to work with version 22 hardware serial library
{
/*
long baud, rate = 10000, x;
for (int i = 0; i < 10; i++) {
x = pulseIn(recpin,LOW); // measure the next zero bit width
rate = x < rate ? x : rate;
*/
long baud, x;
for (int i = 0; i < 5; i++)
{
while(digitalRead(recpin) == 1){} // wait for low bit to start
x = pulseIn(recpin,LOW); // measure the next zero bit width
rate = x < rate ? x : rate;
}
// long rate = pulseIn(recpin,LOW); // measure zero bit width from character 'U'
if (rate < 12)
baud = 115200;
else if (rate < 20)
baud = 57600;
else if (rate < 29)
baud = 38400;
else if (rate < 40)
baud = 28800;
else if (rate < 60)
baud = 19200;
else if (rate < 80)
baud = 14400;
else if (rate < 150)
baud = 9600;
else if (rate < 300)
baud = 4800;
else if (rate < 600)
baud = 2400;
else if (rate < 1200)
baud = 1200;
else
baud = 0;
return baud;
}
Lefty
Capital 'Z' would seem a good candidate, and used to be used a lot, IIRC, in applications that did auto rate sensing.