msgdata.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package msgdata
  2. import (
  3. "bytes"
  4. "encoding/binary"
  5. "fmt"
  6. )
  7. // Protocol 定义协议数据结构
  8. type Protocol struct {
  9. StartBytes [3]byte // 协议开始符
  10. SourceID uint8 // 源设备号
  11. DestinationID uint8 // 目的设备号
  12. MessageID uint8 // 消息号
  13. DataLength uint16 // 数据长度
  14. Data []byte // 数据
  15. Checksum uint8 // 异或校验码
  16. EndByte uint8 // 协议结束符
  17. }
  18. // NewProtocol 创建一个新的 Protocol 实例
  19. func NewProtocol() *Protocol {
  20. return &Protocol{
  21. StartBytes: [3]byte{0x7F, 0x8E, 0x9D},
  22. EndByte: 0xFE,
  23. }
  24. }
  25. // Encode 将 Protocol 结构体编码为字节切片
  26. func (p *Protocol) Encode() ([]byte, error) {
  27. // 初始化字节缓冲区
  28. var buf bytes.Buffer // 写入协议开始符
  29. buf.Write(p.StartBytes[:])
  30. // 写入源设备号、目的设备号和消息号
  31. binary.Write(&buf, binary.BigEndian, p.SourceID)
  32. binary.Write(&buf, binary.BigEndian, p.DestinationID)
  33. binary.Write(&buf, binary.BigEndian, p.MessageID)
  34. // 写入数据长度
  35. binary.Write(&buf, binary.BigEndian, p.DataLength)
  36. // 写入数据
  37. buf.Write(p.Data[:])
  38. // 计算校验码
  39. checksum := p.CalculateChecksum()
  40. p.Checksum = checksum // 写入校验码
  41. binary.Write(&buf, binary.BigEndian, p.Checksum)
  42. // 写入协议结束符
  43. binary.Write(&buf, binary.BigEndian, p.EndByte)
  44. return buf.Bytes(), nil
  45. }
  46. // CalculateChecksum 计算校验码
  47. func (p *Protocol) CalculateChecksum() uint8 {
  48. // 初始化校验码
  49. checksum := uint8(0)
  50. // 跳过协议开始符
  51. data := append([]byte{byte(p.SourceID), byte(p.DestinationID), byte(p.MessageID)},
  52. append([]byte{byte(p.DataLength >> 8), byte(p.DataLength)}, p.Data...)...)
  53. // 计算校验码
  54. for _, b := range data {
  55. checksum ^= b
  56. }
  57. return checksum
  58. }
  59. // Decode 解码字节切片为 Protocol 结构体
  60. func Decode(data []byte) (*Protocol, error) {
  61. if len(data) < 10 { // 最小长度:3(StartBytes) +1(SourceID) +1(DestinationID) +1(MessageID) +2(DataLength) +1(Checksum) +1(EndByte)
  62. return nil, fmt.Errorf("data too short")
  63. }
  64. p := NewProtocol()
  65. // 读取协议开始符
  66. copy(p.StartBytes[:], data[:3])
  67. // 读取源设备号、目的设备号和消息号
  68. p.SourceID = data[3]
  69. p.DestinationID = data[4]
  70. p.MessageID = data[5]
  71. // 读取数据长度
  72. p.DataLength = binary.BigEndian.Uint16(data[6:8])
  73. // 读取数据
  74. dataLength := int(p.DataLength)
  75. if len(data) < 10+dataLength {
  76. return nil, fmt.Errorf("data length mismatch")
  77. }
  78. p.Data = data[8 : 8+dataLength]
  79. // 读取校验码
  80. p.Checksum = data[8+dataLength]
  81. // 读取协议结束符
  82. p.EndByte = data[9+dataLength]
  83. // 验证校验码
  84. if p.Checksum != p.CalculateChecksum() {
  85. return nil, fmt.Errorf("checksum mismatch")
  86. }
  87. return p, nil
  88. }
  89. func SubstrByRune(s string, start, length int) string {
  90. runes := []rune(s)
  91. if start >= len(runes) {
  92. return ""
  93. }
  94. end := start + length
  95. if end > len(runes) {
  96. end = len(runes)
  97. }
  98. return string(runes[start:end])
  99. }