Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
6 years, 3 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:
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, 3 months ago.
Hi there,
A few things:
- The default baud rate for the
Serial
object andprintf
in Mbed OS is9600
, unless you manually specify that you want to change the baud rate to115200
with the following code in main:uart.baud(115200);
- The default parity for
Serial
isnone
, you can change the parity toEven
,Odd
,Forced0
, orForced1
with the followingformat
function here and you can use it in your main function as follows:uart.format(bits, parity, stop_bits);
- Your code is not using the
Serial uart
object you specified abovemain()
, 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!