|
|
View previous topic :: View next topic |
Author |
Message |
Indira Learner
Joined: 08 Sep 2017 Posts: 3
|
Propblem comparing two strings using rs232 |
Posted: Sat Sep 09, 2017 3:33 am |
|
|
Hello, there is a code i wrote to compare a string received using a communication port. The code worked well a few times, and then it stopped working. I do not know what the error is. It actually compiles without an error, but when i run it, the output is always wrong even if both words match.
Code: | #include <main.h>
#include <stdio.h>
#include <string.h>
#fuses HS
#use rs232(uart1, baud=9600)
char password[] = {"123abc"};
char string_pass[];
char input_str[];
#define BUFFER_SIZE 32
BYTE buffer[BUFFER_SIZE];
BYTE next_in = 0;
BYTE next_out = 0;
#INT_EXT
void EXT_isr(void)
{
reset_cpu();
}
#INT_RDA
void RDA_isr(void)
{
int t;
buffer[next_in] = getc();
t = next_in;
next_in =(next_in + 1) % BUFFER_SIZE;
if(next_in == next_out)
next_in = t;
}
#define bkbhit (next_in!=next_out)
BYTE bgetc()
{
BYTE c;
while( ! bkbhit);
c = buffer[next_out];
next_out =(next_out + 1) % BUFFER_SIZE;
return(c);
}
void my_get_string(char* s)
{
unsigned int8 len;
char c;
len = 0;
do
{
c = bgetc();// Call bgetc()
if((c >= ' ')&&(c <= '~'))
{
s[len++] = c;
putc(c);
}
}
while(c != 13);
s[len] = 0;
}
void toggle_function()
{
int i;
i=0;
while(i<20){
output_high(led1);
delay_ms(500);
output_low(led1);
delay_ms(500);
i++;
}
}
void compare_function(char *string)
{
if(strcmp(string,password) == 0)
{
printf("\n Correct Keyword ! \n ");
toggle_function();
break;
}
else
{
printf("\n ERROR\n");
break;
}
}
void main()
{
clear_interrupt(INT_EXT);
enable_interrupts(INT_EXT);
clear_interrupt(INT_RDA);
enable_interrupts(INT_RDA);
enable_interrupts(GLOBAL);
while(TRUE)
{
printf("Type string here: ");
my_get_string(input_str);
strcpy(string_pass,input_str);
compare_function(string_pass);
}
} |
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sat Sep 09, 2017 9:55 am |
|
|
Quote: | char password[] = {"123abc"};
char string_pass[];
char input_str[];
|
Give these two arrays a size. Without the size, you don't have arrays.
You just have a pointers, and they will overwrite other RAM values
if you write to the "array".
Give each array a useful size, big enough so that it will hold the entire
expected string, and maybe some more bytes as a precaution.
Make sure you make the arrays big enough to have the 0x00 byte
that marks the end of every string. |
|
|
|
|
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
|