We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
Hi. I'm using an eval board with C1610 (Phytec KC161). I know that KEIL doesn't have the function kbhit(). But I need somthing like that function, is it posible to make one? If does anyone know, please give me ideeas, or, the body of he function, even it is for over device. Thank you.
Bad concept. If you do implement a kbhit(), it will only tell you that there is at least one character available.
You will not know if you have enough data for the call to scanf().
First of all, avoid scanf().
A better alternative is often to retrieve all data in a buffer, and then use sscanf(). Or tu just use atoi(), strtol() or similar to extract the data.
I tryed to avoid scanf(), and I used like this:
char aString[10]; int nr; gets(aString, sizeof(aString)-1); nr = atoi(aString); printf("Number introduced: %d", nr)
But in the Hyper terminal the device jump quickly to printf(), an don't let me introduce something. So , I can't use gets()! But when I used scanf it worked fine... Thank you.
So why do you not receive the characters one by one, until you have enough characters to form a full command? If you are sending the commands directly from the hyperterminal, you have enough when you receive a newline character.
If you get too much characters without a newline character, you can throw everything away and wait until a newline character is received before starting to collect a new command.
gets() and sprintf() are extremely bad commands when used by a PC program. They are not getting any better to use, just because you are programming an embedded application.
Exactly how do you think that gets() can know how much buffer space you have for receiving the expected command? If there is 1k characters available, gets() will try to receive a 1k string - even if your buffer is 20 characters large...
You can not leave it to the external equipment (or the user) to make sure that you can't send dangerous information to your application!
So why do you not receive the characters one by one, until you have enough characters to form a full command? I think the instruction scanf("%s", command) can read the command I want. What do you think about this code, the question is after code:
//........ bit kbhit(void){ //Return 1 if character available, otherwise 0 if ( S0RIR ) return ( 1 ); return ( 0 ); } void main(void){ char command[10]; unsigned int nr; serial_setup(); //like previous message //...ports init... /* ... some intructions dependent of which command I typed from hyper teminal ... */ if(kbhit()){ printf("Type a command: "); //I have defined some commands wich I use above scanf("%s", command); if(strncmp(command, train, 3)==0) // if the string I typed match a defined command // here I change some variables wich I use above } } The problem is that kbhit() is always true, why? Is it dependent of how many characteres I entered? How can I modify ?
Thank you.
I soleved the problem, I had to delete the S0RIR at the end of the body of if(kbhit()){...} It work fine , with scanf()...
I think the instruction scanf("%s", command) can read the command I want.
How large is the command buffer?
Try to send a couple of extra characters to the unit, and check what happens with your other RAM variables stored directly after the command buffer...
gets() and scanf() are two of the most dangerous functions available in the C library, and you specifically want to use them despite being warned?
Immediately plan for using the watchdog in the processor. You might need it sooner than you think. However, the watchdog can only help to restart a hung application. It will not protect the application from producing erroneous results, caused by buffer overruns.
If you really have decided to use scanf(), at least always specify a maximum field width when retrieving strings. Without a field width, scanf() will behave similar to gets() - trying to receive any number of characters, whatever the size of your buffer is!