日期:2014-05-18 浏览次数:21003 次
uint gen_crc(byte[] frame) { int frame_len = frame.Length; byte c; uint treat, bcrc; uint wcrc = 0; for (int i = 0; i < frame_len; i++) { c = frame[i]; for (int j = 0; j < 8; j++) { treat = c & 0x80U; c <<= 1; bcrc = (wcrc >> 8) & 0x80; wcrc <<= 1; if (treat != bcrc) { wcrc ^= 0x1021; } } } return wcrc; }
------解决方案--------------------
02 30 31 30 32 34 38 31 4D E5 03
分析一下,是这段“30 31 30 32 34 38 31”运算后才是“4D E5”
类型该换成unsigned short
unsigned short gen_crc(BYTE far *frame, short frame_len) { BYTE c, treat, bcrc; unsigned short wcrc = 0; short i, j; for (i = 0; i < frame_len; i++) { c = frame[i]; for (j = 0; j < 8; j++) { treat = c & 0x80; c <<= 1; bcrc = (wcrc >> 8) & 0x80; wcrc <<= 1; if (treat != bcrc) wcrc ^= 0x1021; } } return wcrc; }