Missing first charater form Serial

27 Feb 2012

Hi,

can anyone help with this code ? I am missing the first char form the PC with the code.

#include "mbed.h"
#include <string>

void Rx_interrupt(void);

Serial term(USBTX, USBRX); // tx, rx

DigitalOut bootup_led(LED1);
DigitalOut config_led(LED2);
DigitalOut pc_config_led(LED3);
DigitalOut Irq_led(LED4);

string rx_string = "\0";

int main() 
{
    term.baud(115200);
    term.attach(&Rx_interrupt, Serial::RxIrq);
        
    bootup_led = 1;
    
    while(true)
    {
        if(rx_string == "c" || rx_string == "C")
        {
            config_led = 1;
        }
        else
        {
            config_led = 0;    
        }
        
        if(rx_string == "PC-CONFIG")
        {
            pc_config_led = 1;
        }
        else
        {
            pc_config_led = 0;    
        }
        wait(1);
        
        term.printf("\x1B[2J");    //VT100 erase screen
        term.printf("\x1B[H");     //VT100 home
        term.printf("rx_string: %s\r\n", rx_string);
    }
        

}

void Rx_interrupt()
{
    Irq_led = 1;
    
    volatile char rx_buffer;
    //volatile char rx_buffer_array[25];
    volatile int rx_buffer_array_count = 0;
    
    rx_string.clear();

    while(term.readable())
    {
        rx_buffer = term.getc();
        
        term.putc(rx_buffer);
        
        rx_string += rx_buffer;
    }
    
    wait_ms(200);
    Irq_led = 0;
}
27 Feb 2012

I am not sure if this will solve your problem but you need to use a separate serial object in interrupts:

Serial term(USBTX, USBRX); Serial term_isr(USBTX, USBRX);

I forgot the reason for this quirk but it might help.

Also, you shouldn't do any sort of waiting in an ISR, it's bad programming practice. In your ISR routine you need to get in and get out as fast as possible.

28 Feb 2012

Thanks Igor,

Problem solved, it was the "wait" and "rx_string.clear()" that caused the problem.

Best Steen...