【修改联锁采集结构体信息】

This commit is contained in:
weizhihong 2023-11-06 15:20:45 +08:00
parent 479538f42b
commit 41f55ba9eb
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)
if collectInfoState != nil {
serialNumber++
collectInfoState.Header.SerialNumber = serialNumber
collectInfoState.SetSerialNumber(serialNumber)
i.sendCollectUdpClient.SendMsg(collectInfoState)
}
time.Sleep(time.Millisecond * InterlockMessageSendInterval)

View File

@ -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
}

View File

@ -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
}