tcp

package
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 21, 2021 License: BSD-3-Clause Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func TCPClient

func TCPClient(address string) modbus.Client

TCPClient creates TCP client with default handler and given connect string.

Types

type TCPClientHandler

type TCPClientHandler struct {
	// contains filtered or unexported fields
}

TCPClientHandler implements Packager and Transporter interface.

func NewTCPClientHandler

func NewTCPClientHandler(address string) *TCPClientHandler

NewTCPClientHandler allocates a new TCPClientHandler.

func (*TCPClientHandler) Close

func (mb *TCPClientHandler) Close() error

Close closes current connection.

func (*TCPClientHandler) Connect

func (mb *TCPClientHandler) Connect() error

Connect establishes a new connection to the address in Address. Connect and Close are exported so that multiple requests can be done with one session

func (*TCPClientHandler) Decode

func (mb *TCPClientHandler) Decode(adu []byte) (pdu *modbus.ProtocolDataUnit, err error)

Decode extracts PDU from TCP frame:

Transaction identifier: 2 bytes
Protocol identifier: 2 bytes
Length: 2 bytes
Unit identifier: 1 byte

func (*TCPClientHandler) Encode

func (mb *TCPClientHandler) Encode(pdu *modbus.ProtocolDataUnit) (adu []byte, err error)

Encode adds modbus application protocol header:

Transaction identifier: 2 bytes
Protocol identifier: 2 bytes
Length: 2 bytes
Unit identifier: 1 byte
Function code: 1 byte
Data: n bytes

func (*TCPClientHandler) Received

func (mb *TCPClientHandler) Received(handler func(data []byte))

处理服务端响应

func (*TCPClientHandler) Send

func (mb *TCPClientHandler) Send(aduRequest []byte) (aduResponse []byte, err error)

Send sends data to server and ensures response length is greater than header length.

func (*TCPClientHandler) Verify

func (mb *TCPClientHandler) Verify(aduRequest []byte, aduResponse []byte) (err error)

Verify confirms transaction, protocol and unit id.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL