This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Help with printf, gets

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;
  //}
  }

Parents
  • 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.

Reply
  • 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.

Children