UPGRADE YOUR BROWSER

We have detected your current browser version is not the latest one. Xilinx.com uses the latest web technologies to bring you the best online experience possible. Please upgrade to a Xilinx.com supported browser:Chrome, Firefox, Internet Explorer 11, Safari. Thank you!

cancel
Showing results for 
Search instead for 
Did you mean: 
Observer wangf
Observer
14,643 Views
Registered: ‎09-05-2008

how to input characters from console, why getchar() works not properly?

Jump to solution

I try to input character from console, I use standard C function, getchar(), but it does not works properly,

first, it is especially large, second, it can get character back only after I push return key, It is not the way as in standard C.

 

What is wrong, and how can I input character? Must I use XUartLite_RecvByte() ? I'd like to use standard C functions to make the program easy.

 

 

test_id = getchar();
switch (test_id)
 {
    case '1':
     print("input 1\n");
     break;
    case '2':
     print("input 2\n");
     break;
    case '3':
     print("input 3\n");
     break;
    case 'C':
     print("input C\n");
     break;
    default:
     print("wrong enter");
     break;
   }

0 Kudos
1 Solution

Accepted Solutions
Highlighted
18,765 Views
Registered: ‎09-05-2008

Re: how to input characters from console, why getchar() works not properly?

Jump to solution

Hi together,

I had the same problem.

 

You can change the buffering behaviour of stdin by calling

setvbuf() before calling getchar();

 

Example :

 

setvbuf(stdin, NULL, _IONBF, 0);

for (;;)

{

  Input = getchar();

  if (Input != EOF)

    xil_printf("%c", Input);

}

 

You may omit the line with EOF, I never got that value.

I have tested the code above with XMK, it should work

in a standalone version too.

 

BTW: Hyperterminal sends each character as soon as you type it.

It does not wait for the <RETURN> key.

 

Hope this helps

  Ralf

6 Replies
Historian
Historian
14,640 Views
Registered: ‎02-25-2008

Re: how to input characters from console, why getchar() works not properly?

Jump to solution

Your terminal program might not send the characters until it sees that you've hit ENTER.

 

-a

----------------------------Yes, I do this for a living.
0 Kudos
Observer wangf
Observer
14,634 Views
Registered: ‎09-05-2008

Re: how to input characters from console, why getchar() works not properly?

Jump to solution

my terminal program is SecureCRT, and it works well with others embedded system. Also I tried HyperTerminal, it works in same way.

so, do anyone use getchar() successfuly before?  and Must I turn to xilinx uart api?  Thanks.

 

(My tools and envirorment is Microblaze, EDK92, uartlite v1.0)

0 Kudos
Scholar golson
Scholar
14,625 Views
Registered: ‎04-07-2008

Re: how to input characters from console, why getchar() works not properly?

Jump to solution

I did a search on getchar and EDK.  Other people have been using it according to search in Xilinx Forum.

 

 

http://www.xilinx.com/support/answers/29379.htm

 

and

 

http://warp.rice.edu/trac/browser/trunk/EDK/LEDs/Demo1.c?rev=56

 

http://forums.xilinx.com/xlnx/board/message?board.id=EMBEDDED&message.id=876&query.id=392564#M876

 

http://forums.xilinx.com/xlnx/board/message?board.id=EDK&message.id=4510&query.id=392610#M4510

 

 

Try increasing Heap and Stack Size a lot to see if that is the problem.

 

 

Message Edited by golson on 12-17-2008 10:17 AM
0 Kudos
Highlighted
18,766 Views
Registered: ‎09-05-2008

Re: how to input characters from console, why getchar() works not properly?

Jump to solution

Hi together,

I had the same problem.

 

You can change the buffering behaviour of stdin by calling

setvbuf() before calling getchar();

 

Example :

 

setvbuf(stdin, NULL, _IONBF, 0);

for (;;)

{

  Input = getchar();

  if (Input != EOF)

    xil_printf("%c", Input);

}

 

You may omit the line with EOF, I never got that value.

I have tested the code above with XMK, it should work

in a standalone version too.

 

BTW: Hyperterminal sends each character as soon as you type it.

It does not wait for the <RETURN> key.

 

Hope this helps

  Ralf

Observer wangf
Observer
14,591 Views
Registered: ‎09-05-2008

Re: how to input characters from console, why getchar() works not properly?

Jump to solution

Your method works. But it seems that the getchar() function is huge! I decided to use xilinx's api, and deal with all the input myself.

Still, many thanks for your reply.

0 Kudos
Explorer
Explorer
9,071 Views
Registered: ‎11-13-2009

Re: how to input characters from console, why getchar() works not properly?

Jump to solution
I know this was a long time ago, do you want to share your input parsing code snippets or others to learn and expand?
0 Kudos