Can someone help me get this to work? I want to be able to receive an input on the serial window and then display that input. The problem is the first text repeats forever with a while loop, and without a loop the program displays text but doesn't respond at all. What am I doing wrong? It seems like this should be simple.
#include<stdio.h> #include <reg51.h> //Used for serial interface int main () { char string [50]; //Required for Serial Interface SCON = 0x52; // serial port configuration TMOD = 0x20; TCON = 0x40; TH1 = 0xf3; // 2403 baudrate @12mhz //while (1) { printf ("Insert sentence: "); gets (string); printf ("Your sentence is: %s",string); //return 0; //} }
You would want to stay at the gets(string) until you actually have valid data. Also you shouldn't have a return 0 in your while loop. Embedded programs should never end. Something like:
while (1) { printf ("Insert sentence: "); while(gets (string) == NULL) {/*do nothing*/} printf ("Your sentence is: %s",string); }
I'm not sure if gets will return NULL if nothing is there, but you get the idea.
Once you get comfortable with this method (busy polling) you should start looking into using the serial interrupts :). That would allow your program to do other things and only handle rs232 io when it happens.
That still just loops the "Insert sentence" text.
I got it to work this way but its not what I want:
while(1){ char msg[60]; char ch; int i=0; printf("Type a sentence and press ENTER\n"); while ((ch=getchar()) != '\n') msg[i++]=ch; msg[i]='\0'; i=0; while(msg[i] != '\0') putchar(msg[i++]); printf("\n"); }
Can someone help a novice translate this into gets and puts form? I want to be able to manipulate strings later.
A novice should not use gets(). And neither should anyone else. It is the #1 buffer overflow function call known to man.
You would normally use fgets() instead, since it takes a buffer size as parameter.
Unfortunately fgets is not available in the standard C51 library. See below:
"The ANSI C Standard Library includes a vast number of routines, most of which are included in the Cx51 Library. Many, however, are not applicable to an embedded application and are excluded from the Cx51 Library." http://www.keil.com/support/man/docs/c51/c51_xa_librarydif.htm
So it seems I must use gets()
No. What isn't available can be created.
But I took a look at the manual. The gets() in the Keil RTL is not the gets() from the C language standard since it does take a buffer size parameter. That makes it safe to use.
If it hadn't, it would have been trivial two write a new function that had picked up characters one at a time, while keeping track of the buffer space.
Nifty. On the man page for gets the example code is practically the inverse of what I need.
void tst_gets (void) { char buf [21]; do { gets (buf, sizeof(buf)-1); printf ("Input string \"%s\"", buf); } while (buf [0] != '\0'); }
So I try
while(1){ char msg[60]; printf("Type a sentence and press ENTER\n"); while (msg [0] != '\0') {gets (msg, sizeof(msg)-1); //puts(msg); printf("%s\n",msg); } }
Which again just loops the "type a sentence" text. Frustrating.
Where is msg[0] set to '\0'?
Jon
Not sure what you mean. I'm pretty new at programming.
I understand gets() will read a line and put a terminating \0 into the string array. Maybe I'm in over my head. I've been trying to get this to work all week.
Try this.
while(1) { char msg[60]; printf("Type a sentence and press ENTER\n"); gets (msg, sizeof(msg)-1); printf("%s\n",msg); }
Ah, I was close. Thank you.