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

help in LRC checksum string

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



Joined: 06 Mar 2008
Posts: 35
Location: pakistan

View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger

help in LRC checksum string
PostPosted: Thu Mar 27, 2008 10:31 pm     Reply with quote

HI,
i have a string
"010909000AFFFFFFFFFFFF"

i want to calculate checksum such that
01 (XOR) 09 (XOR) 09 (XOR) 00 (XOR) 0A (XOR) FF (XOR) FF (XOR) FF (XOR) FF (XOR) FF (XOR) FF
and
put it in the end of the string
the result in this case is
"0B"

so that the resulting string becomes

"010909000AFFFFFFFFFFFF0B"
I have tried the following code but failed

Code:
BYTE string[24];

BYTE getLRC(BYTE *pbdata,int icount)
{
BYTE chlrc = 0;
while(icount > 0)
  {
  chlrc ^= *pbdata;
  pbdata=pbdata+2;
  icount--;
  }
return chlrc;
}

void main()
{
 strcpy(string,"010909000AFFFFFFFFFFFF00");
 string[22]=getLRC(&string[0],11);
 string[23]=getLRC(&string[1],11);
 for(i=0;i<24;i++)
 putc(string[i]);
}


i get the data
010909000AFFFFFFFFFFFF which is missing the last two characters


Please help me!
_________________
sohail khan
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Mar 28, 2008 1:32 am     Reply with quote

Quote:
BYTE string[24];

strcpy(string,"010909000AFFFFFFFFFFFF00");

Your array can only hold 24 bytes. Your string is 24 bytes.
But a string always has a terminator byte at the end, which is 0x00.
You need to increase your array size to 25.
Guest








PostPosted: Fri Mar 28, 2008 2:16 am     Reply with quote

in this case array is terminated by ETX
but i didn't mentioned it
for the sake of ease
this is not complete code its the part which is not working
Ttelmah
Guest







PostPosted: Fri Mar 28, 2008 3:21 am     Reply with quote

The ETX, doesn't change the point that PCMprogrammer is making. You start with a 24 character string. A 24 character string _requires_ 25 characters of storage. As soon as you use 'strcpy' to move the constant string into the storage area, the _next_ location in memory, _will_ be destroyed.
If working with string library functions, you _must_ allow for the terminator character.

Best Wishes
Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

PostPosted: Fri Mar 28, 2008 3:29 am     Reply with quote

As PCM said you need to define your string as
BYTE string[25];
Although you are iterating through the 24 locations and using putc the function strcpy in main requires an array of 25 bytes not 24!

The problem with your code is that getLRC returns a numerical value and NOT the ASCII code you require so the following

Code:

BYTE getLRC(BYTE *pbdata,int icount)
{
BYTE chlrc = 0;
while(icount > 0)
  {
  chlrc ^= *pbdata;
  pbdata=pbdata+2;
  icount--;
  }
return chlrc;
}


returns the value 0 which is a string termination character and not the char '0' which you need to put at position 22. So when you put it at position 22 the putc routine thinks pos 22 is the end of the string!
Now there is a big problem with the way you are doing it because you will return an 8 bit number for the even indexes and another 8 bit number for the odd indexes. These when converted to ascii will occupy 4 positions e.g. 0x000B.

I would to the following

First you need to convert each pair of chars to a value, there are many ways to do this but try:-

Code:

BYTE getLRC(BYTE *pbdata,int icount) {
  BYTE chlrc = 0;
  char hex[5] = "0x00";
 
  while(icount-- > 0) {
    hex[2] = *pbdata++;
    hex[3] = *pbdata++;
    chlrc ^= atoi(hex);  // Convert the hex string to a value
  }
  return chlrc;
}



the second problem is in main

Code:

void main()
{
 strcpy(string,"010909000AFFFFFFFFFFFF00");
 string[22]=getLRC(&string[0],11);
 string[23]=getLRC(&string[1],11);
 for(i=0;i<24;i++)
 putc(string[i]);
}


Change to
Code:

void main()
{
  int8 crc;
  char *pos;
  strcpy(string,"010909000AFFFFFFFFFFFF00");
  crc = getLRC(string[0], 11);
  pos = string[22];
  sprintf(pos, "%X", crc);
  string[24] = 0;
  printf("%s", string);
  //for(i=0;i<24;i++)
  // putc(string[i]);
}

I think that is it but I have to go now so good luck.


Last edited by Wayne_ on Fri Mar 28, 2008 7:13 am; edited 2 times in total
Matro
Guest







PostPosted: Fri Mar 28, 2008 3:44 am     Reply with quote

- You need to convert your hexadecimal-coded string to integer
- You need to have a sufficient array length to store the data, the checksum and the termination char.
- When doing your putc() and if you want to see the result, you have to send more than ust the 24 bytes of data...
Code:

BYTE string[27];

BYTE getLRC(BYTE *pbdata,int icount)
{
BYTE chlrc = 0;
while(icount > 0)
  {
   if(*pbdata <= '9')
   {
      chlrc ^= (*pbdata) - '0';
   }
   else
   {
      chlrc ^= (*pbdata) - 'A' + 0xA;
   }
  pbdata=pbdata+2;
  icount--;
  }
  if(chlrc <= '9')
  {
      return chlrc + '0';
  }
  else
  {
      return chlrc - 0xA + 'A';
  }
}

void main()
{
 strcpy(string,"010909000AFFFFFFFFFFFF00");
 string[22]=getLRC(&string[0],11);
 string[23]=getLRC(&string[1],11);
 for(i=0;i<26;i++)
 putc(string[i]);
}


Your way of coding is not very proper but I try to stay close to that and this could should work.

Matro
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