package bx import ( "encoding/binary" "fmt" "testing" ) func TestCRC16(t *testing.T) { data := []byte{ 0xA5, 0xA5, 0xA5, 0xA5, 0xA5, 0xA5, 0xA5, 0xA5, //帧头 //包头 0x01, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFE, 0x02, 0x0D, 0x00, //数据域 0xA2, 0x03, 0x01, 0x08, 0x00, 0x13, 0x20, 0x01, 0x25, 0x11, 0x17, 0x26, 0x05, 0xB0, 0x3F, //校验码,报头和数据域 0x5A, //帧尾 } // // crc16 := CRC16(data[:len(data)-3], 8, len(data)) real := binary.LittleEndian.Uint16(data[len(data)-3 : len(data)-1]) if crc16 != real { t.Error("不通过!结果:", crc16, " 预期:", real) } else { fmt.Println("结果:", crc16, " 预期:", real) } }