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.
ntool-biz/nmodbus/protocol/packet_tcp.go

69 lines
1.2 KiB
Go

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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