Communicating between two mbeds using serial

07 Feb 2014

Hi, I am trying to connect two mbeds (both FRDM-KL25Z) together using serial connection. The code for the first one is :

main.cpp

#include "mbed.h"
Serial myarduino(PTD3, PTD2);
Serial btserial(PTC4, PTC3);
DigitalOut myled(LED1);//////////
 
int main() 
{
    char data;
    btserial.baud(9600);
    myarduino.baud(9600);
    while(1){
    data = 'u';
    //data = btserial.getc();
    myarduino.printf("%c",data);
    wait(0.2); 
    while(data == 'u') {
        myled = 1;
        wait(0.2);
        myled = 0;
        wait(0.2);
    }}
} 

This mbed is meant to send the letter 'u' to the second mbed. The code for the second mbed is:

testmbedconnection.cpp

#include "mbed.h"
 
Serial freescale(PTD3, PTD2);
Serial btserial(PTC4, PTC3);
DigitalOut myled(LED1);//////////
 
int main() 
{
    char data;
    btserial.baud(9600);
    freescale.baud(9600);
    while(1){
    if(freescale.readable()) 
    {
  data = freescale.getc();
   if(data == 'u') {
        myled = 1;
        wait(0.2);
        myled = 0;
        wait(0.2);
    }}}
}

Ideally, i set the LED on mbed 2 to blink when data received is u. This doesn't work. I commented out the condition and set a new condition for the LED to blink if "if(freescale.readable()) " is true. That worked. It means my mbed 1 is readable but the data is not being sent. Please any idea what i'm doing wrong? All help would be appreciated. Ps my connections are Rx1 to Tx2 and Tx1 to Rx2 so I'm almost certain that's not the issue.

09 Feb 2014

Is it a timing problem? You are only writing one 'u' to the stream, and then looping forever flashing the led. If the receiver doesn't get that one 'u' then it will also just loop endlessly. I'd try a more robust handshake, and better diagnostics (eg, if there is a readable character, then what is it?). Serial communiation is inherently unreliable, especially when synchronizing at the start of a stream.

10 Feb 2014

Ian McCulloch wrote:

Is it a timing problem? You are only writing one 'u' to the stream, and then looping forever flashing the led. If the receiver doesn't get that one 'u' then it will also just loop endlessly. I'd try a more robust handshake, and better diagnostics (eg, if there is a readable character, then what is it?). Serial communiation is inherently unreliable, especially when synchronizing at the start of a stream.

I changed the loop like you said as it was looping forever.. Thanks for the help. Works now.

30 May 2014

Hi friend,

I get two mbed now, and I am interesting to your topic. How do you link two mbed? Which Transmission line do you use. Looking forward your reply.

Best Regards