BxUtils_test.go 692 B

12345678910111213141516171819202122232425262728
  1. package bx
  2. import (
  3. "encoding/binary"
  4. "fmt"
  5. "testing"
  6. )
  7. func TestCRC16(t *testing.T) {
  8. data := []byte{
  9. 0xA5, 0xA5, 0xA5, 0xA5, 0xA5, 0xA5, 0xA5, 0xA5, //帧头
  10. //包头
  11. 0x01, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFE, 0x02, 0x0D, 0x00,
  12. //数据域
  13. 0xA2, 0x03, 0x01, 0x08, 0x00, 0x13, 0x20, 0x01, 0x25, 0x11, 0x17, 0x26, 0x05,
  14. 0xB0, 0x3F, //校验码,报头和数据域
  15. 0x5A, //帧尾
  16. }
  17. //
  18. //
  19. crc16 := CRC16(data[:len(data)-3], 8, len(data))
  20. real := binary.LittleEndian.Uint16(data[len(data)-3 : len(data)-1])
  21. if crc16 != real {
  22. t.Error("不通过!结果:", crc16, " 预期:", real)
  23. } else {
  24. fmt.Println("结果:", crc16, " 预期:", real)
  25. }
  26. }