This commit is contained in:
xzb 2023-11-06 15:31:13 +08:00
commit 00519db8c7
3 changed files with 58 additions and 38 deletions

View File

@ -99,7 +99,7 @@ func (i *interlockProxy) collectInfoStateTask(ctx context.Context) {
collectInfoState := i.manager.CollectInterlockRelayInfo(i.runConfig.Code) collectInfoState := i.manager.CollectInterlockRelayInfo(i.runConfig.Code)
if collectInfoState != nil { if collectInfoState != nil {
serialNumber++ serialNumber++
collectInfoState.Header.SerialNumber = serialNumber collectInfoState.SetSerialNumber(serialNumber)
i.sendCollectUdpClient.SendMsg(collectInfoState) i.sendCollectUdpClient.SendMsg(collectInfoState)
} }
time.Sleep(time.Millisecond * InterlockMessageSendInterval) time.Sleep(time.Millisecond * InterlockMessageSendInterval)

View File

@ -3,74 +3,94 @@ package message
import "fmt" import "fmt"
// 消息包头解析 // 消息包头解析
type InterlockMsgPkgHeader struct { type interlockMsgPkgHeader struct {
Header1 byte //包头1 1个字节 header1 byte //包头1 1个字节 0xFF
Header2 byte //包头2 1个字节 header2 byte //包头2 1个字节 0xFF
TypeCode uint8 //类型码 1个字节 0x02 typeCode uint8 //类型码 1个字节 0x02
SerialNumber uint8 //序列号 1个字节 序列号0~255 serialNumber uint8 //序列号 1个字节 序列号0~255
Reserve1 byte //预留 1个字节 reserve1 byte //预留 1个字节
Reserve2 byte //预留 1个字节 reserve2 byte //预留 1个字节
} }
func (h *InterlockMsgPkgHeader) Encode() []byte { func newInterlockMsgPkgHeader() *interlockMsgPkgHeader {
return []byte{h.Header1, h.Header2, h.TypeCode, h.SerialNumber, h.Reserve1, h.Reserve2} 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 { if len(buf) < 6 {
return fmt.Errorf("数据少于6个字节") return fmt.Errorf("数据少于6个字节")
} }
h.Header1 = buf[0] h.header1 = buf[0]
h.Header2 = buf[1] h.header2 = buf[1]
h.TypeCode = buf[2] h.typeCode = buf[2]
h.SerialNumber = buf[3] h.serialNumber = buf[3]
h.Reserve1 = buf[4] h.reserve1 = buf[4]
h.Reserve2 = buf[5] h.reserve2 = buf[5]
return nil return nil
} }
// 消息包尾解析 // 消息包尾解析
type InterlockMsgPkgTail struct { type interlockMsgPkgTail struct {
Tail []byte // 包尾2个字节 tail []byte // 包尾2个字节
} }
func (t *InterlockMsgPkgTail) Encode() []byte { func newInterlockMsgPkgTail() *interlockMsgPkgTail {
if len(t.Tail) == 0 { return &interlockMsgPkgTail{tail: make([]byte, 2)}
}
func (t *interlockMsgPkgTail) encode() []byte {
if len(t.tail) == 0 {
return make([]byte, 2) 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 { if len(buf) < 2 {
return fmt.Errorf("数据少于2个字节") return fmt.Errorf("数据少于2个字节")
} }
t.Tail = buf[len(buf)-2:] t.tail = buf[len(buf)-2:]
return nil return nil
} }
// 发送给联锁的采集数据 // 发送给联锁的采集数据
type InterlockSendMsgPkg struct { type InterlockSendMsgPkg struct {
Header *InterlockMsgPkgHeader // 包头 header *interlockMsgPkgHeader // 包头
Info []bool // 发给联锁的状态数据 info []bool // 发给联锁的状态数据
Tail *InterlockMsgPkgTail // 包尾 tail *interlockMsgPkgTail // 包尾
}
func NewInterlockSendMsgPkg(info []bool) *InterlockSendMsgPkg {
return &InterlockSendMsgPkg{
header: newInterlockMsgPkgHeader(),
info: info,
tail: newInterlockMsgPkgTail(),
}
} }
var boolsToByteArrLen int = 8 var boolsToByteArrLen int = 8
func (m *InterlockSendMsgPkg) SetSerialNumber(serialNumber uint8) {
m.header.serialNumber = serialNumber
}
func (m *InterlockSendMsgPkg) Encode() []byte { func (m *InterlockSendMsgPkg) Encode() []byte {
var data []byte var data []byte
data = append(data, m.Header.Encode()...) data = append(data, m.header.encode()...)
for index, length, cycles := 0, len(m.Info), len(m.Info)/boolsToByteArrLen; index < cycles; index++ { for index, length, cycles := 0, len(m.info), len(m.info)/boolsToByteArrLen; index < cycles; index++ {
startIndex := index * boolsToByteArrLen startIndex := index * boolsToByteArrLen
toByteArr := [8]bool{} toByteArr := [8]bool{}
for i := 0; i < boolsToByteArrLen && startIndex < length; i++ { for i := 0; i < boolsToByteArrLen && startIndex < length; i++ {
startIndex = startIndex + i startIndex = startIndex + i
toByteArr[i] = m.Info[startIndex+i] toByteArr[i] = m.info[startIndex+i]
} }
data = append(data, boolsToByte(toByteArr)) data = append(data, boolsToByte(toByteArr))
} }
data = append(data, m.Tail.Encode()...) data = append(data, m.tail.encode()...)
return data return data
} }
@ -90,11 +110,11 @@ type InterlockReceiveMsgPkg struct {
toagent_len int toagent_len int
et_out_num int et_out_num int
tcc_output_len int tcc_output_len int
Header *InterlockMsgPkgHeader // 包头 header *interlockMsgPkgHeader // 包头
SyncZone []byte // 同步区状态 SyncZone []byte // 同步区状态
DriveInfo []bool // 驱动数据 DriveInfo []bool // 驱动数据
TccInfo []*InterlockResponderMsgPkg // 应答器报文 TccInfo []*InterlockResponderMsgPkg // 应答器报文
Tail *InterlockMsgPkgTail // 包尾 tail *interlockMsgPkgTail // 包尾
} }
// 应答器数据格式 // 应答器数据格式
@ -111,8 +131,8 @@ func NewInterlockReceiveMsgPkg(tlen, etLen, tccLen int) *InterlockReceiveMsgPkg
toagent_len: tlen, toagent_len: tlen,
et_out_num: etLen, et_out_num: etLen,
tcc_output_len: tccLen * 131, tcc_output_len: tccLen * 131,
Header: &InterlockMsgPkgHeader{}, header: newInterlockMsgPkgHeader(),
Tail: &InterlockMsgPkgTail{}, tail: newInterlockMsgPkgTail(),
} }
} }
@ -122,7 +142,7 @@ func (t *InterlockReceiveMsgPkg) Decode(buf []byte) error {
} }
var preIndex, lastIndex int = 0, 6 var preIndex, lastIndex int = 0, 6
// 包头 // 包头
t.Header.Decode(buf[preIndex:lastIndex]) t.header.decode(buf[preIndex:lastIndex])
// 同步区状态 // 同步区状态
preIndex = lastIndex preIndex = lastIndex
lastIndex = lastIndex + t.toagent_len lastIndex = lastIndex + t.toagent_len
@ -136,7 +156,7 @@ func (t *InterlockReceiveMsgPkg) Decode(buf []byte) error {
lastIndex = lastIndex + t.tcc_output_len lastIndex = lastIndex + t.tcc_output_len
t.TccInfo = parseResponder(buf, preIndex, lastIndex) t.TccInfo = parseResponder(buf, preIndex, lastIndex)
// 包尾 // 包尾
t.Tail.Decode(buf) t.tail.decode(buf)
return nil return nil
} }

View File

@ -413,7 +413,7 @@ func (s *VerifySimulation) CollectInterlockRelayInfo(code string) *message.Inter
} }
collectInfo[i] = rs collectInfo[i] = rs
} }
return &message.InterlockSendMsgPkg{Info: collectInfo} return message.NewInterlockSendMsgPkg(collectInfo)
} }
return nil return nil
} }