7 years, 1 month ago.

Direct Serial Output on Terminal != Serial read from mbed with output generation on Terminal

Dear mbed community,

I have a device denoted as "impact" generating a series of ASCII characters through Serial Comms port. As an example, the output format looks something like "ddd.ddm" where ddd.ddx are the reading and x is a fixed character.

When the device is connected to a serial to usb converter and the viewed in Terminal, it is showing the right results.

As soon read I read it using another mbed device and dump the data onto the Terminal the output appears to be displaced.

I have checked the baud rate, data bits, parity, stop bits between "impact", mbed and Terminal and they are all synced.

I need advice and suggestion how can this be done in the most appropriate way.

Also, note that the incorrect message is always consistent, perhaps I am not reading the data correctly?

The code that I am using is the most elementary as following:

#include "mbed.h"
 
Serial pc(USBTX, USBRX, 9600); // tx, rx
Serial impact(p13, p14, 9600);

DigitalOut my_led1(LED1), my_led2(LED2);
 
int main() {
    pc.format(8, Serial::None, 1);

    impact.format(8, Serial::None, 1);

while(1){
    
    while(impact.readable()){
        
        pc.putc((char)impact.getc());
        my_led2 =! my_led2;
    }
    
}
}

1 Answer

7 years, 1 month ago.

Hello Hua,
Try to use a serial interrupt service routine as follows:

#include "mbed.h"

Serial pc(USBTX, USBRX, 9600); // tx, rx
Serial impact(p13, p14, 9600);

//When using RTOS it's more safe to use RawSerial in ISR than Serial
//RawSerial pc(USBTX, USBRX, 9600); // tx, rx
//RawSerial impact(p13, p14, 9600);

DigitalOut my_led1(LED1), my_led2(LED2);

void onImpactReceived(void)
{
    while(impact.readable()) {
        pc.putc((char)impact.getc());
        my_led2 = !my_led2;
    }
    my_led2 = 0;
}

int main()
{
    pc.format(8, Serial::None, 1);
    impact.format(8, Serial::None, 1);
    impact.attach(&onImpactReceived);

    while(1) {
        my_led1 =! my_led1;
        wait_ms(500);
    }
}

It was a common mistaken where I just assuming RS232 as UART and plug the TX and RX directly on the mbed serial port. An interface conversation IC MAX232 was used to resolved this issue.

HK

posted by Hua Khee Chan 09 May 2017