Seeduine Mega / Serial

Hi,

( tested on arduino17 and arduino21 )

setup:

2 seeduino mega

mega 1 talking to mega 2 and then output to usb/serial and displayed on a serial console. After some test, even in 9600, at some point, something got corrupted and data is garbage.

MEGA1 (Serial2 ) -> MEGA2 (Serial/USB) -> Serial Monitor

Code on Mega2:

int b = 0;      // for incoming serial data
int ledPin = 13;                 // LED connected to digital pin 13
void setup() {
  // initialize both serial ports:
  Serial.begin(9600); 
  Serial.flush();
    pinMode(ledPin, OUTPUT);      // sets the digital pin as output
     Serial.println("");
      Serial.println("");
    Serial.println("USB Serial enable!");
          Serial.println("");
                Serial.println("");
}

void loop()
{
  digitalWrite(ledPin, HIGH);   // sets the LED on
  // send data only when you receive data:
   while(Serial.available())
    {              
        delay(1);
        b=Serial.read(); 
        if(b != -1 ) 
        {
            Serial.print((char)b); 
        }
        else
        {
          Serial.println();
        }
    }    
          delay(10);    
        digitalWrite(ledPin, LOW);    // sets the LED off
 delay(10);
          
}

Maybe my code on the mega2 is garbage or is there hardware limitation I dont see?

Regards,

Mart

but I can get it to work properly

Can or Can’t? It kind of takes all the air out of your question? :wink:

The Arduino IDE uploads sketches to a mega at 57600 baud using the first hardware serial port. I suspect that your best avenue for help is to post your code here and let the software gurus here (not me :wink: ) have a look at it.

Lefty

retro,

one of the issues is better explained now. Any thought?

thanx

Mart

Some questions.

  Serial.flush();

You've just enabled serial port access, and the first thing you do is throw away random amounts of data. Why?

   while(Serial.available())
    {              
        delay(1);
        b=Serial.read();
        if(b != -1 )
        {
            Serial.print((char)b);

Why the delay? There is data to read. What are you waiting for?

Since you checked that there is data to read, Serial.read() will never return a -1, so why the check?

Is this all the code? The same serial port is being used to output data as is used to input data. If that serial port is connected to the other Mega, how is data getting to the serial monitor?

Some questions. Code:

Serial.flush();

You've just enabled serial port access, and the first thing you do is throw away random amounts of data. Why?

I agree that the flush() is not necessary.

Code:

while(Serial.available()) { delay(1); b=Serial.read(); if(b != -1 ) { Serial.print((char)b);

Why the delay? There is data to read. What are you waiting for?

delay: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1202440260 did experiment something similar a while ago at 4800. I will remove it and do some test

also: http://techie.wordpress.com/2010/02/12/arduino-setting-delay-time-via-serial-input/

"I found that at a baud rate of 9600, the Arduino was executing the run loop faster than the serial bytes were being received. As a result if I sent the number 100, the while loop only ran once and so the value was set to 1. The one ms delay gives the Arduino time to receive the next byte."

Is this all the code? The same serial port is being used to output data as is used to input data. If that serial port is connected to the other Mega, how is data getting to the serial monitor?

MEGA 2 RX Serial is getting data from the MEGA 1 Serial2 TX. then mega 2 TX ( it is binded to usb/serial as well )

obviously, I am doing something wrong. I will revisit my code and test more tonight.

thanx

Mart

doing:

MEGA1 (Serial2 ) -> (Serial2) MEGA2 (Serial/USB) -> Serial Monitor

seems to work so far uhm...

Mart

seems to work so far uhm...

You say that like it's a bad thing. ;)