5 years, 8 months ago.

Communicating between two mbeds using serial

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.

Can you put your code between <<code>> and <</code>> (on seperate lines), now it is hard to read it.

posted by Erik Olieman 07 Feb 2014

Alot clearer :).

I can look at it later (don't have mbed now), but what you already can try is replacing the printf by putc (printf should work, I believe when it is called like this it isn't buffered, but not 100% sure, although that doesn't explain why it does receive something). Also you can send your received character to your PC to check what it receives. (If you use printf with %d you get the ascii equivalent of the received value, can be useful if it receives something that isn't printable).

posted by Erik Olieman 07 Feb 2014

Thank you...looking forward to your answer

posted by Kosy Onyenso 07 Feb 2014
Comment on this question

1 Answer

5 years, 8 months ago.

Tested it myself, works fine here.

One idea: do your devices have a common ground? If they are powered by one PC for example, or preferably if you explicitly connected the ground lines, it will be the case. If one uses an external power supply it probably isn't the case.

Maybe the common ground thing...i'll check my connections on Monday when I'm back in school

posted by Kosy Onyenso 08 Feb 2014

I solved it....it seems the second loop in main.cpp was looping infinitely so after only one transmission, the LED just blinks forever and nothing else is sent. Corrected now. Thanks for the help.

posted by Kosy Onyenso 10 Feb 2014

You need to log in to post a question