From 2355c8c51878f9093aa1bc678a0eaceacd07ccbf Mon Sep 17 00:00:00 2001 From: xzb <223@qq.com> Date: Thu, 16 Nov 2023 18:12:46 +0800 Subject: [PATCH] balise btm --- third_party/message/balise_btm.go | 38 +++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/third_party/message/balise_btm.go b/third_party/message/balise_btm.go index 9038948..c576609 100644 --- a/third_party/message/balise_btm.go +++ b/third_party/message/balise_btm.go @@ -80,6 +80,9 @@ func NewAtpRequestFrame(sn byte) *AtpRequestFrame { return &AtpRequestFrame{ FId: *NewCanFrameId(CAN_ADDR_REQ_BTM, CAN_ADDR_REQ_ATP, CAN_FRAME_ATP_REQ, sn), } +} +func (f *AtpRequestFrame) Decode(bits *CanBits) { + } func (f *AtpRequestFrame) Encode() *CanBits { bits := NewCanBits(12) @@ -103,12 +106,43 @@ func (f *AtpRequestFrame) Encode() *CanBits { bits.AddBits(byte(f.Speed>>8), 4) bits.AddByte(byte(f.Speed)) //时间 - + timeArr := make([]byte, 4, 4) + timeArr[0] = byte(f.time >> 24) + timeArr[1] = byte(f.time >> 16) + timeArr[2] = byte(f.time >> 8) + timeArr[3] = byte(f.time) + for _, timeByte := range timeArr { + bits.AddByte(timeByte) + } + //crc + crc16 := calculateAtpReqCrc16(timeArr) + bits.AddByte(byte(crc16 >> 8)) + bits.AddByte(byte(crc16)) // return bits } -///////////////////////////////////////////// +///////////////////// CRC //////////////////////// + +const ( + CAN_CRC16_ATPREQ = 0x11021 +) + +var ( + crc16AtpReqTable []uint32 = nil +) + +func CanCreateCrcTable() { + if crc16AtpReqTable == nil { + crc16AtpReqTable = CreateCrcTable(CAN_CRC16_ATPREQ, 16, false) + } +} +func calculateAtpReqCrc16(data []byte) uint16 { + crc := CrcTableBased(data, 16, 0, false, false, 0, crc16AtpReqTable) + return uint16(crc) +} + +///////////////////// bit 流处理 //////////////////////// // CanBits 可以在CAN总线上传输的bit流 // 按bit位来存储数据