I'm using GNU C in Ubuntu 14.0.4. I wrote a CRC_XOR() function and call it with the same input argument multiple times. But It's wierd, each call may sometimes get different result. What's going wrong? Sample code is here:
#include <stdio.h>
#include <stdlib.h>
char CRC_XOR(char *as_Pkt,int ai_len);
void DO_Get_CRC(void);
int main(void)
{
DO_Get_CRC(); //get 01 00 02 03
DO_Get_CRC(); //get 01 00 02 03
DO_Get_CRC(); //get 01 00 02 00 (strange?)
DO_Get_CRC(); //get 01 00 02 03
DO_Get_CRC(); //get 01 00 02 00 (strange?)
DO_Get_CRC(); //get 01 00 02 03
DO_Get_CRC(); //get 01 00 02 00 (strange?)
exit(0);
}
/*
use same input to invoke CRC_XOR()
*/
void DO_Get_CRC(void)
{
char ls_pkt[20];
int li_idx;
short li_max = 512;
int li_len = 0;
ls_pkt[li_len++] = 0x01; //station ID
ls_pkt[li_len++] = 0x00; //length-low byte
ls_pkt[li_len++] = 0x02; //length-high byte
ls_pkt[li_len++] = CRC_XOR(ls_pkt,li_len);
ls_pkt[li_len] = 0;
for (li_idx=0; li_idx<li_len;li_idx++) {
printf("%02X ", ls_pkt[li_idx]); //display in hexdigits
}
printf("\n");
}
/*
return 1 byte of CRC by XOR byte array
*/
char CRC_XOR(char *as_Pkt,int ai_len)
{
int li_idx;
char lc_CRC = 0x00;
for (li_idx=0; li_idx < ai_len; li_idx++){
lc_CRC ^= as_Pkt[li_idx]; //XOR each byte
}
return (char)(lc_CRC & 0x000000FF); //return CRC byte
}