6 years, 2 months ago.

uart.getc() not match ASCII

Hi,

I'm trying to get input character from serial port, based on "Hello world" program.

But some char value input is not the same as my expected, would some one help to check about it please ?

Here is my code:

#include "mbed.h"
Serial uart(USBTX, USBRX);

int main() {  
    while(1) {
        printf("hello world\n");
        printf("read char 0x%x\n", uart.getc());
        wait(1);
    }
}

When I input '1', ' 2', '3' one by one, the output shows as below:

Quote:

hello world
read char 0x61
hello world
read char 0x62
hello world
read char 0xe3

The first two output seems normally, but when '3' input, my expectation output should be 0x63. but it output "0xe3"

I guess this maybe due to my terminal setting, but it still fails after many attempts.

Here is my teraterm configuration:

/media/uploads/Donny_AKMChina/teraterm.png

I tried to change the *'Parity'* to *'None'*, the output print will disappear, it only works when setting to 'Space' or 'Mark'

Thank a lot in advance.

1 Answer

6 years, 1 month ago.

Hi there,

A few things:

  1. The default baud rate for the Serial object and printf in Mbed OS is 9600, unless you manually specify that you want to change the baud rate to 115200 with the following code in main: uart.baud(115200);
  2. The default parity for Serial is none, you can change the parity to Even, Odd, Forced0, or Forced1 with the following format function here and you can use it in your main function as follows: uart.format(bits, parity, stop_bits);
  3. Your code is not using the Serial uart object you specified above main(), so you need to change your code to the following to ensure you are using the correct UART pins:

#include "mbed.h"
Serial uart(USBTX, USBRX);

int main() {
    while(1) {
        uart.printf("hello world\n");
        uart.printf("read char 0x%x\n", uart.getc());
        wait(1);
    }
}

So if you would like the baud rate to be 115200 you need to use the following code:

#include "mbed.h"
Serial uart(USBTX, USBRX);

int main() {
    uart.baud(115200);
    while(1) {
        uart.printf("hello world\n");
        uart.printf("read char 0x%x\n", uart.getc());
        wait(1);
    }
}

Please let me know if you have any questions!

- Jenny, team Mbed

If this solved your question, please make sure to click the "Thanks" link below!