This commit is contained in:
walker 2023-11-17 14:12:41 +08:00
commit 27e1de404f

View File

@ -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位来存储数据