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 support@ccsinfo.com

rs232 string receive an compare problem

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
magic
Guest







rs232 string receive an compare problem
PostPosted: Fri Apr 25, 2003 9:04 am     Reply with quote

<font face="Courier New" size=-1>following code sometimes bad working without "rs232 enable and disable lines"..i could not find Where is the problem.

problem is;
code passing from if line to bottom even you entered a invalid code!!!
i changed baudrate (to more speed or slow) but not solved.

would you suggest any more stable code about this task.
Regards.



char a[10];//="ab";
char b[10]="give";

while(1)
{
char a[10]="";
printf("please enter your code: \n\r");
delay_ms(50);
bit_set(*0x18,4); //bit_set( RCSTA, SPEN );rs232 recieve enable

gets(a);
delay_ms(50);
bit_clear(*0x18,4);//rs232 recieve disable

if(!stricmp(a,b))// if result is zero entered code is "give".
{
.
.
.
</font>
___________________________
This message was ported from CCS's old forum
Original Post ID: 13993
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

Re: rs232 string receive an compare problem
PostPosted: Fri Apr 25, 2003 11:28 am     Reply with quote

:=problem is;
:=code passing from if line to bottom even you entered a invalid code!!!
:=i changed baudrate (to more speed or slow) but not solved.
:=
---------------------------------------------------------

I didn't have that problem. It appears to work OK
after I made it into a compilable program, below.

However, there are still some major design problems
with your code. One problem is that the gets() function
does not have any limit on the number of characters
that can be entered. If you do a Google search on gets(),
you will read comments like this:

"The gets function is known to be a security risk. This is
because the length of an input line can overflow the size
of the buffer, resulting in indeterminate behavior.."
<a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnucmg/html/UCMGch08.asp" TARGET="_blank">http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnucmg/html/UCMGch08.asp</a>

"Gets()-- The Devil's Function"
<a href="http://www.accu.org/acornsig/public/caugers/volume2/issue4/gets.html" TARGET="_blank">http://www.accu.org/acornsig/public/caugers/volume2/issue4/gets.html</a>

etc.

So I suggest you use the get_string() function which is
provided by CCS in their INPUT.C file. This file is here:
c:\Program Files\Picc\Drivers
It's also available online here:
<a href="http://ms-n.org/DataSheets/ccs/examples/INPUT.C" TARGET="_blank">http://ms-n.org/DataSheets/ccs/examples/INPUT.C</a>
-------

Also, there's another problem in your code. According to
the .LST file, when you declare char a[10]="";
inside the while() loop, you are creating another instance
of a. The .LST file refers to them as "a" and "main.a".
They are allocated at different ram addresses.
Your code still works, it just wastes ram.



<PRE>
#include <16F877.H>
#fuses HS, NOWDT, NOPROTECT, BROWNOUT, NOLVP, PUT
#use delay(clock = 8000000)
#use rs232(baud = 9600, xmit=PIN_C6, rcv = PIN_C7, ERRORS)
<BR>
#include <string.h>
<BR>
void main()
{
char a[10]; //="ab";
char b[10]= "give";
<BR>
while(1)
{
char a[10]="";
printf("please enter your code: \n\r");
delay_ms(50);
<BR>
gets(a);
delay_ms(50);
<BR>
if(!stricmp(a,b))// if result is zero entered code is "give".
printf("Good code\n\r");
else
printf("Bad code\n\r");
<BR>
}
}

</PRE>
___________________________
This message was ported from CCS's old forum
Original Post ID: 13996
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
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