package message import ( "encoding/binary" "math" ) type DynamicsTurnoutInfo struct { LifeSignal uint16 Code uint16 NPosition bool RPosition bool } func (t *DynamicsTurnoutInfo) Encode() []byte { var data []byte data = binary.BigEndian.AppendUint16(data, t.LifeSignal) data = binary.BigEndian.AppendUint16(data, t.Code) var b byte if t.NPosition { b |= 1 << 7 } if t.RPosition { b |= 1 << 6 } data = append(data, b) return data } type DynamicsTrainInfo struct { //生命信号 LifeSignal uint16 //列车号(车辆) Number uint8 //列车长度 cm Len uint16 //列车所在轨道link Link uint8 //列车所在link偏移量(mm) LinkOffset uint32 //列车所在位置坡度值(‰) Slope uint16 //列车所在位置坡度走势(上/下坡) UpSlope bool //列车当前运行方向(偏移量增大/减小方向) Up bool //实际运行阻力(总)(N) TotalResistance int32 //阻力1(空气阻力)(N) AirResistance int32 //阻力2(坡道阻力)(N) SlopeResistance int32 //阻力3(曲线阻力)(N) CurveResistance int32 //列车运行速度(m/s) Speed float32 //头车速传1速度值(m/s) HeadSpeed1 float32 //头车速度2速度值(m/s) HeadSpeed2 float32 //尾车速传1速度值(m/s) TailSpeed1 float32 //尾车速度2速度值(m/s) TailSpeed2 float32 //头车雷达速度值(m/s) HeadRadarSpeed float32 //尾车雷达速度值(m/s) TailRadarSpeed float32 //加速度(m/s^2) Acceleration float32 } // 解析动力学的列车信息 func (t *DynamicsTrainInfo) Decode(buf []byte) error { t.LifeSignal = binary.BigEndian.Uint16(buf[0:2]) t.Number = buf[2] t.Len = binary.BigEndian.Uint16(buf[3:5]) t.Link = buf[5] t.LinkOffset = binary.BigEndian.Uint32(buf[6:10]) t.Slope = binary.BigEndian.Uint16(buf[10:12]) b := buf[12] t.UpSlope = (b & (1 << 7)) != 0 t.Up = (b & (1 << 6)) != 0 t.TotalResistance = int32(binary.BigEndian.Uint32(buf[14:18])) t.AirResistance = int32(binary.BigEndian.Uint32(buf[18:22])) t.SlopeResistance = int32(binary.BigEndian.Uint32(buf[22:26])) t.CurveResistance = int32(binary.BigEndian.Uint32(buf[26:30])) t.Speed = math.Float32frombits(binary.BigEndian.Uint32(buf[30:34])) t.HeadSpeed1 = math.Float32frombits(binary.BigEndian.Uint32(buf[34:38])) t.HeadSpeed2 = math.Float32frombits(binary.BigEndian.Uint32(buf[38:42])) t.TailSpeed1 = math.Float32frombits(binary.BigEndian.Uint32(buf[42:46])) t.TailSpeed2 = math.Float32frombits(binary.BigEndian.Uint32(buf[46:50])) t.HeadRadarSpeed = math.Float32frombits(binary.BigEndian.Uint32(buf[50:54])) t.TailRadarSpeed = math.Float32frombits(binary.BigEndian.Uint32(buf[54:58])) t.Acceleration = math.Float32frombits(binary.BigEndian.Uint32(buf[58:62])) return nil }