From 41f55ba9eb0a748997f02ffe9425ab311e64078c Mon Sep 17 00:00:00 2001 From: weizhihong Date: Mon, 6 Nov 2023 15:20:45 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E4=BF=AE=E6=94=B9=E8=81=94=E9=94=81?= =?UTF-8?q?=E9=87=87=E9=9B=86=E7=BB=93=E6=9E=84=E4=BD=93=E4=BF=A1=E6=81=AF?= =?UTF-8?q?=E3=80=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- third_party/interlock/interlock.go | 2 +- third_party/message/interlock.go | 92 +++++++++++-------- .../wayside/memory/wayside_simulation.go | 2 +- 3 files changed, 58 insertions(+), 38 deletions(-) diff --git a/third_party/interlock/interlock.go b/third_party/interlock/interlock.go index 0863419..857fda7 100644 --- a/third_party/interlock/interlock.go +++ b/third_party/interlock/interlock.go @@ -99,7 +99,7 @@ func (i *interlockProxy) collectInfoStateTask(ctx context.Context) { collectInfoState := i.manager.CollectInterlockRelayInfo(i.runConfig.Code) if collectInfoState != nil { serialNumber++ - collectInfoState.Header.SerialNumber = serialNumber + collectInfoState.SetSerialNumber(serialNumber) i.sendCollectUdpClient.SendMsg(collectInfoState) } time.Sleep(time.Millisecond * InterlockMessageSendInterval) diff --git a/third_party/message/interlock.go b/third_party/message/interlock.go index a5018b7..305eccf 100644 --- a/third_party/message/interlock.go +++ b/third_party/message/interlock.go @@ -3,74 +3,94 @@ package message import "fmt" // 消息包头解析 -type InterlockMsgPkgHeader struct { - Header1 byte //包头1 1个字节 - Header2 byte //包头2 1个字节 - TypeCode uint8 //类型码 1个字节 0x02 - SerialNumber uint8 //序列号 1个字节 序列号0~255 - Reserve1 byte //预留 1个字节 - Reserve2 byte //预留 1个字节 +type interlockMsgPkgHeader struct { + header1 byte //包头1 1个字节 0xFF + header2 byte //包头2 1个字节 0xFF + typeCode uint8 //类型码 1个字节 0x02 + serialNumber uint8 //序列号 1个字节 序列号0~255 + reserve1 byte //预留 1个字节 + reserve2 byte //预留 1个字节 } -func (h *InterlockMsgPkgHeader) Encode() []byte { - return []byte{h.Header1, h.Header2, h.TypeCode, h.SerialNumber, h.Reserve1, h.Reserve2} +func newInterlockMsgPkgHeader() *interlockMsgPkgHeader { + return &interlockMsgPkgHeader{header1: 0xFF, header2: 0xFF, typeCode: 0x02} } -func (h *InterlockMsgPkgHeader) Decode(buf []byte) error { +func (h *interlockMsgPkgHeader) encode() []byte { + return []byte{h.header1, h.header2, h.typeCode, h.serialNumber, h.reserve1, h.reserve2} +} + +func (h *interlockMsgPkgHeader) decode(buf []byte) error { if len(buf) < 6 { return fmt.Errorf("数据少于6个字节") } - h.Header1 = buf[0] - h.Header2 = buf[1] - h.TypeCode = buf[2] - h.SerialNumber = buf[3] - h.Reserve1 = buf[4] - h.Reserve2 = buf[5] + h.header1 = buf[0] + h.header2 = buf[1] + h.typeCode = buf[2] + h.serialNumber = buf[3] + h.reserve1 = buf[4] + h.reserve2 = buf[5] return nil } // 消息包尾解析 -type InterlockMsgPkgTail struct { - Tail []byte // 包尾2个字节 +type interlockMsgPkgTail struct { + tail []byte // 包尾2个字节 } -func (t *InterlockMsgPkgTail) Encode() []byte { - if len(t.Tail) == 0 { +func newInterlockMsgPkgTail() *interlockMsgPkgTail { + return &interlockMsgPkgTail{tail: make([]byte, 2)} +} + +func (t *interlockMsgPkgTail) encode() []byte { + if len(t.tail) == 0 { return make([]byte, 2) } - return t.Tail + return t.tail } -func (t *InterlockMsgPkgTail) Decode(buf []byte) error { +func (t *interlockMsgPkgTail) decode(buf []byte) error { if len(buf) < 2 { return fmt.Errorf("数据少于2个字节") } - t.Tail = buf[len(buf)-2:] + t.tail = buf[len(buf)-2:] return nil } // 发送给联锁的采集数据 type InterlockSendMsgPkg struct { - Header *InterlockMsgPkgHeader // 包头 - Info []bool // 发给联锁的状态数据 - Tail *InterlockMsgPkgTail // 包尾 + header *interlockMsgPkgHeader // 包头 + info []bool // 发给联锁的状态数据 + tail *interlockMsgPkgTail // 包尾 +} + +func NewInterlockSendMsgPkg(info []bool) *InterlockSendMsgPkg { + return &InterlockSendMsgPkg{ + header: newInterlockMsgPkgHeader(), + info: info, + tail: newInterlockMsgPkgTail(), + } } var boolsToByteArrLen int = 8 +func (m *InterlockSendMsgPkg) SetSerialNumber(serialNumber uint8) { + m.header.serialNumber = serialNumber +} + func (m *InterlockSendMsgPkg) Encode() []byte { var data []byte - data = append(data, m.Header.Encode()...) - for index, length, cycles := 0, len(m.Info), len(m.Info)/boolsToByteArrLen; index < cycles; index++ { + data = append(data, m.header.encode()...) + for index, length, cycles := 0, len(m.info), len(m.info)/boolsToByteArrLen; index < cycles; index++ { startIndex := index * boolsToByteArrLen toByteArr := [8]bool{} for i := 0; i < boolsToByteArrLen && startIndex < length; i++ { startIndex = startIndex + i - toByteArr[i] = m.Info[startIndex+i] + toByteArr[i] = m.info[startIndex+i] } data = append(data, boolsToByte(toByteArr)) } - data = append(data, m.Tail.Encode()...) + data = append(data, m.tail.encode()...) return data } @@ -90,11 +110,11 @@ type InterlockReceiveMsgPkg struct { toagent_len int et_out_num int tcc_output_len int - Header *InterlockMsgPkgHeader // 包头 + header *interlockMsgPkgHeader // 包头 SyncZone []byte // 同步区状态 DriveInfo []bool // 驱动数据 TccInfo []*InterlockResponderMsgPkg // 应答器报文 - Tail *InterlockMsgPkgTail // 包尾 + tail *interlockMsgPkgTail // 包尾 } // 应答器数据格式 @@ -111,8 +131,8 @@ func NewInterlockReceiveMsgPkg(tlen, etLen, tccLen int) *InterlockReceiveMsgPkg toagent_len: tlen, et_out_num: etLen, tcc_output_len: tccLen * 131, - Header: &InterlockMsgPkgHeader{}, - Tail: &InterlockMsgPkgTail{}, + header: newInterlockMsgPkgHeader(), + tail: newInterlockMsgPkgTail(), } } @@ -122,7 +142,7 @@ func (t *InterlockReceiveMsgPkg) Decode(buf []byte) error { } var preIndex, lastIndex int = 0, 6 // 包头 - t.Header.Decode(buf[preIndex:lastIndex]) + t.header.decode(buf[preIndex:lastIndex]) // 同步区状态 preIndex = lastIndex lastIndex = lastIndex + t.toagent_len @@ -136,7 +156,7 @@ func (t *InterlockReceiveMsgPkg) Decode(buf []byte) error { lastIndex = lastIndex + t.tcc_output_len t.TccInfo = parseResponder(buf, preIndex, lastIndex) // 包尾 - t.Tail.Decode(buf) + t.tail.decode(buf) return nil } diff --git a/ts/simulation/wayside/memory/wayside_simulation.go b/ts/simulation/wayside/memory/wayside_simulation.go index c8a9d22..d68644c 100644 --- a/ts/simulation/wayside/memory/wayside_simulation.go +++ b/ts/simulation/wayside/memory/wayside_simulation.go @@ -413,7 +413,7 @@ func (s *VerifySimulation) CollectInterlockRelayInfo(code string) *message.Inter } collectInfo[i] = rs } - return &message.InterlockSendMsgPkg{Info: collectInfo} + return message.NewInterlockSendMsgPkg(collectInfo) } return nil }