Go Down

Topic: Hardware serial with 2 arduino's (Read 860 times) previous topic - next topic

rubendel

In the simplified version of my problem Arduino 1 is sending 'hallo' strings to it's UART at 50Hz. Arduino 2 (a Mega) is reading everything from a hardware serial port correctly connected to Arduino 1 and sending it to a second hardware serial port. Now when I connect my pc to this second port, I see strange stuff in my terminal:

Arduino 1
Code: [Select]

Serial.println("hallo"); // once per 20ms


Arduino 2
Code: [Select]

void loop() {
     int v = Serial1.read();
     // Keep polling for valid input
     while (v == -1) {
           v = Serial1.read();
     }
     // Only allow characters
     if (v < 127) {
           Serial.write(v);            
     }
}


An excerpt of the output
Code: [Select]

haHHhalloCHhalloCHhaHHhallHHhXH
halHHhalHHhalloCHhallHHhalHHhalHHhXH
hallHHhalloCHhalloCHhallo
HhalHHhaHHhXH
hallo
HhallHHhallHHhaHHhalloCHhalHHhXH
halloCHhalloCHhaH
hallo
HhallHHhalloCH


All speeds are 57600 baud. Output us as espected when I connect the PC directly to the first Arduino its hardware serial port.

AWOL

What happened when you used "Serial.available()"?
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

rubendel

Adding

Code: [Select]

     while (Serial1.available() == 0) {
     }


does not change the behavior, am I doing it right?

One other thing I don't understand, I am only sending 5 normal characters, why are there numbers above 127 popping up?

PaulS

Why do you want to do nothing when there is no serial data? You should turn that around, and do something when there is serial data.
Code: [Select]
while(Serial1.available() > 0)
{
  // Read what's available
  // Write it out
}

rubendel

Hi PaulS, thanks for your reply, but I don't think that it is fundamentally changing the situation, it just moves the place where more iteration is done to the loop() method.

I have made a cleaner test case (including your suggestion), the code:

Arduino 1 (code in Arduino language)
Code: [Select]

void setup() {
Serial.begin(57600);
}

void loop() {
 Serial.println("hi");
 delay(1000);
}


Arduino 2 (code in c++)
Code: [Select]

#include <HardwareSerial.h>
#include <wiring.h>

extern "C" void __cxa_pure_virtual() {
     while (1)
           ;
}

void setup() {
     Serial.begin(57600);
     Serial1.begin(57600);
}

void loop() {
     while (Serial1.available() > 0) {
           int val = Serial1.read();
           Serial.write(val);
     }
}

int main(void) {
     init();
     setup();
     for (;;) {
           loop();
     }
}


The output:
Code: [Select]

hiCáhi
hi
hZCáhi
áhi
hi
hZCáhiCáhi
áhi
hi
hi
hi
hZCáhZCáhiCáhiCáhi
áhi
áhi
hi
hi
áhi


It looks like sometimes the 'i' is missing, sometimes the '\r' or '\n' is missing, I really don't understand what's going on.

rubendel

Ah I have gotten a bit further. I was using the 9DOF Razor from sparkfun (http://www.sparkfun.com/commerce/product_info.php?products_id=9623) as the sending Arduino. When I swap that Arduino with an Arduino Duemilanove, things just work as I would expect.

When using a 3.3V FTDI interface directly on the Razor, everything is fine too. So the problem is probably in the conversion between the 3.3V signal of the Razor to the 5V signal of the second Arduino (a Mega 2560).

I am using a signal level converter board also from Sparkfun, but the results are the same when not using that board at all. So I am still stuck, but getting there hopefully :)

rubendel

#6
Oct 30, 2010, 03:57 pm Last Edit: Oct 30, 2010, 03:58 pm by rubendel Reason: 1
For anybody having the same problems, what finally fixed it is was changing the baud rate to 38400 instead of 57600. Maybe it has something to do with the difference in clockspeed (8MHz and 16MHz) I still don't know, but this works for me.

Go Up