7 years, 4 months ago.

serial communication mbed and arduino

Hi Guys, i have a project to make a communication between arduino mega and nucleo446 using mbed. i have done sending the char from mbed to arduino and its worked. but there are problem. the problem is the data is not received in arduino in real time, there is a delay about 0.5sec... here is my code :

mbed

#include "mbed.h"
 
Serial pc(USBTX, USBRX);    // tx, rx
Serial mega(PA_9, PA_10); //TX, RX
Timer timer;

int main() {
    pc.baud(9600);
    mega.baud(9600);
    pc.printf("Starting...\n\r");
 
    timer.start();
    while (1) {
		if (pc.readable()){
			char x=pc.getc();
        if (timer.read() > 1) {
            pc.printf("Start Writing!\n\r");
            mega.putc(x);
            timer.reset();
					  pc.printf("Done Writing!\n\r");
        }
			}		
    }
}

Arduino Mega

int x;

void setup()
{

  Serial.begin(9600);
  Serial1.begin(9600);
  Serial.println("Starting...");
}

void loop() {
  if (Serial1.available()) {
    Serial.print("Got something!  ");
    x = Serial1.parseInt();
    Serial.println(x);
  }

}

can someone help me to reduce the delay?

Sorry For my bad English...

1 Answer

7 years, 4 months ago.

Try with a baud rate of 19200 for both send & receive sides. Faster ? Continue up the scale to 115200 and keep your cables relatively short (12" or so should be fine) since you are most likely working with CMOS to CMOS levels (mbed direct to Arduino).

Also do you see the output immediately without delays from the following code ?

int main() {
    pc.baud(115200);
    mega.baud(115200);

    while(1)
    {
     for(x=0; x<=0xff;x++)
         pc.printf("Starting %02x...\n\r",x);
    }

If the above is fast(er) then the issue is related to the next code chunk dealing with the timer routines.