Serial.attach()

06 Jun 2011

Hello,

I have some problems with my code :

#include "mbed.h"

DigitalOut led1(LED1);
DigitalOut led2(LED2);
DigitalOut led3(LED3);

Serial computer(USBTX, USBRX);

// This function is called when a character goes into the TX buffer.
void txCallback() {
    led2 = !led2;
}

// This function is called when a character goes into the RX buffer.
void rxCallback() {
    led3 = !led3;
}

int main() {
    computer.baud(115200);
    computer.attach(&txCallback, Serial::TxIrq);
    
    computer.attach(&rxCallback, Serial::RxIrq);

    while (1) {
        led1 = !led1;
        wait(0.5);
        computer.putc('A');
        wait(0.5);
    }
}

At beginning, Led 1 & Led 2 blink. it's ok.

When mbed receive some instructions through serial pin, Led 3 turn on, and then nothing happens. 3 leds stop blinking.

Do you have an issue for this problem please .??

Thanks :)

06 Jun 2011

Hello.

Look at your code there.

This function is called when a character goes into the RX buffer. void rxCallback() { led3 = !led3; }

but this code can only be executed only and only if the mbed receives something. Try to verify if your mbed effectively receives some data.

for example use this,

char c; while (1) { c = computer.getc(); led1 = !led1; wait(0.5); computer.putc(c); wait(0.5); }

it should normally reprint what you type.

07 Jun 2011

den rick wrote:

for example use this,

char c; while (1) { c = computer.getc(); led1 = !led1; wait(0.5); computer.putc(c); wait(0.5); }

Hi,

Thank you for you help, I tried your code but it seems to freeze in the while loop. I mean at beginning LED 1 didn't blink.

So I tried to change my code and it seems to work, but I have to test to be sure :

#include "mbed.h"

DigitalOut led1(LED1);
DigitalOut led2(LED2);
DigitalOut led3(LED3);

Serial computer(USBTX, USBRX);

// This function is called when a character goes into the TX buffer.
void txCallback() {
    led2 = !led2;
}

// This function is called when a character goes into the RX buffer.
void rxCallback() {
    led3 = !led3;
     computer.putc(computer.getc());
}

int main() {
    computer.baud(115200);
    
    computer.attach(&txCallback, Serial::TxIrq);
    computer.attach(&rxCallback, Serial::RxIrq);
    

    while (1) {
        led1 = !led1;
        wait(0.5);
    }
}

10 Jun 2011

Hi,

So it's OK.

This code below doesn't work, i think it's because we have to clean (empty) buffer memory :

// This function is called when a character goes into the RX buffer.
void rxCallback() {
    led3 = !led3;
}

So we have to do this :

// This function is called when a character goes into the RX buffer.
void rxCallback() {
    led3 = !led3;
     computer.putc(computer.getc());
}

If someone have an order idea about this, I will be glad to read your answer.

Thanks.

17 Jun 2015

try with LEDs in DigitalInOut rather than DigitalOut