Forum Discussion
Altera_Forum
Honored Contributor
8 years agoHandling data with alt_getchar()
Hi,
I've written a short program for a nios ii (e) with a uart rs232 core, to echo a character I send from a serial terminal on a PC. I have managed to get it to work, however it appears to 'receive' a character of its own after every echo. My main is as follows, delay() is just a simple counter;# include "sys/alt_stdio.h"# include <stdio.h># include "altera_avalon_uart_regs.h"
int main(){
char c;
while (1){
printf("Hello from Nios II!\n");
c = alt_getchar();
printf("You entered: %c \n", c);
delay(1);
}
return 0;
} However, after every time I send a character, I will get "You entered x" followed by "hello...." then another "you entered" which is blank, before it sends another "hello..", this time waiting for another character. Is there something I'm missing here? Many thanks in advance.3 Replies
- Altera_Forum
Honored Contributor
Hi,
alt_getchar works fine outside the loop Small C library.
Check with getchar() using Normal C library. Best Regards, Anand Raj Shankar (This message was posted on behalf of Intel Corporation)# include "sys/alt_stdio.h" # include <stdio.h> # include "altera_avalon_uart_regs.h" int main(){ char c; c = alt_getchar(); printf("You entered: %c \n", c); while (1){ } return 0; } -------------------------------- democode - Altera_Forum
Honored Contributor
--- Quote Start --- However, after every time I send a character, I will get "You entered x" followed by "hello...." then another "you entered" which is blank, before it sends another "hello..", this time waiting for another character. Is there something I'm missing here? Many thanks in advance. --- Quote End --- Your code doesn't consume the CR (or CR/LF) you most likely typed after the character. Consequently it's read (and displayed) next round. - Altera_Forum
Honored Contributor
Thanks mfro, you are correct I did forget. Many thanks.