Nios® II Embedded Design Suite (EDS)
Support for Embedded Development Tools, Processors (SoCs and Nios® II processor), Embedded Development Suites (EDSs), Boot and Configuration, Operating Systems, C and C++
Announcements
Intel Support hours are Monday-Fridays, 8am-5pm PST, except Holidays. Thanks to our community members who provide support during our down time or before we get to your questions. We appreciate you!

Need Forum Guidance? Click here
Search our FPGA Knowledge Articles here.
12408 Discussions

Handling data with alt_getchar()

Altera_Forum
Honored Contributor II
1,916 Views

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.
0 Kudos
3 Replies
Altera_Forum
Honored Contributor II
517 Views

Hi, 

 

alt_getchar works fine outside the loop Small C library. 

# 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  

 

Check with getchar() using Normal C library. 

 

Best Regards, 

Anand Raj Shankar 

(This message was posted on behalf of Intel Corporation)
Altera_Forum
Honored Contributor II
517 Views

 

--- 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 II
517 Views

Thanks mfro, you are correct I did forget. Many thanks.

Reply