You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
|
|
package protocol
|
|
|
|
|
|
|
|
|
|
var _ Packet = (*TCPPacket)(nil)
|
|
|
|
|
|
|
|
|
|
type (
|
|
|
|
|
TCPHeader struct {
|
|
|
|
|
TransactionIdentifier uint16
|
|
|
|
|
ProtocolIdentifier uint16
|
|
|
|
|
Length uint16
|
|
|
|
|
ModbusHeader
|
|
|
|
|
}
|
|
|
|
|
// TCPPacket modbus-tcp 数据帧,相对于rtu添加了协议头,减少了crc
|
|
|
|
|
TCPPacket struct {
|
|
|
|
|
*TCPHeader
|
|
|
|
|
Data []byte
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func NewTCPPacket() *TCPPacket {
|
|
|
|
|
return &TCPPacket{
|
|
|
|
|
TCPHeader: &TCPHeader{
|
|
|
|
|
TransactionIdentifier: 0,
|
|
|
|
|
ProtocolIdentifier: 0,
|
|
|
|
|
Length: 0,
|
|
|
|
|
ModbusHeader: ModbusHeader{},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (p *TCPPacket) Copy() Packet {
|
|
|
|
|
c := *p
|
|
|
|
|
return &c
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (p *TCPPacket) GetHeader() interface{} {
|
|
|
|
|
return p.TCPHeader
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (p *TCPPacket) GetLen() uint64 {
|
|
|
|
|
return uint64(p.Length)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (p *TCPPacket) GetBody() []byte {
|
|
|
|
|
return p.Data
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (p *TCPPacket) GetAddress() uint8 {
|
|
|
|
|
return p.Address
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (p *TCPPacket) GetFunction() uint8 {
|
|
|
|
|
return p.Function
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (p *TCPPacket) SetBody(data []byte) {
|
|
|
|
|
p.Data = data
|
|
|
|
|
p.calcLength()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (p *TCPPacket) SetError(err *MError) {
|
|
|
|
|
p.Function = p.Function | 0x80
|
|
|
|
|
p.Data = []byte{byte(*err)}
|
|
|
|
|
p.calcLength()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (p *TCPPacket) calcLength() {
|
|
|
|
|
p.Length = uint16(len(p.Data) + 2)
|
|
|
|
|
}
|