日志调整及现场测试

This commit is contained in:
tiger_zhou 2024-06-18 17:10:21 +08:00
parent 2a380347c6
commit 6211f61ecd
8 changed files with 207 additions and 59 deletions

View File

@ -1,12 +1,15 @@
package main package main
import ( import (
"encoding/binary"
"encoding/hex" "encoding/hex"
"fmt" "fmt"
"github.com/spf13/viper" "github.com/spf13/viper"
"joylink.club/bj-rtsts-server/bin/config" "joylink.club/bj-rtsts-server/bin/config"
"joylink.club/bj-rtsts-server/const/balise_const" "joylink.club/bj-rtsts-server/const/balise_const"
"joylink.club/bj-rtsts-server/third_party/message" "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" "joylink.club/bj-rtsts-server/third_party/udp"
"log/slog" "log/slog"
"strconv" "strconv"
@ -22,8 +25,8 @@ func initConfig() {
cnf := viper.New() cnf := viper.New()
cnf.SetConfigName(config_name) cnf.SetConfigName(config_name)
cnf.SetConfigType("yml") cnf.SetConfigType("yml")
cnf.AddConfigPath("./bin/config/") //cnf.AddConfigPath("./bin/config/")
//cnf.AddConfigPath("./config/") //cnf.AddConfigPath("./")
cnf.AddConfigPath(".") cnf.AddConfigPath(".")
err := cnf.ReadInConfig() err := cnf.ReadInConfig()
if err != nil { if err != nil {
@ -57,12 +60,94 @@ func initSpeedTest() {
speedClient = udp.NewClient(fmt.Sprintf("%v:%v", exampleConfig.Speed.RemoteIp, exampleConfig.Speed.RemotePort)) speedClient = udp.NewClient(fmt.Sprintf("%v:%v", exampleConfig.Speed.RemoteIp, exampleConfig.Speed.RemotePort))
go testSpeed(speedClient, 30) 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() { func main() {
initConfig() initConfig()
initBtmTest() //initBtmTest()
//initTrainPc()
//initAccTest() //initAccTest()
//initSpeedTest() initSpeedTest()
for { for {
} }
@ -74,11 +159,15 @@ var freeBtmMsg = strings.Repeat("00", balise_const.UserTelegramByteLen)
func sendPacket(lifeNum uint32, autoId byte) { func sendPacket(lifeNum uint32, autoId byte) {
userMsg, _ := hex.DecodeString(testUserBtmMsg) 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()} VobcLifeNum: lifeNum, BaseBtmVobc: message.BaseBtmVobc{AutoIdFrame: autoId}, MsgSerial: message.GetAutoMessageId()}
sendData := msg.Encode() sendData := msg.Encode()
fmt.Println("发送btm vobc len:", len(sendData), "报文:", hex.EncodeToString(sendData), "报文序列号:", msg.MsgSerial) 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) /*freeMsg, _ := hex.DecodeString(freeBtmMsg)
@ -96,16 +185,22 @@ func sendPacketFree(lifeNum uint32, autoId byte, msgs byte) {
newMsg = 1 newMsg = 1
} }
msg2 := &message.BtmVobcMsgFree{BtmStatus: 0x00, WorkTemperature: 10, Fun1: uint16(0), Fun2: uint16(0), Fun3: uint16(0), Fun4: uint16(0), 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() sendData2 := msg2.Encode()
fmt.Println("发送btm vobc 空报文:", hex.EncodeToString(sendData2), "len:", len(sendData2), "报文序列号:", msg2.MsgSerial, "atoId=", autoId, "报文序列号:", msg2.MsgSerial) 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) { 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)) //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 { 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 { } else if req.FrameStatus == message.REQ_FRAME_STATUS_OK {
//帧正确,删除之前发送的数据 //帧正确,删除之前发送的数据
fmt.Println("11111111111111111111") fmt.Println("11111111111111111111")
@ -115,8 +210,13 @@ func RequestFramePackets(req *message.BtmVobcReq) {
fmt.Println("22222222222222222") fmt.Println("22222222222222222")
} }
} }
var btmReceiveTime = time.Now().UnixMilli()
var vobcNumLife uint32 = 0
func handleBtmVobcFrames(cfs []byte) { 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) frameType, dataText, err := message.BtmVobcDecode(cfs)
if err != nil { if err != nil {
return return
@ -124,11 +224,28 @@ func handleBtmVobcFrames(cfs []byte) {
if frameType == message.COMMAND_TYPE { if frameType == message.COMMAND_TYPE {
idCommand := &message.BtmVobcIdCommand{} idCommand := &message.BtmVobcIdCommand{}
idCommand.Decode(dataText) 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 { } else if frameType == message.REQUEST_TYPE {
if vobcNumLife <= 0 {
return
} else {
fmt.Println(fmt.Sprintf("准备发送vobcLife:%v", vobcNumLife))
}
req := &message.BtmVobcReq{} req := &message.BtmVobcReq{}
req.Decode(dataText) 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 { } else {
slog.Error(fmt.Sprintf("btm vobc 解析未知命令帧类型:0x%v,原始数据:%v,长度:%v", strconv.FormatInt(int64(frameType), 16), hex.EncodeToString(cfs), len(cfs))) slog.Error(fmt.Sprintf("btm vobc 解析未知命令帧类型:0x%v,原始数据:%v,长度:%v", strconv.FormatInt(int64(frameType), 16), hex.EncodeToString(cfs), len(cfs)))
return return

View File

@ -9,3 +9,7 @@ btm:
remotePort: 4196 remotePort: 4196
localIp: "192.168.1.150" localIp: "192.168.1.150"
localPort: 4646 localPort: 4646
trainpc:
remoteIp: "192.168.3.211"
remotePort: 10007

View File

@ -4,6 +4,7 @@ type ExampleConfig struct {
Acc acc Acc acc
Speed speed Speed speed
Btm btm Btm btm
Trainpc trainpc
} }
type acc struct { type acc struct {
RemoteIp string RemoteIp string
@ -19,3 +20,8 @@ type btm struct {
LocalIp string LocalIp string
LocalPort int LocalPort int
} }
type trainpc struct {
RemoteIp string
RemotePort int
}

View File

@ -3,8 +3,11 @@ package btm_vobc
import ( import (
"encoding/hex" "encoding/hex"
"fmt" "fmt"
"joylink.club/bj-rtsts-server/const/balise_const"
"joylink.club/bj-rtsts-server/third_party/message" "joylink.club/bj-rtsts-server/third_party/message"
"strings"
"testing" "testing"
"time"
) )
const ( const (
@ -106,6 +109,7 @@ func TranslateFromFFFE(pSrc []byte) ([]byte, uint16) {
return pTgt[:tgtPos], tgtPos return pTgt[:tgtPos], tgtPos
} }
func TestTss(t *testing.T) { func TestTss(t *testing.T) {
ss := "fffee601804f004d00921d21002a00ea014fef63995bff009122ce0000000000000000000000000000211d90000000000000000000007b07310379cefffd" ss := "fffee601804f004d00921d21002a00ea014fef63995bff009122ce0000000000000000000000000000211d90000000000000000000007b07310379cefffd"
ss = "fffee601804f004d00444a1c002a00473bbfa11d4b3b029122a800000000000000000000000000001c4a42000000000000000000008970492fff00d0fffd" 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, msg := &message.BtmVobcMessage{FontTtl: 5, BtmStatus: 0x00, DecodeTime: 10, BackTtl: 4, BtmMsg: userMsg, ResponseTime: 10,
VobcLifeNum: 123, BaseBtmVobc: message.BaseBtmVobc{AutoIdFrame: 123}} VobcLifeNum: 123, BaseBtmVobc: message.BaseBtmVobc{AutoIdFrame: 123}}
sendData := msg.Encode() 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) { func TestDecode232(t *testing.T) {
var dd uint32 = 888888888 var dd uint32 = 888888888
fmt.Println(dd) fmt.Println(dd)
fmt.Println(uint16(dd)) fmt.Println(uint16(dd))
ss := "9287a8000511006464000a000000000000000000000490007f8181b60b10183280003fff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff000a0023438bf4c27929" ss := "9287a8000511006464000a000000000000000000000490007f8181b60b10183280003fff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff000a0023438bf4c27929"
ss = "fffe9287d300056f006464000a000000000000000000000490007f8181b60b10183280003fff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff000a00221eedf0be28e9fffd"
userMsg, _ := hex.DecodeString(ss) userMsg, _ := hex.DecodeString(ss)
arr, _ := TranslateFromFFFE(userMsg) arr, _ := TranslateFromFFFE(userMsg)
fmt.Println(arr) 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) { func TestDecode2311(t *testing.T) {
var data uint16 // 初始化为0 var data uint16 // 初始化为0

View File

@ -21,8 +21,8 @@ const (
func TestMsg22(t *testing.T) { func TestMsg22(t *testing.T) {
msg := &message.TrainPcSimBaseMessage{} msg := &message.TrainPcSimBaseMessage{}
msg.Type = train_pc_sim.SENDER_TRAIN_OUTR_INFO msg.Type = train_pc_sim.RECIVE_TRAIN_CREATE_REMOVE
data := []byte{0, 1} data := []byte{1}
msg.Data = data msg.Data = data
code := msg.Encode() code := msg.Encode()
hexCode := hex.EncodeToString(code) hexCode := hex.EncodeToString(code)
@ -55,12 +55,11 @@ func TestDecode(t *testing.T) {
} }
func TestMsg(t *testing.T) { func TestMsg(t *testing.T) {
now := time.Now().UTC()
index := 1 sec := now.Unix()
for i := 22; i <= 125; i++ { fmt.Println(sec)
fmt.Println(index) fmt.Println(now.UnixMilli())
index++ fmt.Println(now.UnixMilli() - sec*1000)
}
} }
func TestBytes(t *testing.T) { func TestBytes(t *testing.T) {
@ -91,7 +90,7 @@ func TestRes(t *testing.T) {
ss = "fffee601804f004d006c1d21002a00e2c732ff00f86276339122a80000000000000000000000000000211d6a0000000000000000000049c46daebcf6fffd" ss = "fffee601804f004d006c1d21002a00e2c732ff00f86276339122a80000000000000000000000000000211d6a0000000000000000000049c46daebcf6fffd"
ss = "fffee601804f004d00871d21002a00ee97d0fb1c4b2a849122c30000000000000000000000000000211d8500000000000000000000c4e2343e4117fffd" ss = "fffee601804f004d00871d21002a00ee97d0fb1c4b2a849122c30000000000000000000000000000211d8500000000000000000000c4e2343e4117fffd"
ss = "fffee601804f004d00444a1c002a00473bbfa11d4b3b029122a800000000000000000000000000001c4a42000000000000000000008970492fff00d0fffd" ss = "fffee601804f004d00444a1c002a00473bbfa11d4b3b029122a800000000000000000000000000001c4a42000000000000000000008970492fff00d0fffd"
//ss = "fffee601804f004d00921d21002a00ea014fef63995bff009122ce0000000000000000000000000000211d90000000000000000000007b07310379cefffd" ss = "fffe9287d4000570006464000a000000000000000000000490007f8181b60b10183280003fff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff000a00221eede6c80a6efffd"
cfs, _ := hex.DecodeString(ss) cfs, _ := hex.DecodeString(ss)
frameType, dataText, _ := message.BtmVobcDecode(cfs) frameType, dataText, _ := message.BtmVobcDecode(cfs)
if frameType == message.COMMAND_TYPE { if frameType == message.COMMAND_TYPE {

View File

@ -277,7 +277,7 @@ func TranslateToFFFE(src []byte) ([]byte, error) {
} }
// TranslateToFFFE 将数据进行FFFE转义处理不加头FFFE及尾FFFD // TranslateToFFFE 将数据进行FFFE转义处理不加头FFFE及尾FFFD
func TranslateToFFFE2(pSrc []byte) []byte { /*func TranslateToFFFE2(pSrc []byte) []byte {
var ( var (
srcPos, tgtPos, pos1, pos2, iii uint16 srcPos, tgtPos, pos1, pos2, iii uint16
gap uint16 gap uint16
@ -348,7 +348,7 @@ func TranslateToFFFE2(pSrc []byte) []byte {
// 截取实际使用的部分返回 // 截取实际使用的部分返回
return pTgt[:tgtPos] return pTgt[:tgtPos]
} }
*/
func TranslateToFFFE3(pSrc []uint8, SrcLen uint16) ([]byte, uint16) { func TranslateToFFFE3(pSrc []uint8, SrcLen uint16) ([]byte, uint16) {
var ( var (
SrcPos, TgtPos, Pos1, Pos2, iii uint16 SrcPos, TgtPos, Pos1, Pos2, iii uint16
@ -447,14 +447,6 @@ func baseEncode(source []byte) []byte {
data := make([]byte, 0) data := make([]byte, 0)
crc32 := crc.CalculateCRC(crc.CRC32, source) crc32 := crc.CalculateCRC(crc.CRC32, source)
newSource := binary.BigEndian.AppendUint32(source, uint32(crc32)) 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) f2 := make([]byte, len(newSource)*2)
lens := aa(newSource, f2) lens := aa(newSource, f2)
@ -464,7 +456,8 @@ func baseEncode(source []byte) []byte {
data = append(data, PACKAGE_END...) data = append(data, PACKAGE_END...)
return data return data
} }
func TranslateToFFFE4(pSrc []byte, pTgt []byte) (int, error) {
/*func TranslateToFFFE4(pSrc []byte, pTgt []byte) (int, error) {
if pSrc == nil || pTgt == nil { if pSrc == nil || pTgt == nil {
return 0, fmt.Errorf("pSrc or pTgt is 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 return tgtLen, nil
} }
*/
// ID 命令帧的正文 // ID 命令帧的正文
type BtmVobcIdCommand struct { type BtmVobcIdCommand struct {
BaseBtmVobc BaseBtmVobc
@ -634,7 +627,8 @@ const (
btm_status_warn = 0x04 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 ( const (
BTM_STSTUS_NORMAL = 0x00 BTM_STSTUS_NORMAL = 0x00
@ -681,8 +675,6 @@ func (b *BtmVobcMessage) Encode() []byte {
return baseEncode(buf.Bytes()) return baseEncode(buf.Bytes())
} }
var yuliu3 = []byte{0, 0, 0, 0, 0, 0}
type BtmVobcMsgFree struct { type BtmVobcMsgFree struct {
BaseBtmVobc BaseBtmVobc
//yuliu1 byte //yuliu1 byte

View File

@ -26,12 +26,14 @@ func (tp *TrainPcSimBaseMessage) Encode() []byte {
pack = append(pack, byte(len(tp.Data)+1+1+1+2)) pack = append(pack, byte(len(tp.Data)+1+1+1+2))
if len(tp.Data) > 0 { if len(tp.Data) > 0 {
dataBufs := bytes.NewBuffer(nil) pack = append(pack, tp.Data...)
binary.Write(dataBufs, binary.BigEndian, tp.Data) //dataBufs := bytes.NewBuffer(nil)
data := dataBufs.Bytes() //binary.Write(dataBufs, binary.BigEndian, tp.Data)
pack = append(pack, 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:]))) pack = binary.BigEndian.AppendUint16(pack, uint16(crc.CalculateCRC(crc.CRC16, pack[1:])))
return pack return pack
} }
@ -48,13 +50,13 @@ func (tp *TrainPcSimBaseMessage) Decode(data []byte) error {
} }
var pcType byte var pcType byte
pcType, _ = buf.ReadByte() pcType, _ = buf.ReadByte()
dataLen, _ := buf.ReadByte() dataLen, _ := buf.ReadByte()
if buf.Len() > int(dataLen)-1-1-1 { dataLen = dataLen - 3
if buf.Len() < int(dataLen) {
return fmt.Errorf("") return fmt.Errorf("")
} }
var dd = make([]byte, dataLen) var dd = make([]byte, dataLen-2)
binary.Read(buf, binary.BigEndian, &dd) binary.Read(buf, binary.BigEndian, &dd)
var crcCode uint16 var crcCode uint16
@ -127,7 +129,7 @@ func (tp *TrainSpeedPlaceReportMsg) Decode(d []byte) {
binary.Read(buf, binary.BigEndian, &c2) binary.Read(buf, binary.BigEndian, &c2)
var ts uint32 var ts uint32
binary.Read(buf, binary.BigEndian, &ts) 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))
} }

View File

@ -251,7 +251,7 @@ func (d *trainPcSimService) initConn(clientKey string) {
client2, err := tcp.StartTcpClient(addr, rd.receiverDataHandle, d.readError) client2, err := tcp.StartTcpClient(addr, rd.receiverDataHandle, d.readError)
if err != nil { 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) d.updateState(tpapi.ThirdPartyState_Broken)
} else { } else {
rd.tcpClient = client2 rd.tcpClient = client2
@ -302,8 +302,9 @@ func (d *trainPcSimService) Start(pcSimManage TrainPcSimManage) {
//FireTrainControlEventType.Subscribe(wd, d.trainControlEventHandle) //FireTrainControlEventType.Subscribe(wd, d.trainControlEventHandle)
d.updateState(tpapi.ThirdPartyState_Normal) d.updateState(tpapi.ThirdPartyState_Normal)
//go d.sendTrainLocationAndSpeedTask(ctx) go d.sendTrainLocationAndSpeedTask(ctx)
} }
func (d *trainPcSimService) Stop() { func (d *trainPcSimService) Stop() {
d.updateState(tpapi.ThirdPartyState_Closed) d.updateState(tpapi.ThirdPartyState_Closed)
if d.cancleContext != nil { if d.cancleContext != nil {
@ -419,11 +420,11 @@ func (d *trainPcSimService) SendDriverActive(train *state_proto.TrainState) {
//{,"msg":"发送驾驶激活列车","1":"数据","!BADKEY":"eb0004002437"} //{,"msg":"发送驾驶激活列车","1":"数据","!BADKEY":"eb0004002437"}
da := msg.Encode() 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 := rd.tcpClient.Send(da)
//err := client.Send(da) //err := client.Send(da)
if err != nil { 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) { 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] //client := d.pcSimClientMap[clientKey]
for _, msg := range baseMsgs { for _, msg := range baseMsgs {
da := msg.Encode() 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 := rd.tcpClient.Send(da)
//err := client.Send(da) //err := client.Send(da)
if err != nil { 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] //client := d.pcSimClientMap[clientKey]
da := msg.Encode() da := msg.Encode()
slog.Info("发送列车PC仿真应答器信息,数据", hex.EncodeToString(da)) slog.Info(fmt.Sprintf("发送列车PC仿真应答器信息,数据:%v", hex.EncodeToString(da)))
err := rd.tcpClient.Send(da) err := rd.tcpClient.Send(da)
if err != nil { if err != nil {
slog.Info("发送列车PC仿真应答器信息失败,数据", hex.EncodeToString(da)) slog.Info(fmt.Sprintf("发送列车PC仿真应答器信息失败,数据:%v", hex.EncodeToString(da)))
} }
} }