We have a Razor IMU (10736) sensor board connected to a RS-485 Breakout board, talking RS485 to the computer via a USB-RS485 Serial Interface. We use the Arduino IDE to program Razor's on-board ATmega328 (code is based on the Razor AHRS open source firmware).
Everything works fine, but for one detail: The Arduino-programmed Razor seems to need a small delay between the reading-in and the writing-out of a serial message. The way it works, is: the computer polls the razor, the razor reads the request and then responds by reporting it's data. The code looks like this (simplified) :
if(Serial.available() > 0) {
int incoming = (int) Serial.read(); // read data
delay(1); // ??? why necessary ???
digitalWrite(RTS_PIN, HIGH); // RE high, sets RS485-board to transmit
Serial.write(c); // send data
delay(1);
digitalWrite(RTS_PIN, LOW); // RE low, sets RS485-board to receive
}
There is the need for a delay of minimum 1 millisecond between executing Serial.read() and any form of output (digitalWrite and/or Serial.write()). We tried setting the value lower into the microseconds range, but always got corrupted data packages as a result. This problem does not occur when you only poll the razor once in a while, but when you repeatedly poll it at short time intervals (the computer waits for a response and then automatically triggers a new polling). This all is happening at a baud rate of 76800 right now.
Next we did tests by programming the Razor's on-board ATmega328 directly with C code. And this eliminated the need for the delay value! Reading and writing serial data could happen right after each other. But - as we'd like to stay within the Arduino language (for several reasons) - we'd like to figure how we could solve this problem in other ways.
In the Arduino firmware, the Serial library, is there any reason why there would have to be a delay of 1ms between Serial input and Serial output? And if yes, are there ways of changing that?
-
\$\begingroup\$ Can you look at the signals on an oscilloscope to ensure they are actually the exact baud rate you have the serial port configured for? \$\endgroup\$NickHalden– NickHalden2012年03月26日 15:02:15 +00:00Commented Mar 26, 2012 at 15:02
-
\$\begingroup\$ Not at the moment. But i did try with different baud rates (9600, 57600, 76800) and the problem seemed consistent. \$\endgroup\$evsc– evsc2012年03月26日 20:05:40 +00:00Commented Mar 26, 2012 at 20:05
-
\$\begingroup\$ Was the fix of directly coding in C also consistent across baud rates? \$\endgroup\$NickHalden– NickHalden2012年03月27日 00:37:17 +00:00Commented Mar 27, 2012 at 0:37
1 Answer 1
This problem was solved by programming with an older version of the Arduino IDE (i used Arduino 0022). Apparently Arduino 1.0 has a different buffered writes queuing system. See the answers on the Arduino forum for more information.