373 lines
7.5 KiB
Go
373 lines
7.5 KiB
Go
package message
|
||
|
||
//数据帧(0x80+扩展类型),一个CAN帧只能存储8个字节,对于一个应答器报文,分片到多个CAN数据帧中
|
||
|
||
// BtmDataMessageFrame 数据帧-应答器报文
|
||
// offset = [0x00,0x0c] 共13个即可用于存储应答器报文的共104个字节
|
||
type BtmDataMessageFrame struct {
|
||
//帧ID
|
||
FId CanFrameId
|
||
//8字节,应答器报文片段
|
||
Message []byte
|
||
}
|
||
|
||
func NewBtmDataMessageFrame(sn byte, offset byte) *BtmDataMessageFrame {
|
||
return &BtmDataMessageFrame{
|
||
FId: *NewCanFrameId(CAN_ADDR_RSP_ATP, CAN_ADDR_RSP_BTM, 0x80+offset, sn),
|
||
}
|
||
}
|
||
func (f *BtmDataMessageFrame) Encode() *CanetFrame {
|
||
cf := &CanetFrame{}
|
||
|
||
cf.CanId = f.FId
|
||
cf.CanLen = 8
|
||
cf.FF = true
|
||
cf.RTR = false
|
||
//
|
||
writer := NewCanBitsWriter(8)
|
||
for _, data := range f.Message {
|
||
writer.AddByte(data)
|
||
}
|
||
cf.CanData = writer.(CanBusData).GetData()
|
||
|
||
//
|
||
return cf
|
||
}
|
||
func (f *BtmDataMessageFrame) Decode(cf *CanetFrame) bool {
|
||
f.FId = cf.CanId
|
||
//
|
||
reader := NewCanBitsReader(cf.CanData, 8)
|
||
//
|
||
f.Message = make([]byte, 0, 8)
|
||
for c := 0; c < 8; c++ {
|
||
data, ok := reader.ReadByte()
|
||
if !ok {
|
||
return false
|
||
}
|
||
f.Message = append(f.Message, data)
|
||
}
|
||
//
|
||
return true
|
||
}
|
||
|
||
/////////////////////////////////////////////////////
|
||
|
||
// BtmDataMessageTimeAFrame 数据帧-时间戳A
|
||
// offset = 0x0d
|
||
type BtmDataMessageTimeAFrame struct {
|
||
//帧ID
|
||
FId CanFrameId
|
||
//时间戳A
|
||
TimeA uint32
|
||
//CRC
|
||
Crc32A uint32
|
||
}
|
||
|
||
func NewBtmDataMessageTimeAFrame(sn byte) *BtmDataMessageTimeAFrame {
|
||
return &BtmDataMessageTimeAFrame{
|
||
FId: *NewCanFrameId(CAN_ADDR_RSP_ATP, CAN_ADDR_RSP_BTM, 0x80+0x0d, sn),
|
||
}
|
||
}
|
||
|
||
func (f *BtmDataMessageTimeAFrame) Encode() *CanetFrame {
|
||
cf := &CanetFrame{}
|
||
|
||
cf.CanId = f.FId
|
||
cf.CanLen = 8
|
||
cf.FF = true
|
||
cf.RTR = false
|
||
|
||
writer := NewCanBitsWriter(8)
|
||
|
||
byteMsk := uint32(0x00_00_00_ff)
|
||
|
||
writer.AddByte(byte((f.TimeA >> 24) & byteMsk))
|
||
writer.AddByte(byte((f.TimeA >> 16) & byteMsk))
|
||
writer.AddByte(byte((f.TimeA >> 8) & byteMsk))
|
||
writer.AddByte(byte(f.TimeA & byteMsk))
|
||
|
||
writer.AddByte(byte((f.Crc32A >> 24) & byteMsk))
|
||
writer.AddByte(byte((f.Crc32A >> 16) & byteMsk))
|
||
writer.AddByte(byte((f.Crc32A >> 8) & byteMsk))
|
||
writer.AddByte(byte(f.Crc32A & byteMsk))
|
||
|
||
cf.CanData = writer.(CanBusData).GetData()
|
||
|
||
return cf
|
||
}
|
||
|
||
func (f *BtmDataMessageTimeAFrame) Decode(cf *CanetFrame) bool {
|
||
f.FId = cf.CanId
|
||
//
|
||
reader := NewCanBitsReader(cf.CanData, 8)
|
||
//
|
||
f.TimeA = 0
|
||
a1, a1Ok := reader.ReadByte()
|
||
if !a1Ok {
|
||
return false
|
||
}
|
||
f.TimeA |= uint32(a1) << 24
|
||
//
|
||
a2, a2Ok := reader.ReadByte()
|
||
if !a2Ok {
|
||
return false
|
||
}
|
||
f.TimeA |= uint32(a2) << 16
|
||
//
|
||
a3, a3Ok := reader.ReadByte()
|
||
if !a3Ok {
|
||
return false
|
||
}
|
||
f.TimeA |= uint32(a3) << 8
|
||
//
|
||
a4, a4Ok := reader.ReadByte()
|
||
if !a4Ok {
|
||
return false
|
||
}
|
||
f.TimeA |= uint32(a4)
|
||
//
|
||
f.Crc32A = 0
|
||
c1, c1Ok := reader.ReadByte()
|
||
if !c1Ok {
|
||
return false
|
||
}
|
||
f.Crc32A |= uint32(c1) << 24
|
||
//
|
||
c2, c2Ok := reader.ReadByte()
|
||
if !c2Ok {
|
||
return false
|
||
}
|
||
f.Crc32A |= uint32(c2) << 16
|
||
//
|
||
c3, c3Ok := reader.ReadByte()
|
||
if !c3Ok {
|
||
return false
|
||
}
|
||
f.Crc32A |= uint32(c3) << 8
|
||
//
|
||
c4, c4Ok := reader.ReadByte()
|
||
if !c4Ok {
|
||
return false
|
||
}
|
||
f.Crc32A |= uint32(c4)
|
||
//
|
||
return true
|
||
}
|
||
|
||
//////////////////////////////////////////////////////////
|
||
|
||
// BtmDataMessageTimeBFrame 数据帧-时间戳B
|
||
// offset = 0x0e
|
||
type BtmDataMessageTimeBFrame struct {
|
||
//帧ID
|
||
FId CanFrameId
|
||
//时间戳B
|
||
TimeB uint32
|
||
//CRC
|
||
Crc32B uint32
|
||
}
|
||
|
||
func NewBtmDataMessageTimeBFrame(sn byte) *BtmDataMessageTimeBFrame {
|
||
return &BtmDataMessageTimeBFrame{
|
||
FId: *NewCanFrameId(CAN_ADDR_RSP_ATP, CAN_ADDR_RSP_BTM, 0x80+0x0e, sn),
|
||
}
|
||
}
|
||
func (f *BtmDataMessageTimeBFrame) Encode() *CanetFrame {
|
||
cf := &CanetFrame{}
|
||
cf.CanId = f.FId
|
||
cf.CanLen = 8
|
||
cf.FF = true
|
||
cf.RTR = false
|
||
//
|
||
writer := NewCanBitsWriter(8)
|
||
|
||
byteMsk := uint32(0x00_00_00_ff)
|
||
|
||
writer.AddByte(byte((f.TimeB >> 24) & byteMsk))
|
||
writer.AddByte(byte((f.TimeB >> 16) & byteMsk))
|
||
writer.AddByte(byte((f.TimeB >> 8) & byteMsk))
|
||
writer.AddByte(byte(f.TimeB & byteMsk))
|
||
|
||
writer.AddByte(byte((f.Crc32B >> 24) & byteMsk))
|
||
writer.AddByte(byte((f.Crc32B >> 16) & byteMsk))
|
||
writer.AddByte(byte((f.Crc32B >> 8) & byteMsk))
|
||
writer.AddByte(byte(f.Crc32B & byteMsk))
|
||
|
||
cf.CanData = writer.(CanBusData).GetData()
|
||
|
||
return cf
|
||
}
|
||
func (f *BtmDataMessageTimeBFrame) Decode(cf *CanetFrame) bool {
|
||
f.FId = cf.CanId
|
||
//
|
||
reader := NewCanBitsReader(cf.CanData, 8)
|
||
//
|
||
f.TimeB = 0
|
||
a1, a1Ok := reader.ReadByte()
|
||
if !a1Ok {
|
||
return false
|
||
}
|
||
f.TimeB |= uint32(a1) << 24
|
||
//
|
||
a2, a2Ok := reader.ReadByte()
|
||
if !a2Ok {
|
||
return false
|
||
}
|
||
f.TimeB |= uint32(a2) << 16
|
||
//
|
||
a3, a3Ok := reader.ReadByte()
|
||
if !a3Ok {
|
||
return false
|
||
}
|
||
f.TimeB |= uint32(a3) << 8
|
||
//
|
||
a4, a4Ok := reader.ReadByte()
|
||
if !a4Ok {
|
||
return false
|
||
}
|
||
f.TimeB |= uint32(a4)
|
||
//
|
||
f.Crc32B = 0
|
||
c1, c1Ok := reader.ReadByte()
|
||
if !c1Ok {
|
||
return false
|
||
}
|
||
f.Crc32B |= uint32(c1) << 24
|
||
//
|
||
c2, c2Ok := reader.ReadByte()
|
||
if !c2Ok {
|
||
return false
|
||
}
|
||
f.Crc32B |= uint32(c2) << 16
|
||
//
|
||
c3, c3Ok := reader.ReadByte()
|
||
if !c3Ok {
|
||
return false
|
||
}
|
||
f.Crc32B |= uint32(c3) << 8
|
||
//
|
||
c4, c4Ok := reader.ReadByte()
|
||
if !c4Ok {
|
||
return false
|
||
}
|
||
f.Crc32B |= uint32(c4)
|
||
//
|
||
return true
|
||
}
|
||
|
||
////////////////////////////////////////////////////////////
|
||
|
||
// BtmDataMessageEndFrame 数据帧-结束
|
||
// offset = 0x7f
|
||
type BtmDataMessageEndFrame struct {
|
||
//帧ID
|
||
FId CanFrameId
|
||
//TK time B
|
||
TkB uint32
|
||
//CRC32C校验包含对状态应答帧与数据帧的总校验,总共为8×17-4=132字节,4-即Crc32C
|
||
//1个状态应答帧 + 13个BtmDataMessageFrame + 1个BtmDataMessageTimeAFrame + 1个BtmDataMessageTimeBFrame + 1个BtmDataMessageEndFrame
|
||
Crc32C uint32
|
||
}
|
||
|
||
func NewBtmDataMessageEndFrame(sn byte) *BtmDataMessageEndFrame {
|
||
return &BtmDataMessageEndFrame{
|
||
FId: *NewCanFrameId(CAN_ADDR_RSP_ATP, CAN_ADDR_RSP_BTM, 0x80+0x7f, sn),
|
||
}
|
||
}
|
||
|
||
func (f *BtmDataMessageEndFrame) Encode() *CanetFrame {
|
||
cf := &CanetFrame{}
|
||
|
||
cf.CanId = f.FId
|
||
cf.CanLen = 8
|
||
cf.FF = true
|
||
cf.RTR = false
|
||
//
|
||
writer := NewCanBitsWriter(8)
|
||
//
|
||
byteMsk := uint32(0x00_00_00_ff)
|
||
//
|
||
writer.AddByte(byte((f.TkB >> 24) & byteMsk))
|
||
writer.AddByte(byte((f.TkB >> 16) & byteMsk))
|
||
writer.AddByte(byte((f.TkB >> 8) & byteMsk))
|
||
writer.AddByte(byte(f.TkB & byteMsk))
|
||
//
|
||
writer.AddByte(byte((f.Crc32C >> 24) & byteMsk))
|
||
writer.AddByte(byte((f.Crc32C >> 16) & byteMsk))
|
||
writer.AddByte(byte((f.Crc32C >> 8) & byteMsk))
|
||
writer.AddByte(byte(f.Crc32C & byteMsk))
|
||
//
|
||
cf.CanData = writer.(CanBusData).GetData()
|
||
//
|
||
return cf
|
||
}
|
||
func (f *BtmDataMessageEndFrame) Decode(cf *CanetFrame) bool {
|
||
f.FId = cf.CanId
|
||
//
|
||
reader := NewCanBitsReader(cf.CanData, 8)
|
||
//
|
||
f.TkB = 0
|
||
a1, a1Ok := reader.ReadByte()
|
||
if !a1Ok {
|
||
return false
|
||
}
|
||
f.TkB |= uint32(a1) << 24
|
||
//
|
||
a2, a2Ok := reader.ReadByte()
|
||
if !a2Ok {
|
||
return false
|
||
}
|
||
f.TkB |= uint32(a2) << 16
|
||
//
|
||
a3, a3Ok := reader.ReadByte()
|
||
if !a3Ok {
|
||
return false
|
||
}
|
||
f.TkB |= uint32(a3) << 8
|
||
//
|
||
a4, a4Ok := reader.ReadByte()
|
||
if !a4Ok {
|
||
return false
|
||
}
|
||
f.TkB |= uint32(a4)
|
||
//
|
||
f.Crc32C = 0
|
||
c1, c1Ok := reader.ReadByte()
|
||
if !c1Ok {
|
||
return false
|
||
}
|
||
f.Crc32C |= uint32(c1) << 24
|
||
//
|
||
c2, c2Ok := reader.ReadByte()
|
||
if !c2Ok {
|
||
return false
|
||
}
|
||
f.Crc32C |= uint32(c2) << 16
|
||
//
|
||
c3, c3Ok := reader.ReadByte()
|
||
if !c3Ok {
|
||
return false
|
||
}
|
||
f.Crc32C |= uint32(c3) << 8
|
||
//
|
||
c4, c4Ok := reader.ReadByte()
|
||
if !c4Ok {
|
||
return false
|
||
}
|
||
f.Crc32C |= uint32(c4)
|
||
//
|
||
return true
|
||
}
|
||
|
||
// 32位时间戳,转换成字节数组
|
||
// 大端模型
|
||
func canTimeToBytes(ts uint32) []byte {
|
||
rt := make([]byte, 0, 4)
|
||
byteMsk := uint32(0x00_00_00_ff)
|
||
rt = append(rt, byte((ts>>24)&byteMsk))
|
||
rt = append(rt, byte((ts>>16)&byteMsk))
|
||
rt = append(rt, byte((ts>>8)&byteMsk))
|
||
rt = append(rt, byte(ts&byteMsk))
|
||
return rt
|
||
}
|