CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

reading a string
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Jason
Guest







reading a string
PostPosted: Mon Jun 26, 2006 5:29 am     Reply with quote

I have prorammed the PIC to read a string through RS232. but the program keeps running gets(string);. I have already typed a string to it. The memory stored in the variable was nothing. Please give advice to me to solve this problem. thanks


#include <16F877.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=19200, xmit=PIN_C6, rcv=PIN_C7)
#include <input.c>
#include <string.h>

void main()
{
char string[5];

printf("please type a string: ");

if(!kbhit())
{ gets(string); }

printf("%c",string);

}
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Mon Jun 26, 2006 8:09 am     Reply with quote

1. You should have a while(1) in your main.
2. %c is a char not a string and you are going to print the address of the string with the syntax that you chose. Try %s.
3. 5 chars is not very much room for a string unless you are only tying 4-letter words Laughing
4.
Jason
Guest







PostPosted: Mon Jun 26, 2006 8:50 am     Reply with quote

Thanks!
I have put the while(1) and changed to %s, but it still stucked in the gets() instruction.
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Mon Jun 26, 2006 8:51 am     Reply with quote

Did you hit the return key?
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Mon Jun 26, 2006 8:54 am     Reply with quote

You should also consider using
Code:
void get_string(char* s, int max)
instead of gets(). It is safer Smile

Code:

#include <16F877.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=19200, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
#include <input.c>
#include <string.h>

#define MAX_BUFFER 10
void main()
{
  char string[MAX_BUFFER];

  while(1)
  {
    printf("please type a string: ");

    // Wait for a key press
    while(!kbhit());
 
    get_string(string, MAX_BUFFER);
    printf("%s",string);
  }
}


Last edited by Mark on Mon Jun 26, 2006 10:15 am; edited 1 time in total
Jason
Guest







PostPosted: Mon Jun 26, 2006 9:11 am     Reply with quote

Sorry, what is return key?
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Mon Jun 26, 2006 9:13 am     Reply with quote

Enter/Return key on the keyboard!
Jason
Guest







PostPosted: Mon Jun 26, 2006 9:19 am     Reply with quote

Sorry for my silly question.
I have typed ENTER key but it still has nothing happen.
I have tried your code, the program is still waiting for key press and nothing stored in the register string.
Jason
Guest







PostPosted: Mon Jun 26, 2006 9:24 am     Reply with quote

I have just found a problem.
I cannot find the string.h file in the folder. Would this be the reason cause the problem that it fails to read the string?
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Mon Jun 26, 2006 9:43 am     Reply with quote

If the compiler cannot find the file, then it wouldn't compile. Maybe your hardware is not setup correctly.
Jason
Guest







PostPosted: Mon Jun 26, 2006 10:07 am     Reply with quote

Thanks.
The compiler can compile and it works with one character reading.
This code is the first step of the device activation development.
In fact, the device required to send and receive a series of string and activate the device automatically.
So, I need to generate a code that can receive a string and go further.
Would you mind telling me which part has gone wrong in my code?
THANKS.
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Mon Jun 26, 2006 10:16 am     Reply with quote

This code is tested and works
Code:

#include <16F877.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP,DEBUG
#use delay(clock=10000000)
#use rs232(baud=19200, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
#include <input.c>
#include <string.h>

#define MAX_BUFFER 10
void main()
{
  char string[MAX_BUFFER];

  while(1)
  {
    printf("please type a string: ");

    // Wait for a key press
    while(!kbhit());
 
    get_string(string, MAX_BUFFER);
    printf("\r\n%s\r\n",string);
  }
}
Jason
Guest







PostPosted: Mon Jun 26, 2006 10:28 am     Reply with quote

it is working now.
Thanks very much!!!
kiltjim
Guest







PostPosted: Fri Jul 28, 2006 5:34 am     Reply with quote

Instead of using while(!kbhit()) wouldn't it be better to use if(kbhit())? This way the program wouldn't be waiting for input and could continue with other functions?

I am trying to program a 16F877a to accept commands from the RS232 port, and react accordingly.
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Fri Jul 28, 2006 6:59 am     Reply with quote

Well if you really want to do other things, then it would be better to use an interrupt to receive the characters. When you get a return key (\r) then process the data. If you just use and if(kbhit()) then you have to make sure that your longest cycle time is less than the time required to receive a byte (or possibly 2 bytes depending on how you write the code) or else you will get a buffer overrun. This method also has the advantage that the system will not stop as soon as a key press is received. If you use the get_string() commands they will wait until the return key (or max chars) is received. A bit of noise could trigger the program to enter the get_string() routine and "lock up" the system since there is no user actually entering commands.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group