main() { char tabuf[90]; int i, j; int CRC; int unCRC; char buf[100]; printf("Testing CRC generation.\n"); for(i = 0; i < 90; i++) tabuf[i] = i; for(i = 0; i <= 90; i+=2) { memset(buf, 0, 100); for(j = 0; j < i; j++) buf[j] = tabuf[j]; CRC = 0; memcpy(buf+i, &CRC, sizeof(int)); CRC = genCRC(buf, i+4); printf("CRC is %08X.\n", CRC); //buf[i] = (CRC & 0xFF000000) >> 24; //buf[i+1] = (CRC & 0x00FF0000) >> 16; //buf[i+2] = (CRC & 0x0000FF00) >> 8; //buf[i+3] = CRC & 0x000000FF; CRC = htonl(CRC); memcpy(buf+i, &CRC, sizeof(int)); unCRC = genCRC(buf, i+4); printf("unCRC is %08X.\n", unCRC); if(unCRC == 0) printf("Case %d passed.\n", i/2); else printf("Case %d failed.\n", i/2); usleep(100000); } }
static int send_cmd(struct recboat_type *recboat, int cmd, int len, int id, char *buf) { char msg[512]; short crc = 0; int err = 0; msg[0] = 0x53; /* start bytes */ msg[1] = 0x74; msg[2] = cmd >> 8; msg[3] = cmd & 0xff; msg[4] = len >> 8; msg[5] = len & 0xff; msg[6] = id >> 8; msg[7] = id & 0xff; memcpy(&msg[8], buf, len); crc = genCRC(msg, len+8); msg[len+8] = crc >> 8; msg[len+8+1] = crc & 0xff; sendto(recboat->s, msg, len+10, 0, (struct sockaddr *) &recboat->dest, sizeof(recboat->dest)); return(err); }