package message import ( "fmt" "strings" ) // BtmStatusRspFrame BTM发往ATP的状态应答帧 type BtmStatusRspFrame struct { //帧ID FId CanFrameId //数据流水号,0~255变化,每次加一 Dsn byte //应答器计数(每过一个应答器加一,在同一个应答器内不变) BaliseCounter byte //报文计数器(每解出一个报文加一) MessageCounter byte //功率放大器状态,true-1 turn on ;false-0 turn off PowerAmplifierOn bool //ATP查询帧CRC16校验结果,true-1校验失败,false-0校验通过 AtpReqCrcCheckWrong bool //功率放大器故障,true-1故障,false-0正常 PowerAmplifierFailure bool //天线故障,true-1故障,false-0正常 AntennaFault bool //BTM detailed code DetailedCode byte //有具体报文内容时TK A时刻填充为CD信号上升沿时刻,报文全零时TK A时刻填充为CD信号下降沿时刻。 TkTimeA uint32 IsTrainPcSim bool } func NewBtmStatusRspFrame(sn byte, isTrainPcSim bool) *BtmStatusRspFrame { return &BtmStatusRspFrame{ FId: *NewCanFrameId(CAN_ADDR_RSP_ATP, CAN_ADDR_RSP_BTM, CAN_FRAME_STATUS_RSP, sn), IsTrainPcSim: isTrainPcSim, } } func (f *BtmStatusRspFrame) Decode(cf *CanetFrame) bool { f.FId = cf.CanId // reader := NewCanBitsReader(cf.CanData, 8) //数据流水号 if d, ok := reader.ReadByte(); ok { f.Dsn = d } else { return false } //应答器计数 if d, ok := reader.ReadByte(); ok { f.BaliseCounter = d } else { return false } //报文计数器 if d, ok := reader.ReadByte(); ok { f.MessageCounter = d } else { return false } //功率放大器状态 if d, ok := reader.ReadBits(1); ok { f.PowerAmplifierOn = 1 == d } else { return false } //ATP查询帧CRC16校验结果 if d, ok := reader.ReadBits(1); ok { f.AtpReqCrcCheckWrong = 1 == d } else { return false } //功率放大器故障 if d, ok := reader.ReadBits(1); ok { f.PowerAmplifierFailure = 1 == d } else { return false } //天线故障 if d, ok := reader.ReadBits(1); ok { f.AntennaFault = 1 == d } else { return false } //BTM detailed code if d, ok := reader.ReadBits(4); ok { f.DetailedCode = d } else { return false } //tk time a tk1, tk1Ok := reader.ReadByte() if !tk1Ok { return false } tk2, tk2Ok := reader.ReadByte() if !tk2Ok { return false } tk3, tk3Ok := reader.ReadByte() if !tk3Ok { return false } tk4, tk4Ok := reader.ReadByte() if !tk4Ok { return false } f.TkTimeA = 0 f.TkTimeA |= uint32(tk1) << 24 f.TkTimeA |= uint32(tk2) << 16 f.TkTimeA |= uint32(tk3) << 8 f.TkTimeA |= uint32(tk4) // return true } func (f *BtmStatusRspFrame) Encode() *CanetFrame { cf := &CanetFrame{} cf.IsTrainPcSim = f.IsTrainPcSim cf.CanId = f.FId cf.CanLen = 8 cf.FF = true cf.RTR = false // writer := NewCanBitsWriter(8) writer.AddByte(f.Dsn) writer.AddByte(f.BaliseCounter) writer.AddByte(f.MessageCounter) // if f.PowerAmplifierOn { writer.AddBits(1, 1) } else { writer.AddBits(0, 1) } // if f.AtpReqCrcCheckWrong { writer.AddBits(1, 1) } else { writer.AddBits(0, 1) } // if f.PowerAmplifierFailure { writer.AddBits(1, 1) } else { writer.AddBits(0, 1) } // if f.AntennaFault { writer.AddBits(1, 1) } else { writer.AddBits(0, 1) } // writer.AddBits(f.DetailedCode, 4) //tk time a tkMsk := uint32(0x00_00_00_ff) tk1 := byte((f.TkTimeA >> 24) & tkMsk) tk2 := byte((f.TkTimeA >> 16) & tkMsk) tk3 := byte((f.TkTimeA >> 8) & tkMsk) tk4 := byte(f.TkTimeA & tkMsk) writer.AddByte(tk1) writer.AddByte(tk2) writer.AddByte(tk3) writer.AddByte(tk4) // cf.CanData = writer.(CanBusData).GetData() return cf } func (f *BtmStatusRspFrame) String() string { sb := strings.Builder{} sb.WriteString(fmt.Sprintf("BtmStatusRspFrame ID1 = 0x%0x, ID2 = 0x%0x, ID3 = 0x%0x, ID4 = 0x%0x,", f.FId.ID1, f.FId.ID2, f.FId.ID3, f.FId.ID4)) sb.WriteString(fmt.Sprintf("Dsn = %d", f.Dsn)) sb.WriteString(fmt.Sprintf(",BaliseCounter = %d", f.BaliseCounter)) sb.WriteString(fmt.Sprintf(",MessageCounter = %d", f.MessageCounter)) sb.WriteString(fmt.Sprintf(",PowerAmplifierOn = %t", f.PowerAmplifierOn)) sb.WriteString(fmt.Sprintf(",AtpReqCrcCheckWrong = %t", f.AtpReqCrcCheckWrong)) sb.WriteString(fmt.Sprintf(",PowerAmplifierFailure = %t", f.PowerAmplifierFailure)) sb.WriteString(fmt.Sprintf(",AntennaFault = %t", f.AntennaFault)) sb.WriteString(fmt.Sprintf(",DetailedCode = %d", f.DetailedCode)) sb.WriteString(fmt.Sprintf(",TkTimeA = %d", f.TkTimeA)) return sb.String() }