136 lines
2.7 KiB
Go
136 lines
2.7 KiB
Go
package message
|
||
|
||
import "fmt"
|
||
|
||
// SectionCmdMsg CI系统发送的区段操作命令
|
||
type SectionCmdMsg struct {
|
||
//bit5 计轴直接复位
|
||
Drst bool
|
||
//bit2 计轴预复位
|
||
Pdrst bool
|
||
}
|
||
|
||
func (s *SectionCmdMsg) Encode() byte {
|
||
buf := 0x00
|
||
if s.Drst {
|
||
buf = buf | (0x01 << 5)
|
||
}
|
||
if s.Pdrst {
|
||
buf = buf | (0x01 << 2)
|
||
}
|
||
return byte(buf)
|
||
}
|
||
func (s *SectionCmdMsg) Decode(buf byte) {
|
||
s.Drst = buf&(0x01<<5) != 0
|
||
s.Pdrst = buf&(0x01<<2) != 0
|
||
}
|
||
|
||
// SectionCmdMsgPack CI系统发送区段操作命令数据包
|
||
type SectionCmdMsgPack struct {
|
||
//检查字节,固定0x80
|
||
Ck byte
|
||
//区段命令表,注意顺序要与协商一致
|
||
Scs []*SectionCmdMsg
|
||
}
|
||
|
||
func (s *SectionCmdMsgPack) Encode() []byte {
|
||
buf := make([]byte, 0, 1+len(s.Scs))
|
||
buf = append(buf, s.Ck)
|
||
for _, msg := range s.Scs {
|
||
buf = append(buf, msg.Encode())
|
||
}
|
||
return buf
|
||
}
|
||
func (s *SectionCmdMsgPack) Decode(buf []byte) error {
|
||
if len(buf) < 1 {
|
||
return fmt.Errorf("buf 没有足够的数据")
|
||
}
|
||
s.Ck = buf[0]
|
||
var scs []*SectionCmdMsg
|
||
for i := 1; i < len(buf); i++ {
|
||
msg := &SectionCmdMsg{}
|
||
msg.Decode(buf[i])
|
||
scs = append(scs, msg)
|
||
}
|
||
s.Scs = scs
|
||
return nil
|
||
}
|
||
|
||
///////////////////////////////////////////////
|
||
|
||
// SectionStatusMsg 发送给CI系统的区段当前的状态
|
||
type SectionStatusMsg struct {
|
||
//0-bit7 计轴出清
|
||
Clr bool
|
||
//0-bit6 计轴占用
|
||
Occ bool
|
||
//1-bit6 计轴复位反馈
|
||
Rac bool
|
||
//1-bit5 运营原因拒绝计轴复位
|
||
Rjo bool
|
||
//1-bit4 技术原因拒绝计轴复位
|
||
Rjt bool
|
||
}
|
||
|
||
func (s *SectionStatusMsg) Encode() []byte {
|
||
buf := []byte{0x00, 0x00}
|
||
if s.Clr {
|
||
buf[0] = buf[0] | (0x01 << 7)
|
||
}
|
||
if s.Occ {
|
||
buf[0] = buf[0] | (0x01 << 6)
|
||
}
|
||
//
|
||
if s.Rac {
|
||
buf[1] = buf[1] | (0x01 << 6)
|
||
}
|
||
if s.Rjo {
|
||
buf[1] = buf[1] | (0x01 << 5)
|
||
}
|
||
if s.Rjt {
|
||
buf[1] = buf[1] | (0x01 << 4)
|
||
}
|
||
return buf
|
||
}
|
||
func (s *SectionStatusMsg) Decode(buf []byte) error {
|
||
if len(buf) != 2 {
|
||
return fmt.Errorf("buf 长度须为2")
|
||
}
|
||
s.Clr = buf[0]&(0x01<<7) != 0
|
||
s.Occ = buf[0]&(0x01<<6) != 0
|
||
s.Rac = buf[1]&(0x01<<6) != 0
|
||
s.Rjo = buf[1]&(0x01<<5) != 0
|
||
s.Rjt = buf[1]&(0x01<<4) != 0
|
||
return nil
|
||
}
|
||
|
||
type SectionStatusMsgPack struct {
|
||
//检查字节,CI系统暂时不使用
|
||
Ck byte
|
||
//区段状态表,注意顺序要与协商一致
|
||
Sms []*SectionStatusMsg
|
||
}
|
||
|
||
func (s *SectionStatusMsgPack) Encode() []byte {
|
||
buf := make([]byte, 0, 1+2*len(s.Sms))
|
||
buf = append(buf, s.Ck)
|
||
for _, msg := range s.Sms {
|
||
buf = append(buf, msg.Encode()...)
|
||
}
|
||
return buf
|
||
}
|
||
func (s *SectionStatusMsgPack) Decode(buf []byte) error {
|
||
if len(buf)%2 != 1 {
|
||
return fmt.Errorf("buf的长度须为奇数")
|
||
}
|
||
s.Ck = buf[0]
|
||
var sms []*SectionStatusMsg
|
||
for i := 1; i < len(buf)-1; i += 2 {
|
||
msg := &SectionStatusMsg{}
|
||
msg.Decode(buf[i : i+2])
|
||
sms = append(sms, msg)
|
||
}
|
||
s.Sms = sms
|
||
return nil
|
||
}
|