-3
#include<reg51.h>
#include<string.h>
#include"_LCD_R8C.c"
unsigned char c[12];
unsigned char chr[11];
void serial_int (void) interrupt 4
{
  if (RI==1)      
  {
    chr[11] = SBUF;
    RI = 0;     
    TI = 0;     
  }
}

int main()
{
  unsigned char a[2][11]={"$0016221826","$0123456789"};
  int i,j;
  lcd_init();
  lcd_clear();
  SCON = 0x50;              
  TMOD = 0x20;                
  TH1  = 0xFD;                 
  ET0  = 0;                     
  TR1  = 1;                       
  RI   = 1;                   
  ES   = 1;                   
  EA   = 1;
  for(j=0;j<1;j++)
      {
       for(i=0;i<=10;i++)
       {
        c[i]=chr[i];
       }
     c[11]='\0';
     }                   
  for(i=0;i<=1;i++)
  {
    j=strcmp(a[i],c); /* !!! Here is the problem !!! */
    if(j==0)
     {
      lcd_printxy(1,1,"yes");
     }
    else
     {
      lcd_printxy(1,6,"no");
     }
 }
}

I am getting the display as "no", please let me know what is the problem? the problem might be 1) the received array of characters are not converted to string, or 2) the received array of characters are converted to string but not able to compare with the available string.. please go through the program

2

2 Answers 2

0

One obvious bug for starters - change:

unsigned char a[2][11]={"$0016221826","$0123456789"};

to:

unsigned char a[2][12]={"$0016221826","$0123456789"};

(You need to allow room for the terminating '\0' in each string - I'm surprised your compiler didn't complain about this ?)

Also, this line in your interrupt handler is wrong:

chr[11] = SBUF;

Several problems with this - char only has storage for 11 chars, not 12, and you probably want to be accumulating characters from index 0 and then bumping the index, otherwise you're just overwriting the same character each time.

Looking at the rest of the code there are so many other problems that I think you may need to take a step back here and start with a simpler program - get that working first and then add to it in stages.

You might also want to get a decent introductory book on C and study it as there are lots of very basic mistakes in the code, so you might benefit from a better understanding of the language itself.

Sign up to request clarification or add additional context in comments.

Comments

0

You only assign a value to chr[11], the rest of the array is uninitialized and will contain random data. You then copy this array containing random data to c (you could use e.g. memcpy here instead of looping yourself), and finally you compare the complete contents of c (which is random data) with one of the entries in a. So it's kind of natural that the result of that comparison will be that the strings are not equal.

Edit: A redesign of the program in the question

Your program has too many problems to be easily fixed, so I decided to try and rewrite it:

#include <reg51.h>
#include <string.h>

#include "_LCD_R8C.c"

#define INPUT_LENGTH 11
#define ACCEPTABLE_INPUT_COUNT 2

char input[INPUT_LENGTH];  /* The input from the serial port */
int  input_pos  = 0;       /* Current position to write in the input buffer */
int  input_done = 0;       /* 0 = not done yet, 1 = all input read */

void serial_int (void) interrupt 4
{
    if (!input_done && RI == 1)
    {
        /* Put the input at next position in the input buffer */
        /* Then increase the position */
        input[input_pos++] = SBUF;

        RI = 0;
        TI = 0;

        /* Check if we have received all input yet */
        if (input_pos >= INPUT_LENGTH)
            input_done = 1;
    }
}

int main()
{
    /* Array of data that this programs thinks is acceptable */
    /* +1 to the input length, to fit the terminating '\0' character */
    char acceptable_inputs[ACCEPTABLE_INPUT_COUNT][INPUT_LENGTH + 1] = {
        "$0016221826", "$0123456789"
    };

    iny acceptable_found = 0;  /* Acceptable input found? */

    /* Initialization */
    lcd_init();
    lcd_clear();
    SCON = 0x50;              
    TMOD = 0x20;                
    TH1  = 0xFD;                 
    ET0  = 0;                     
    TR1  = 1;                       
    RI   = 1;                   
    ES   = 1;                   
    EA   = 1;

    /* Wait until we have received all input */
    while (!input_done)
        ;  /* Do nothing */

    /* Check the received input for something we accept */
    for (int i = 0; i < ACCEPTABLE_INPUT_COUNT; i++)
    {
        if (memcmp(acceptable_inputs[i], input, INPUT_LENGTH) == 0)
        {
            /* Yes, the data received is acceptable */
            acceptable_found = 1;
            break;  /* Don't have to check any more */
        }
    }

    if (acceptable_found)
        lcd_printxy(1, 1, "Yes");
    else
        lcd_printxy(1, 1, "No");

    return 0;
}

10 Comments

I am very sorry for that, I am looping here because, the data which i am receiving through RS232 have series of characters, in order to compare the received data with the other available string i am converting it to string using for loop, can memcpy to make it string?
@pradeep As long as the array contains normal characters, and is terminated by a zero, it's a a string. You could use e.g. memcpy(c, chr, 11); c[11] = '\0'; instead of the strange loop.
in the previous example what you have explained is by taking the string directly i.e char foo[] = "bar"; but think that bar is now character, char foo[] = bar; this bar i want to make it string....
@pradeep I added a rewritten version of your program. In it a don't handle the input as strings at all. I also put each new byte received from the serial port in a new position in the input array. There are also other changes. It is of course untested (I don't have whatever hardware/software you have), but I think it should work.
thanks a lot, but the keil compiler was showing some errors 1)char acceptable_inputs[ACCEPTABLE_INPUT_COUNT][] = { "$0016221826", "$0123456789" error is unknown array size so i have changed it to characceptable_inputs[ACCEPTABLE_INPUT_COUNT][INPUT_LENGTH]={ "$0016221826", "$0123456789"
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.