Not quite right. Once you get the \n you need to read the buffer. for example
#include "mbed.h"
#include "MODSERIAL.h"
DigitalOut led1(LED1);
DigitalOut led2(LED2);
DigitalOut led3(LED3);
DigitalOut led4(LED4);
MODSERIAL arduino(p9, p10);
bool newline_detected = false;
void rxCallback(MODSERIAL_IRQ_INFO *q) {
MODSERIAL *serial = q->serial;
if ( serial->rxGetLastChar() == '\n') {
newline_detected = true;
}
}
int main() {
arduino.baud(9600);
arduino.attach(&rxCallback, MODSERIAL::RxIrq);
while(1) {
if (newline_detected) {
newline_detected = false;
char c = arduino.getc();
switch (c) {
case '1': led1 = !led1; break; // Toggle LED1
case '2': led2 = !led2; break; // Toggle LED2
case '3': led3 = !led3; break; // Toggle LED3
case '4': led4 = !led4; break; // Toggle LED4
}
}
if (button == 1) {
arduino.printf("From mBed\n");
}
}
}
In the above example if the arduino sends "1\n" then the Mbed will toggle LED1, "2\n" will toggle LED2, etc.
You still need to read the RX buffer with getc() and actually get the characters in the buffer and do something with them.
If you just wanted to echo what the arduino sends then you need something like this:-
if (newline_detected) {
newline_detected = false;
char c;
while((c = arduino.getc()) != '\n') {
arduino.putc(c);
}
}
Hey everyone. I am in the process of making a little echo program to echo back whatever ir receives over serial connection. I have another microcontroller hooked up to an LCD and printing whatever it receives to LCD. So far I have sent "Hello world to this other microcontroller and had it printed to the LCD. I am now trying to send "Hello to you" to the mbed, have it wait a second, then send back the message to be displayed on the LCD. Any ideas whats going wrong with my code?