95 lines
2.4 KiB
Go
95 lines
2.4 KiB
Go
package message
|
||
|
||
import "encoding/binary"
|
||
|
||
//数据帧(0x80+扩展类型),一个CAN帧只能存储8个字节,对于一个应答器报文,分片到多个CAN数据帧中
|
||
|
||
// BtmDataMessageFrame 数据帧-应答器报文
|
||
// offset = [0x00,0x0c] 共13个即可用于存储应答器报文的共104个字节
|
||
// BtmDataMessageTimeAFrame 数据帧-时间戳A
|
||
// offset = 0x0d
|
||
// BtmDataMessageTimeBFrame 数据帧-时间戳B
|
||
// offset = 0x0e
|
||
// BtmDataMessageEndFrame 数据帧-结束
|
||
// offset = 0x7f
|
||
const (
|
||
DATA_TIME_A_OFFSET = 0x0d
|
||
DATA_TIME_B_OFFSET = 0x0e
|
||
DATA_END_OFFSET = 0x7f
|
||
)
|
||
|
||
type BtmDataMessageAtpFrame struct {
|
||
//帧ID
|
||
FId CanFrameId
|
||
//8字节,应答器报文片段
|
||
Message []byte
|
||
}
|
||
type BtmDataMessageAtpTimeAndEndFrame struct {
|
||
//帧ID
|
||
FId CanFrameId
|
||
//时间戳A
|
||
Time uint32
|
||
//CRC
|
||
Crc32 uint32
|
||
}
|
||
|
||
func CreateBtmDataMessageDataAtpFrame(sn byte, offset byte) *BtmDataMessageAtpFrame {
|
||
return &BtmDataMessageAtpFrame{
|
||
FId: *NewCanFrameId(CAN_ADDR_RSP_ATP, CAN_ADDR_RSP_BTM, 0x80+offset, sn),
|
||
}
|
||
}
|
||
|
||
func (f *BtmDataMessageAtpFrame) Encode() *BtmHeadFrame {
|
||
cf := &BtmHeadFrame{}
|
||
cf.CanId = f.FId
|
||
cf.CanLen = 8
|
||
|
||
dataBuf := make([]byte, 0, 8)
|
||
for _, data := range f.Message {
|
||
dataBuf = append(dataBuf, data)
|
||
}
|
||
cf.CanData = dataBuf
|
||
return cf
|
||
}
|
||
func (f *BtmDataMessageAtpFrame) Decode(cf *BtmHeadFrame) 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
|
||
}
|
||
|
||
/////////////////////////////////////////////////////
|
||
|
||
func (atp *BtmDataMessageAtpTimeAndEndFrame) Encode() *BtmHeadFrame {
|
||
cf := &BtmHeadFrame{}
|
||
cf.CanId = atp.FId
|
||
cf.CanLen = 8
|
||
data := make([]byte, 0)
|
||
data = binary.BigEndian.AppendUint32(data, atp.Time)
|
||
data = binary.BigEndian.AppendUint32(data, atp.Crc32)
|
||
cf.CanData = data
|
||
return cf
|
||
}
|
||
|
||
func CreateBtmDataMessageTimeAtpFrame(sn byte, offset byte) *BtmDataMessageAtpTimeAndEndFrame {
|
||
return &BtmDataMessageAtpTimeAndEndFrame{FId: *NewCanFrameId(CAN_ADDR_RSP_ATP, CAN_ADDR_RSP_BTM, 0x80+offset, sn)}
|
||
}
|
||
|
||
////////////////////////////////////////////////////////////
|
||
|
||
func BtmDataMessageEndAtpFrame(sn byte) *BtmDataMessageAtpTimeAndEndFrame {
|
||
return &BtmDataMessageAtpTimeAndEndFrame{
|
||
FId: *NewCanFrameId(CAN_ADDR_RSP_ATP, CAN_ADDR_RSP_BTM, 0xff, sn),
|
||
}
|
||
}
|