From 1b3f130e4e1faa1b4b32d974f63a0a5b68f18b5d Mon Sep 17 00:00:00 2001 From: tiger_zhou Date: Thu, 13 Jun 2024 18:13:21 +0800 Subject: [PATCH] =?UTF-8?q?=E6=97=A5=E5=BF=97=E8=B0=83=E6=95=B4=E5=8F=8A?= =?UTF-8?q?=E7=8E=B0=E5=9C=BA=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- third_party/btm_vobc/btm_vobc_test.go | 13 ++++++++ third_party/message/train_pc_sim_message.go | 33 +++++++++++-------- third_party/train_pc_sim/train_pc_sim.go | 17 +++++++--- .../memory/wayside_simulation_train_pc.go | 4 +-- 4 files changed, 47 insertions(+), 20 deletions(-) diff --git a/third_party/btm_vobc/btm_vobc_test.go b/third_party/btm_vobc/btm_vobc_test.go index caca1b6..b7bec67 100644 --- a/third_party/btm_vobc/btm_vobc_test.go +++ b/third_party/btm_vobc/btm_vobc_test.go @@ -5,7 +5,9 @@ import ( "encoding/hex" "encoding/json" "fmt" + "github.com/snksoft/crc" "joylink.club/bj-rtsts-server/third_party/message" + "joylink.club/bj-rtsts-server/third_party/train_pc_sim" "joylink.club/bj-rtsts-server/third_party/udp" "sync/atomic" "testing" @@ -17,6 +19,17 @@ const ( code2 = "fffee601804f004d00800600002a004984ec6f4e902ef6912284000000000000000000000000000000067e000000000000411b0000f5bbee397698fffd" ) +func TestMsg22(t *testing.T) { + msg := &message.TrainPcSimBaseMessage{} + msg.Type = train_pc_sim.SENDER_TRAIN_OUTR_INFO + data := []byte{0, 1} + msg.Data = data + code := msg.Encode() + hexCode := hex.EncodeToString(code) + fmt.Println(hexCode) + fmt.Println(crc.CalculateCRC(crc.CRC16, []byte{0})) +} + var message_id atomic.Uint32 func TestConn(t *testing.T) { diff --git a/third_party/message/train_pc_sim_message.go b/third_party/message/train_pc_sim_message.go index 1b8b753..79dbc05 100644 --- a/third_party/message/train_pc_sim_message.go +++ b/third_party/message/train_pc_sim_message.go @@ -12,7 +12,7 @@ import ( const PC_SIM_HEADER = 0xEB type TrainPcSimBaseMessage struct { - Type uint16 + Type byte DataLen byte Data []byte Crc uint16 // 校验码 @@ -22,13 +22,17 @@ type TrainPcSimBaseMessage struct { func (tp *TrainPcSimBaseMessage) Encode() []byte { pack := make([]byte, 0) pack = append(pack, PC_SIM_HEADER) - pack = binary.BigEndian.AppendUint16(pack, tp.Type) - pack = append(pack, byte(len(tp.Data))) - dataBufs := bytes.NewBuffer(nil) - binary.Write(dataBufs, binary.BigEndian, tp.Data) - data := dataBufs.Bytes() - pack = append(pack, data...) - pack = binary.BigEndian.AppendUint16(pack, uint16(crc.CalculateCRC(crc.CRC16, data))) + pack = append(pack, tp.Type) + + pack = append(pack, byte(len(tp.Data)+1+1+1+2)) + if len(tp.Data) > 0 { + dataBufs := bytes.NewBuffer(nil) + binary.Write(dataBufs, binary.BigEndian, tp.Data) + data := dataBufs.Bytes() + pack = append(pack, data...) + } + + pack = binary.BigEndian.AppendUint16(pack, uint16(crc.CalculateCRC(crc.CRC16, pack[1:]))) return pack } @@ -42,20 +46,21 @@ func (tp *TrainPcSimBaseMessage) Decode(data []byte) error { if h != PC_SIM_HEADER { return fmt.Errorf("") } - var pcType uint16 - binary.Read(buf, binary.BigEndian, &pcType) - len, _ := buf.ReadByte() - if buf.Len() < int(len)+2 { + var pcType byte + pcType, _ = buf.ReadByte() + + dataLen, _ := buf.ReadByte() + if buf.Len() < int(dataLen)-1-1-1+2 { return fmt.Errorf("") } - var dd = make([]byte, len) + var dd = make([]byte, dataLen) binary.Read(buf, binary.BigEndian, &dd) var crcCode uint16 binary.Read(buf, binary.BigEndian, &crcCode) tp.Type = pcType - tp.DataLen = len + tp.DataLen = dataLen tp.Data = dd tp.Crc = crcCode return nil diff --git a/third_party/train_pc_sim/train_pc_sim.go b/third_party/train_pc_sim/train_pc_sim.go index 1cbef67..978aca0 100644 --- a/third_party/train_pc_sim/train_pc_sim.go +++ b/third_party/train_pc_sim/train_pc_sim.go @@ -33,7 +33,7 @@ type TrainPcSim interface { //因文档说明不清楚,在调用的时候目前是注释状态,现场调试可能会用到 SendTrainDirection(train *state_proto.TrainState, trainForward, trainBackward bool) //发送应答器信息数据 - SendBaliseData(train *state_proto.TrainState, msgType uint16, data []byte) + SendBaliseData(train *state_proto.TrainState, msgType byte, data []byte) //发布列车控制的相关事件 PublishTrainControlEvent(train *state_proto.TrainState, events []TrainControlEvent) // CreateOrRemoveSpeedPLace 创建或删除速度位置信息 @@ -327,7 +327,7 @@ func (d *trainPcSimService) CreateOrRemoveTrain(train *state_proto.TrainState, m log = "创建列车" d.initConn(clientKey) } - msg := &message.TrainPcSimBaseMessage{Data: data, Type: uint16(msgType)} + msg := &message.TrainPcSimBaseMessage{Data: data, Type: msgType} rd := d.newPcSimclientMap[clientKey] if rd != nil { sd := msg.Encode() @@ -409,6 +409,9 @@ func (d *trainPcSimService) SendDriverActive(train *state_proto.TrainState) { msg.Type = SENDER_TRAIN_TC_NOT_ACTIVE } } + //:"创建列车-列车号:1,发送数据:eb0050010156e4"} + //{,"msg":"发送驾驶激活列车","1":"数据","!BADKEY":"eb0004002437"} + da := msg.Encode() slog.Info("发送驾驶激活列车", train.Id, "数据", hex.EncodeToString(da)) err := rd.tcpClient.Send(da) @@ -487,7 +490,7 @@ func (d *trainPcSimService) SendTrainDirection(train *state_proto.TrainState, tr } } -func (d *trainPcSimService) SendBaliseData(train *state_proto.TrainState, msgType uint16, data []byte) { +func (d *trainPcSimService) SendBaliseData(train *state_proto.TrainState, msgType byte, data []byte) { msg := &message.TrainPcSimBaseMessage{} msg.Type = msgType msg.Data = data @@ -527,7 +530,13 @@ func (d *trainPcSimService) PublishTrainControlEvent(train *state_proto.TrainSta msg.Type = SENDER_TRAIN_OUTR_INFO data := []byte{event.Command, event.Status} msg.Data = data - rd.tcpClient.Send(msg.Encode()) + code := msg.Encode() + hexCode := hex.EncodeToString(code) + slog.Info(fmt.Sprintf("输出列车控制输出量,命令码位:%v 对应状态:%v,发送数据:%v", event.Command, event.Status, hexCode)) + err := rd.tcpClient.Send(code) + if err != nil { + slog.Error(fmt.Sprintf("输出列车控制输出量发送失败,命令码位:%v 对应状态:%v,发送数据:%v", event.Command, event.Status, hexCode)) + } //client.Send(msg.Encode()) //FireTrainControlEventType.Publish(world, &event) diff --git a/ts/simulation/wayside/memory/wayside_simulation_train_pc.go b/ts/simulation/wayside/memory/wayside_simulation_train_pc.go index e743ac2..b608ed9 100644 --- a/ts/simulation/wayside/memory/wayside_simulation_train_pc.go +++ b/ts/simulation/wayside/memory/wayside_simulation_train_pc.go @@ -167,14 +167,14 @@ func trainControlDriverKey(train *state_proto.TrainState, request *request_proto } panic(sys_error.New("驾驶端不能同时激活")) } - train.TrainActiveDirection = 0 + /*train.TrainActiveDirection = 0 if vobc.Tc1Active { //train.TrainActiveDirection = 1 train.TrainRunUp = true } else if vobc.Tc2Active { //train.TrainActiveDirection = 2 train.TrainRunUp = false - } + }*/ var addNew = true for _, k := range tcc.DriverKey { if k.Id == deviceId {