Forum Discussion

Altera_Forum's avatar
Altera_Forum
Icon for Honored Contributor rankHonored Contributor
7 years ago

Handling 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's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    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's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored 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.