I'm trying to modify the Razor IMU Firmware (Arduino) so that can set the Offsets Matrix during bootup.
I need to parse a String containing an int and a \r as a line delimiter... Sounds simple, but...
Seems like the first char is correct all others are only garbage.
int readInt()
{
int tmp;
char line[100];
int done = 0;
int count = 0;
while (done == 0) {
if (Serial.available() > 0) {
line[count] = (char)Serial.read(); // store the char
if (line[count++] == '\r'){ // if its a CR,
line[count] = '\0'; // zero-terminate it
Serial.println(line);
done = 1; // and print it.
Serial.println("loop"); // re-prompt
count = 0; // and reset the index.
}
}
}
tmp = atoi(line);
return tmp;
}
Serial Input 12345\r
Output: Ã1¦¢5Ã
loop
when i send single chars using Serial Monitor it seems to work correctly. But when i send multiple chars at once, it does not work.
int AN_OFFSET[6]={
0,0,0,0,0,0}; //Array that stores the Offset of the sensors
int readInt()
{
int tmp;
String readString;
char line[100];
int done = 0;
int count = 0;
while (done == 0) {
if (Serial.available() > 0) {
delay(1);
line[count] = (char)Serial.read(); // store the char
if (line[count++] == '\r'){ // if its a CR,
line[count] = '\0'; // zero-terminate it
Serial.println(line);
done = 1; // and print it.
Serial.println("loop"); // re-prompt
count = 0; // and reset the index.
}
}
}
// buffer[i++]='\0';
// char cstr[100]; // Or something long enough to hold the longest file name you will ever use.
// readString.toCharArray(cstr, sizeof(cstr));
tmp = atoi(line);
return tmp;
}
int waitforcommand()
{
int done = 0;
while (done < 2) {
if (Serial.available() > 0) {
char c = Serial.read(); //gets one byte from serial buffer
if (c == '#')
{
return 2;
}
if (c == '!')
{
return 1;
}
}
}
return done;
}
void setup()
{
Serial.begin(115200);
Serial.println("Menu:");
delay(2000);
if (waitforcommand()==2) //check if there is charecter in the serial buffer
{
Serial.println("Enter Values");
//Read Set Values
AN_OFFSET[0] = readInt();
AN_OFFSET[1] = readInt();
AN_OFFSET[2] = readInt();
AN_OFFSET[3] = readInt();
AN_OFFSET[4] = readInt();
AN_OFFSET[5] = readInt();
}
for(int y=0; y<6; y++)
Serial.println(AN_OFFSET[y]);
}
void loop() //Main Loop
{
delay(10);
}
The only other thing that I can suggest is that you store the value from Serial.read() into a local variable, and print that before storing it in the array.
Yesterday i bought an arduino duemilanove and my code works with it. I don't know why it does not work on my Razor IMU with FTDI Setup.
I got an working version. It writes an char wait 50ms and then send the next char via the Serial port to the Arduino. At this slow rate it is working correctly. It is very slow and a real pain, but i guess i'll have to live with that.
I don't understand why the direction from the arduino to the pc works well but not the other way round...
petertester:
I don't understand why the direction from the arduino to the pc works well but not the other way round...
Since the serial connection is asynchronous, it's possible that there is a discrepancy between the two clocks and the PC is better at syncing to the incoming signal than the Arduino is.
Since the problem seems to be about a problem with basic comms, I suggest you would be better to investigate it with a simple 'echo' sketch that simply prints out what it receives (with descriptive text as PaulS advises). If the hard-coded string values come out correctly then you can assume that Arduino-to-PC comms works correctly; if the echoed text then comes out incorrectly, you have proved that PC-to-Arduino comms is working incorrectly. Then you could try varying the comms line parameters to try to eliminate the error.