rts-sim-testing-service/third_party/axle_device/beijing12/msg/common.go

62 lines
1.2 KiB
Go

package msg
import (
"bytes"
"encoding/binary"
)
const (
ProtocolType_Sync = 0x01
ProtocolType_NoSync = 0x02
)
const (
MessageType_A = 0x80
MessageType_B = 0x81
MessageType_SSE = 0x90
MessageType_SSR = 0x91
)
const (
CRC_POLY_1 = 0x100D4E63 //通道1的CRC多项式
CRC_POLY_2 = 0x8CE56011 //通道2的CRC多项式
)
const (
SCW_1 = 0xAE390B5A //通道1的SCW
SCW_2 = 0xC103589C //通道2的SCW
)
const (
T_POLY_1 uint32 = 0x0FC22F87 //通道1的时间戳生成多项式
T_POLY_2 uint32 = 0xC3E887E1 //通道2的时间戳生成多项式
)
const Twait_sse = 3 //默认sse等待回应的周期数
func GetMessageType(data []byte) byte {
return data[1]
}
type MsgHeader struct {
ProtocolType byte //协议交互类别
MessageType byte //报文类型
SourceAddr uint16 //源地址
TargetAddr uint16 //目的地址
}
func (m *MsgHeader) encode() []byte {
var data []byte
data = append(data, m.ProtocolType)
data = append(data, m.MessageType)
data = binary.LittleEndian.AppendUint16(data, m.SourceAddr)
data = binary.LittleEndian.AppendUint16(data, m.TargetAddr)
return data
}
func (m *MsgHeader) decode(data []byte) error {
buf := bytes.NewBuffer(data)
err := binary.Read(buf, binary.LittleEndian, m)
return err
}