From 6211f61ecddd8dc02d074d0ffcf7ffd52960d13f Mon Sep 17 00:00:00 2001 From: tiger_zhou Date: Tue, 18 Jun 2024 17:10: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 --- bin/acc_conn-example.go | 145 ++++++++++++++++++-- bin/config/example.yml | 6 +- bin/config/example_config.go | 12 +- third_party/btm_vobc/btm_vobc2_test.go | 29 +++- third_party/btm_vobc/btm_vobc_test.go | 17 ++- third_party/message/btm_vobc_data.go | 22 +-- third_party/message/train_pc_sim_message.go | 18 +-- third_party/train_pc_sim/train_pc_sim.go | 17 +-- 8 files changed, 207 insertions(+), 59 deletions(-) diff --git a/bin/acc_conn-example.go b/bin/acc_conn-example.go index 5d90dd3..fcf58e3 100644 --- a/bin/acc_conn-example.go +++ b/bin/acc_conn-example.go @@ -1,12 +1,15 @@ package main import ( + "encoding/binary" "encoding/hex" "fmt" "github.com/spf13/viper" "joylink.club/bj-rtsts-server/bin/config" "joylink.club/bj-rtsts-server/const/balise_const" "joylink.club/bj-rtsts-server/third_party/message" + "joylink.club/bj-rtsts-server/third_party/tcp" + "joylink.club/bj-rtsts-server/third_party/train_pc_sim" "joylink.club/bj-rtsts-server/third_party/udp" "log/slog" "strconv" @@ -22,8 +25,8 @@ func initConfig() { cnf := viper.New() cnf.SetConfigName(config_name) cnf.SetConfigType("yml") - cnf.AddConfigPath("./bin/config/") - //cnf.AddConfigPath("./config/") + //cnf.AddConfigPath("./bin/config/") + //cnf.AddConfigPath("./") cnf.AddConfigPath(".") err := cnf.ReadInConfig() if err != nil { @@ -57,12 +60,94 @@ func initSpeedTest() { speedClient = udp.NewClient(fmt.Sprintf("%v:%v", exampleConfig.Speed.RemoteIp, exampleConfig.Speed.RemotePort)) go testSpeed(speedClient, 30) } + +var trainpcClient *tcp.TcpClient + +func initTrainPc() { + addr := fmt.Sprintf("%v:%v", exampleConfig.Trainpc.RemoteIp, exampleConfig.Trainpc.RemotePort) + + client2, err := tcp.StartTcpClient(addr, trainPcDataHandle, trainPcConnErr) + if err != nil { + fmt.Println("仿真列车pc连接失败", err) + return + } + trainpcClient = client2 + circleSendTrainActive() + circleSendTrainMockData() + go circleSendTrainSpeedPlace() +} + +func circleSendTrainActive() { + msg := &message.TrainPcSimBaseMessage{Data: []byte{0x01}, Type: train_pc_sim.RECIVE_TRAIN_CREATE_REMOVE} + data := msg.Encode() + fmt.Println(fmt.Sprintf("发送列车创建数据:%v", hex.EncodeToString(data))) + trainpcClient.Send(data) + act := &message.TrainPcSimBaseMessage{Data: make([]byte, 0), Type: train_pc_sim.SENDER_TRAIN_TC_ACTIVE} + actData := act.Encode() + fmt.Println(fmt.Sprintf("发送列车驾驶室激活:%v", hex.EncodeToString(actData))) + trainpcClient.Send(actData) +} + +func circleSendTrainMockData() { + msg := &message.TrainPcSimBaseMessage{} + msg.Type = train_pc_sim.SENDER_TRAIN_OUTR_INFO + data := []byte{0x00, 1} + msg.Data = data + code := msg.Encode() + fmt.Println(fmt.Sprintf("发送列车模拟量输出数据:%v 模拟量下标:%v", hex.EncodeToString(code), 0x00)) + trainpcClient.Send(code) + //time.Sleep(time.Millisecond * 1000) + msg = &message.TrainPcSimBaseMessage{} + msg.Type = train_pc_sim.SENDER_TRAIN_OUTR_INFO + data = []byte{0x01, 1} + msg.Data = data + code = msg.Encode() + fmt.Println(fmt.Sprintf("发送列车模拟量输出数据:%v 模拟量下标:%v", hex.EncodeToString(code), 0x01)) + trainpcClient.Send(code) + //time.Sleep(time.Millisecond * 1000) + msg = &message.TrainPcSimBaseMessage{} + msg.Type = train_pc_sim.SENDER_TRAIN_OUTR_INFO + data = []byte{0x02, 0} + msg.Data = data + code = msg.Encode() + fmt.Println(fmt.Sprintf("发送列车模拟量输出数据:%v 模拟量下标:%v", hex.EncodeToString(code), 0x02)) + trainpcClient.Send(code) +} +func circleSendTrainSpeedPlace() { + for { + data := make([]byte, 0) + data = binary.BigEndian.AppendUint16(data, uint16(1)) + data = binary.BigEndian.AppendUint32(data, uint32(3)) + data = binary.BigEndian.AppendUint32(data, uint32(4)) + data = binary.BigEndian.AppendUint32(data, uint32(5)) + data = binary.BigEndian.AppendUint32(data, uint32(6)) + now := time.Now().UTC() + sec := now.Unix() + data = binary.BigEndian.AppendUint32(data, uint32(sec)) + data = binary.BigEndian.AppendUint16(data, uint16(now.UnixMilli()-sec*1000)) + + bm := &message.TrainPcSimBaseMessage{Type: train_pc_sim.SENDER_TRAIN_LOCATION_INFO, Data: data} + + dataCode := bm.Encode() + fmt.Println(fmt.Sprintf("发送列车位置信息:%v", hex.EncodeToString(dataCode))) + trainpcClient.Send(dataCode) + time.Sleep(time.Millisecond * 80) + } +} +func trainPcDataHandle(n int, data []byte) { + hexData := hex.EncodeToString(data) + slog.Info(fmt.Sprintf("列车pc仿真接口接受数据:%v", hexData)) +} +func trainPcConnErr(err error) { + +} func main() { initConfig() - initBtmTest() + //initBtmTest() + //initTrainPc() //initAccTest() - //initSpeedTest() + initSpeedTest() for { } @@ -74,11 +159,15 @@ var freeBtmMsg = strings.Repeat("00", balise_const.UserTelegramByteLen) func sendPacket(lifeNum uint32, autoId byte) { userMsg, _ := hex.DecodeString(testUserBtmMsg) - msg := &message.BtmVobcMessage{FontTtl: 5, BtmStatus: 0x00, DecodeTime: 10, BackTtl: 4, BtmMsg: userMsg, ResponseTime: 10, + msg := &message.BtmVobcMessage{FontTtl: 2, BtmStatus: 0x00, DecodeTime: 10, BackTtl: 2, BtmMsg: userMsg, ResponseTime: 10, VobcLifeNum: lifeNum, BaseBtmVobc: message.BaseBtmVobc{AutoIdFrame: autoId}, MsgSerial: message.GetAutoMessageId()} sendData := msg.Encode() fmt.Println("发送btm vobc len:", len(sendData), "报文:", hex.EncodeToString(sendData), "报文序列号:", msg.MsgSerial) - btmCli.Send(sendData) + time.Sleep(time.Millisecond * 100) + err := btmCli.Send(sendData) + if err != nil { + fmt.Println(fmt.Sprintf("发送失败%v", err.Error())) + } /*freeMsg, _ := hex.DecodeString(freeBtmMsg) @@ -96,16 +185,22 @@ func sendPacketFree(lifeNum uint32, autoId byte, msgs byte) { newMsg = 1 } msg2 := &message.BtmVobcMsgFree{BtmStatus: 0x00, WorkTemperature: 10, Fun1: uint16(0), Fun2: uint16(0), Fun3: uint16(0), Fun4: uint16(0), - FreeMsg: freeMsg, RespTime: 20, VobcLifeNum: lifeNum, BaseBtmVobc: message.BaseBtmVobc{AutoIdFrame: autoId}, MsgSerial: newMsg} + //FreeMsg: freeMsg, RespTime: 20, VobcLifeNum: lifeNum, BaseBtmVobc: message.BaseBtmVobc{AutoIdFrame: autoId}, MsgSerial: newMsg} + FreeMsg: freeMsg, RespTime: 20, VobcLifeNum: lifeNum, BaseBtmVobc: message.BaseBtmVobc{AutoIdFrame: autoId}, MsgSerial: message.GetAutoMessageId()} sendData2 := msg2.Encode() fmt.Println("发送btm vobc 空报文:", hex.EncodeToString(sendData2), "len:", len(sendData2), "报文序列号:", msg2.MsgSerial, "atoId=", autoId, "报文序列号:", msg2.MsgSerial) - btmCli.Send(sendData2) + time.Sleep(time.Millisecond * 100) + err := btmCli.Send(sendData2) + if err != nil { + fmt.Println(fmt.Sprintf("发送失败%v", err.Error())) + } } -func RequestFramePackets(req *message.BtmVobcReq) { - fmt.Println(fmt.Sprintf("接受 请求帧 frameStatus:%v,messageType:%v,lifeNum:%v,序列号:%v", req.FrameStatus, req.MessageType, req.VobcLifeNum, req.MessageSerial)) +func RequestFramePackets(req *message.BtmVobcReq, vobcLife uint32) { + //fmt.Println(fmt.Sprintf("接受 请求帧 frameStatus:%v,messageType:%v,lifeNum:%v,序列号:%v", req.FrameStatus, req.MessageType, req.VobcLifeNum, req.MessageSerial)) if req.FrameStatus == message.REQ_FRAME_STATUS_BOOT && req.MessageType == message.REQ_PACKETS_TYPE_BOOT { - sendPacketFree(req.VobcLifeNum, req.AutoIdFrame, req.MessageSerial) + fmt.Println("000000000000000000000000000") + sendPacketFree(vobcLife, req.AutoIdFrame, req.MessageSerial) } else if req.FrameStatus == message.REQ_FRAME_STATUS_OK { //帧正确,删除之前发送的数据 fmt.Println("11111111111111111111") @@ -115,8 +210,13 @@ func RequestFramePackets(req *message.BtmVobcReq) { fmt.Println("22222222222222222") } } + +var btmReceiveTime = time.Now().UnixMilli() +var vobcNumLife uint32 = 0 + func handleBtmVobcFrames(cfs []byte) { - fmt.Println("收到源数据:%v", hex.EncodeToString(cfs)) + + fmt.Println(fmt.Sprintf("收到源数据:%v ,请求帧时间:%v ,vobcLife:%v", hex.EncodeToString(cfs), time.Now().UnixMilli()-btmReceiveTime, vobcNumLife)) frameType, dataText, err := message.BtmVobcDecode(cfs) if err != nil { return @@ -124,11 +224,28 @@ func handleBtmVobcFrames(cfs []byte) { if frameType == message.COMMAND_TYPE { idCommand := &message.BtmVobcIdCommand{} idCommand.Decode(dataText) - sendPacket(idCommand.VobcLifeNum, idCommand.AutoIdFrame) + if vobcNumLife <= 0 { + vobcNumLife = idCommand.VobcLifeNum + } + sendPacketFree(vobcNumLife, idCommand.AutoIdFrame, idCommand.AutoIdFrame) + //sendPacket(vobcNumLife, idCommand.AutoIdFrame) } else if frameType == message.REQUEST_TYPE { + if vobcNumLife <= 0 { + return + } else { + fmt.Println(fmt.Sprintf("准备发送vobcLife:%v", vobcNumLife)) + } req := &message.BtmVobcReq{} req.Decode(dataText) - RequestFramePackets(req) + fmt.Println(fmt.Sprintf("接受 请求帧 frameStatus:%v,messageType:%v,lifeNum:%v,序列号:%v", req.FrameStatus, req.MessageType, req.VobcLifeNum, req.MessageSerial)) + if time.Now().UnixMilli()-btmReceiveTime > 20*1000 { + idCommand := &message.BtmVobcIdCommand{} + idCommand.Decode(dataText) + sendPacket(vobcNumLife, idCommand.AutoIdFrame) + } else { + + RequestFramePackets(req, vobcNumLife) + } } else { slog.Error(fmt.Sprintf("btm vobc 解析未知命令帧类型:0x%v,原始数据:%v,长度:%v", strconv.FormatInt(int64(frameType), 16), hex.EncodeToString(cfs), len(cfs))) return diff --git a/bin/config/example.yml b/bin/config/example.yml index 575bfe2..4735647 100644 --- a/bin/config/example.yml +++ b/bin/config/example.yml @@ -8,4 +8,8 @@ btm: remoteIp: "192.168.1.199" remotePort: 4196 localIp: "192.168.1.150" - localPort: 4646 \ No newline at end of file + localPort: 4646 +trainpc: + remoteIp: "192.168.3.211" + remotePort: 10007 + diff --git a/bin/config/example_config.go b/bin/config/example_config.go index 4892598..cce5e07 100644 --- a/bin/config/example_config.go +++ b/bin/config/example_config.go @@ -1,9 +1,10 @@ package config type ExampleConfig struct { - Acc acc - Speed speed - Btm btm + Acc acc + Speed speed + Btm btm + Trainpc trainpc } type acc struct { RemoteIp string @@ -19,3 +20,8 @@ type btm struct { LocalIp string LocalPort int } + +type trainpc struct { + RemoteIp string + RemotePort int +} diff --git a/third_party/btm_vobc/btm_vobc2_test.go b/third_party/btm_vobc/btm_vobc2_test.go index dcc2884..d8a5238 100644 --- a/third_party/btm_vobc/btm_vobc2_test.go +++ b/third_party/btm_vobc/btm_vobc2_test.go @@ -3,8 +3,11 @@ package btm_vobc import ( "encoding/hex" "fmt" + "joylink.club/bj-rtsts-server/const/balise_const" "joylink.club/bj-rtsts-server/third_party/message" + "strings" "testing" + "time" ) const ( @@ -106,6 +109,7 @@ func TranslateFromFFFE(pSrc []byte) ([]byte, uint16) { return pTgt[:tgtPos], tgtPos } + func TestTss(t *testing.T) { ss := "fffee601804f004d00921d21002a00ea014fef63995bff009122ce0000000000000000000000000000211d90000000000000000000007b07310379cefffd" ss = "fffee601804f004d00444a1c002a00473bbfa11d4b3b029122a800000000000000000000000000001c4a42000000000000000000008970492fff00d0fffd" @@ -130,13 +134,26 @@ func TestFffe(t *testing.T) { msg := &message.BtmVobcMessage{FontTtl: 5, BtmStatus: 0x00, DecodeTime: 10, BackTtl: 4, BtmMsg: userMsg, ResponseTime: 10, VobcLifeNum: 123, BaseBtmVobc: message.BaseBtmVobc{AutoIdFrame: 123}} sendData := msg.Encode() - fmt.Println(hex.EncodeToString(sendData)) + fmt.Println(len(sendData), hex.EncodeToString(sendData)) +} + +func TestFffe2(t *testing.T) { + var testUserBtmMsg = strings.Repeat("00", balise_const.UserTelegramByteLen) + userMsg, _ := hex.DecodeString(testUserBtmMsg) + + msg2 := &message.BtmVobcMsgFree{BtmStatus: 0x00, WorkTemperature: 10, Fun1: uint16(0), Fun2: uint16(0), Fun3: uint16(0), Fun4: uint16(0), + //FreeMsg: freeMsg, RespTime: 20, VobcLifeNum: lifeNum, BaseBtmVobc: message.BaseBtmVobc{AutoIdFrame: autoId}, MsgSerial: newMsg} + FreeMsg: userMsg, RespTime: 20, VobcLifeNum: 2266113, BaseBtmVobc: message.BaseBtmVobc{AutoIdFrame: 123}, MsgSerial: message.GetAutoMessageId()} + sendData := msg2.Encode() + + fmt.Println(len(sendData), hex.EncodeToString(sendData)) } func TestDecode232(t *testing.T) { var dd uint32 = 888888888 fmt.Println(dd) fmt.Println(uint16(dd)) ss := "9287a8000511006464000a000000000000000000000490007f8181b60b10183280003fff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff000a0023438bf4c27929" + ss = "fffe9287d300056f006464000a000000000000000000000490007f8181b60b10183280003fff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff000a00221eedf0be28e9fffd" userMsg, _ := hex.DecodeString(ss) arr, _ := TranslateFromFFFE(userMsg) fmt.Println(arr) @@ -144,6 +161,16 @@ func TestDecode232(t *testing.T) { } +func TestDocode(t *testing.T) { + fmt.Println(time.Microsecond * 200) + ss := "fffe9287d300056f006464000a000000000000000000000490007f8181b60b10183280003fff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff000a00221eedf0be28e9fffd" + cfs, _ := hex.DecodeString(ss) + frameType, dataText, err := message.BtmVobcDecode(cfs) + fmt.Println(frameType) + fmt.Println(hex.EncodeToString(dataText)) + fmt.Println(err) +} + func TestDecode2311(t *testing.T) { var data uint16 // 初始化为0 diff --git a/third_party/btm_vobc/btm_vobc_test.go b/third_party/btm_vobc/btm_vobc_test.go index b7bec67..fdf0d8d 100644 --- a/third_party/btm_vobc/btm_vobc_test.go +++ b/third_party/btm_vobc/btm_vobc_test.go @@ -21,8 +21,8 @@ const ( func TestMsg22(t *testing.T) { msg := &message.TrainPcSimBaseMessage{} - msg.Type = train_pc_sim.SENDER_TRAIN_OUTR_INFO - data := []byte{0, 1} + msg.Type = train_pc_sim.RECIVE_TRAIN_CREATE_REMOVE + data := []byte{1} msg.Data = data code := msg.Encode() hexCode := hex.EncodeToString(code) @@ -55,12 +55,11 @@ func TestDecode(t *testing.T) { } func TestMsg(t *testing.T) { - - index := 1 - for i := 22; i <= 125; i++ { - fmt.Println(index) - index++ - } + now := time.Now().UTC() + sec := now.Unix() + fmt.Println(sec) + fmt.Println(now.UnixMilli()) + fmt.Println(now.UnixMilli() - sec*1000) } func TestBytes(t *testing.T) { @@ -91,7 +90,7 @@ func TestRes(t *testing.T) { ss = "fffee601804f004d006c1d21002a00e2c732ff00f86276339122a80000000000000000000000000000211d6a0000000000000000000049c46daebcf6fffd" ss = "fffee601804f004d00871d21002a00ee97d0fb1c4b2a849122c30000000000000000000000000000211d8500000000000000000000c4e2343e4117fffd" ss = "fffee601804f004d00444a1c002a00473bbfa11d4b3b029122a800000000000000000000000000001c4a42000000000000000000008970492fff00d0fffd" - //ss = "fffee601804f004d00921d21002a00ea014fef63995bff009122ce0000000000000000000000000000211d90000000000000000000007b07310379cefffd" + ss = "fffe9287d4000570006464000a000000000000000000000490007f8181b60b10183280003fff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff000a00221eede6c80a6efffd" cfs, _ := hex.DecodeString(ss) frameType, dataText, _ := message.BtmVobcDecode(cfs) if frameType == message.COMMAND_TYPE { diff --git a/third_party/message/btm_vobc_data.go b/third_party/message/btm_vobc_data.go index 7508e48..ee8bee2 100644 --- a/third_party/message/btm_vobc_data.go +++ b/third_party/message/btm_vobc_data.go @@ -277,7 +277,7 @@ func TranslateToFFFE(src []byte) ([]byte, error) { } // TranslateToFFFE 将数据进行FFFE转义处理(不加头FFFE及尾FFFD)。 -func TranslateToFFFE2(pSrc []byte) []byte { +/*func TranslateToFFFE2(pSrc []byte) []byte { var ( srcPos, tgtPos, pos1, pos2, iii uint16 gap uint16 @@ -348,7 +348,7 @@ func TranslateToFFFE2(pSrc []byte) []byte { // 截取实际使用的部分返回 return pTgt[:tgtPos] } - +*/ func TranslateToFFFE3(pSrc []uint8, SrcLen uint16) ([]byte, uint16) { var ( SrcPos, TgtPos, Pos1, Pos2, iii uint16 @@ -447,14 +447,6 @@ func baseEncode(source []byte) []byte { data := make([]byte, 0) crc32 := crc.CalculateCRC(crc.CRC32, source) newSource := binary.BigEndian.AppendUint32(source, uint32(crc32)) - //fffeData, _ := TranslateToFFFE(newSource) - //fffeData := TranslateToFFFE2(newSource) - //fffeData := FFFEEncode(newSource) - //fffeData, _ := TranslateToFFFE3(newSource, uint16(len(newSource))) - - /*f2 := make([]byte, len(newSource)*2) - lens, _ := TranslateToFFFE4(newSource, f2) - fffeData := f2[:lens]*/ f2 := make([]byte, len(newSource)*2) lens := aa(newSource, f2) @@ -464,7 +456,8 @@ func baseEncode(source []byte) []byte { data = append(data, PACKAGE_END...) return data } -func TranslateToFFFE4(pSrc []byte, pTgt []byte) (int, error) { + +/*func TranslateToFFFE4(pSrc []byte, pTgt []byte) (int, error) { if pSrc == nil || pTgt == nil { return 0, fmt.Errorf("pSrc or pTgt is nil") } @@ -524,7 +517,7 @@ func TranslateToFFFE4(pSrc []byte, pTgt []byte) (int, error) { return tgtLen, nil } - +*/ // ID 命令帧的正文 type BtmVobcIdCommand struct { BaseBtmVobc @@ -634,7 +627,8 @@ const ( btm_status_warn = 0x04 ) -var mesage_yuliu = []byte{0, 0, 0, 0, 0, 0, 0, 0, 0} +var mesage_yuliu = []byte{0, 0, 0, 0, 0, 0, 0, 0, 0} //11~19 预留 +var yuliu3 = []byte{0, 0, 0, 0, 0, 0} //8~13 保留 const ( BTM_STSTUS_NORMAL = 0x00 @@ -681,8 +675,6 @@ func (b *BtmVobcMessage) Encode() []byte { return baseEncode(buf.Bytes()) } -var yuliu3 = []byte{0, 0, 0, 0, 0, 0} - type BtmVobcMsgFree struct { BaseBtmVobc //yuliu1 byte diff --git a/third_party/message/train_pc_sim_message.go b/third_party/message/train_pc_sim_message.go index 1f11181..8e9daf5 100644 --- a/third_party/message/train_pc_sim_message.go +++ b/third_party/message/train_pc_sim_message.go @@ -26,12 +26,14 @@ func (tp *TrainPcSimBaseMessage) Encode() []byte { 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 = append(pack, 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(0)) pack = binary.BigEndian.AppendUint16(pack, uint16(crc.CalculateCRC(crc.CRC16, pack[1:]))) return pack } @@ -48,13 +50,13 @@ func (tp *TrainPcSimBaseMessage) Decode(data []byte) error { } var pcType byte pcType, _ = buf.ReadByte() - dataLen, _ := buf.ReadByte() - if buf.Len() > int(dataLen)-1-1-1 { + dataLen = dataLen - 3 + if buf.Len() < int(dataLen) { return fmt.Errorf("") } - var dd = make([]byte, dataLen) + var dd = make([]byte, dataLen-2) binary.Read(buf, binary.BigEndian, &dd) var crcCode uint16 @@ -127,7 +129,7 @@ func (tp *TrainSpeedPlaceReportMsg) Decode(d []byte) { binary.Read(buf, binary.BigEndian, &c2) var ts uint32 binary.Read(buf, binary.BigEndian, &ts) - fmt.Println(runDir, s1, s2, c1, c2, ts) + fmt.Println(fmt.Sprintf("列车方向:%v,1路脉冲数:%v,2路脉冲数:%v,1路累计数:%v,2路累计数:%v,时间:%v", runDir, s1, s2, c1, c2, ts)) } diff --git a/third_party/train_pc_sim/train_pc_sim.go b/third_party/train_pc_sim/train_pc_sim.go index 06842b7..2002690 100644 --- a/third_party/train_pc_sim/train_pc_sim.go +++ b/third_party/train_pc_sim/train_pc_sim.go @@ -251,7 +251,7 @@ func (d *trainPcSimService) initConn(clientKey string) { client2, err := tcp.StartTcpClient(addr, rd.receiverDataHandle, d.readError) if err != nil { - slog.Error("车载pc连接失败 clientKey:", clientKey, "error:", err.Error()) + slog.Error(fmt.Sprintf("车载pc连接失败 clientKey:%v,error:%v", clientKey, err.Error())) d.updateState(tpapi.ThirdPartyState_Broken) } else { rd.tcpClient = client2 @@ -302,8 +302,9 @@ func (d *trainPcSimService) Start(pcSimManage TrainPcSimManage) { //FireTrainControlEventType.Subscribe(wd, d.trainControlEventHandle) d.updateState(tpapi.ThirdPartyState_Normal) - //go d.sendTrainLocationAndSpeedTask(ctx) + go d.sendTrainLocationAndSpeedTask(ctx) } + func (d *trainPcSimService) Stop() { d.updateState(tpapi.ThirdPartyState_Closed) if d.cancleContext != nil { @@ -419,11 +420,11 @@ func (d *trainPcSimService) SendDriverActive(train *state_proto.TrainState) { //{,"msg":"发送驾驶激活列车","1":"数据","!BADKEY":"eb0004002437"} da := msg.Encode() - slog.Info("发送驾驶激活列车", train.Id, "数据", hex.EncodeToString(da)) + slog.Info(fmt.Sprintf("发送驾驶激活列车id:%v,数据:%v", train.Id, hex.EncodeToString(da))) err := rd.tcpClient.Send(da) //err := client.Send(da) if err != nil { - slog.Error("发送驾驶激活列车", train.Id, "数据", hex.EncodeToString(da), err) + slog.Error(fmt.Sprintf("发送驾驶激活失败列车id:%v,数据:%v,err:%v", train.Id, hex.EncodeToString(da), err.Error())) } } func (d *trainPcSimService) SendHandleSwitch(oldTraction, oldBrakeForce int64, tractionState bool, train *state_proto.TrainState) { @@ -487,11 +488,11 @@ func (d *trainPcSimService) SendTrainDirection(train *state_proto.TrainState, tr //client := d.pcSimClientMap[clientKey] for _, msg := range baseMsgs { da := msg.Encode() - slog.Info("发送列车方向列车", train.Id, "数据", hex.EncodeToString(da)) + slog.Info(fmt.Sprintf("发送列车方向列车:%v ,数据:%v", train.Id, hex.EncodeToString(da))) err := rd.tcpClient.Send(da) //err := client.Send(da) if err != nil { - slog.Error("发送列车方向失败列车", train.Id, "数据", hex.EncodeToString(da)) + slog.Error(fmt.Sprintf("发送列车方向失败列车:%v ,数据:%v,err:%v", train.Id, hex.EncodeToString(da), err.Error())) } } } @@ -505,11 +506,11 @@ func (d *trainPcSimService) SendBaliseData(train *state_proto.TrainState, msgTyp //client := d.pcSimClientMap[clientKey] da := msg.Encode() - slog.Info("发送列车PC仿真应答器信息,数据", hex.EncodeToString(da)) + slog.Info(fmt.Sprintf("发送列车PC仿真应答器信息,数据:%v", hex.EncodeToString(da))) err := rd.tcpClient.Send(da) if err != nil { - slog.Info("发送列车PC仿真应答器信息失败,数据", hex.EncodeToString(da)) + slog.Info(fmt.Sprintf("发送列车PC仿真应答器信息失败,数据:%v", hex.EncodeToString(da))) } }