View previous topic :: View next topic |
Author |
Message |
Jason Guest
|
reading a string |
Posted: Mon Jun 26, 2006 5:29 am |
|
|
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
|
|
Posted: Mon Jun 26, 2006 8:09 am |
|
|
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
4. |
|
|
Jason Guest
|
|
Posted: Mon Jun 26, 2006 8:50 am |
|
|
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
|
|
Posted: Mon Jun 26, 2006 8:51 am |
|
|
Did you hit the return key? |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Mon Jun 26, 2006 8:54 am |
|
|
You should also consider using
Code: | void get_string(char* s, int max) | instead of gets(). It is safer
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
|
|
Posted: Mon Jun 26, 2006 9:11 am |
|
|
Sorry, what is return key? |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Mon Jun 26, 2006 9:13 am |
|
|
Enter/Return key on the keyboard! |
|
|
Jason Guest
|
|
Posted: Mon Jun 26, 2006 9:19 am |
|
|
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
|
|
Posted: Mon Jun 26, 2006 9:24 am |
|
|
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
|
|
Posted: Mon Jun 26, 2006 9:43 am |
|
|
If the compiler cannot find the file, then it wouldn't compile. Maybe your hardware is not setup correctly. |
|
|
Jason Guest
|
|
Posted: Mon Jun 26, 2006 10:07 am |
|
|
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
|
|
Posted: Mon Jun 26, 2006 10:16 am |
|
|
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
|
|
Posted: Mon Jun 26, 2006 10:28 am |
|
|
it is working now.
Thanks very much!!! |
|
|
kiltjim Guest
|
|
Posted: Fri Jul 28, 2006 5:34 am |
|
|
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
|
|
Posted: Fri Jul 28, 2006 6:59 am |
|
|
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. |
|
|
|