Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
10 years, 11 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.
1 Answer
10 years, 11 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.
Can you put your code between
posted by Erik - 07 Feb 2014<<code>> and <</code>>
(on seperate lines), now it is hard to read it.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 - 07 Feb 2014Thank you...looking forward to your answer
posted by Kosy Onyenso 07 Feb 2014