Pages: [1]   Go Down
Author Topic: Hardware serial with 2 arduino's  (Read 776 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 18
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
Serial.println("hallo"); // once per 20ms

Arduino 2
Code:
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:
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.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25732
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What happened when you used "Serial.available()"?
Logged

"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.

0
Offline Offline
Newbie
*
Karma: 0
Posts: 18
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Adding

Code:
     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?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
while(Serial1.available() > 0)
{
   // Read what's available
   // Write it out
}
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 18
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
void setup() {
 Serial.begin(57600);
}

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

Arduino 2 (code in c++)
Code:
#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:
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.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 18
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 18
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: October 30, 2010, 08:58:10 am by rubendel » Logged

Pages: [1]   Go Up
Jump to: