tcp

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 16, 2025 License: MIT Imports: 11 Imported by: 0

README

modbus/tcp

Go Report Card Go Reference

The modbus/tcp package is a library for using the ModBus TCP protocol in Go. Currently, it supports only the Modbus TCP Master/Client role for controlling Modbus Slave/Server. This allows you to build software that communicates with Modbus-enabled devices over TCP/IP.

A simple example program demonstrating how to use the library is provided in tcp-demo.

Documentation

Index

Constants

View Source
const (
	PORT    = 502
	TIMEOUT = 3 * time.Second
	WAIT    = 5 * time.Millisecond
)

Variables

View Source
var (
	ErrorLogFunc func(string, ...interface{})
	InfoLogFunc  func(string, ...interface{})
	DebugLogFunc func(string, ...interface{})
)

Functions

This section is empty.

Types

type BadRxErr

type BadRxErr []byte

func (BadRxErr) Error

func (e BadRxErr) Error() string

type Cmd

type Cmd interface {
	TxBytes() []byte
	TxId() uint16
	SetTxId(uint16)
	DevAddr() byte
	SetDevAddr(byte)
	Addr() uint16
	SetAddr(uint16)
	Tx() string

	RxBytes() *[]byte
	IsValidRx() bool
	Rx() string
	Err() error

	String() string
}

type CmdReq

type CmdReq struct {
	Cmd Cmd
	Err chan<- error
}

func NewCmdReq

func NewCmdReq(cmd Cmd) (CmdReq, <-chan error)

type Conn

type Conn interface {
	io.ReadWriteCloser
	SetReadDeadline(time.Time) error
	SetWriteDeadline(time.Time) error
}

type ConnDialer

type ConnDialer interface {
	Dial(bool) (Conn, time.Duration, time.Duration, uint16, error)
}

type Controller

type Controller struct {
	Dialer ConnDialer
	// contains filtered or unexported fields
}

func (*Controller) Close

func (c *Controller) Close()

func (*Controller) Send

func (c *Controller) Send(cmd Cmd) error

type DialErr

type DialErr struct {
	Addr string
	Err  error
}

func (DialErr) Error

func (e DialErr) Error() string

func (DialErr) Unwrap

func (e DialErr) Unwrap() error

type Dialer

type Dialer struct {
	Host    string
	Port    int
	Timeout time.Duration
	Wait    time.Duration
}

func (*Dialer) Dial

func (p *Dialer) Dial(
	repeat bool,
) (Conn, time.Duration, time.Duration, uint16, error)

type IController

type IController interface {
	Close()
	Send(Cmd) error
}

type ModbusErr

type ModbusErr byte
const (
	IllegalFunction ModbusErr = iota + 1
	IllegalDataAddress
	IllegalDataValue
	SlaveDeviceFail
)

func (ModbusErr) Error

func (e ModbusErr) Error() string

type ReadCoilsCmd

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

func NewReadCoilsCmd

func NewReadCoilsCmd(devAddr byte, addr uint16, count uint16) *ReadCoilsCmd

func (*ReadCoilsCmd) Addr

func (c *ReadCoilsCmd) Addr() uint16

func (*ReadCoilsCmd) Bytes

func (c *ReadCoilsCmd) Bytes() []byte

func (*ReadCoilsCmd) Coil

func (c *ReadCoilsCmd) Coil(i int) bool

func (*ReadCoilsCmd) Count

func (c *ReadCoilsCmd) Count() int

func (*ReadCoilsCmd) DevAddr

func (c *ReadCoilsCmd) DevAddr() byte

func (*ReadCoilsCmd) Err

func (c *ReadCoilsCmd) Err() error

func (*ReadCoilsCmd) IsValidRx

func (c *ReadCoilsCmd) IsValidRx() bool

func (*ReadCoilsCmd) Rx

func (c *ReadCoilsCmd) Rx() string

func (*ReadCoilsCmd) RxBytes

func (c *ReadCoilsCmd) RxBytes() *[]byte

func (*ReadCoilsCmd) SetAddr

func (c *ReadCoilsCmd) SetAddr(x uint16)

func (*ReadCoilsCmd) SetDevAddr

func (c *ReadCoilsCmd) SetDevAddr(x byte)

func (*ReadCoilsCmd) SetTxId

func (c *ReadCoilsCmd) SetTxId(x uint16)

func (*ReadCoilsCmd) String

func (c *ReadCoilsCmd) String() string

func (*ReadCoilsCmd) Tx

func (c *ReadCoilsCmd) Tx() string

func (*ReadCoilsCmd) TxBytes

func (c *ReadCoilsCmd) TxBytes() []byte

func (*ReadCoilsCmd) TxId

func (c *ReadCoilsCmd) TxId() uint16

type ReadDInputsCmd

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

func NewReadDInputsCmd

func NewReadDInputsCmd(
	devAddr byte, addr uint16, count uint16,
) *ReadDInputsCmd

func (*ReadDInputsCmd) Addr

func (c *ReadDInputsCmd) Addr() uint16

func (*ReadDInputsCmd) Bytes

func (c *ReadDInputsCmd) Bytes() []byte

func (*ReadDInputsCmd) Count

func (c *ReadDInputsCmd) Count() int

func (*ReadDInputsCmd) DevAddr

func (c *ReadDInputsCmd) DevAddr() byte

func (*ReadDInputsCmd) Err

func (c *ReadDInputsCmd) Err() error

func (*ReadDInputsCmd) Input

func (c *ReadDInputsCmd) Input(i int) bool

func (*ReadDInputsCmd) IsValidRx

func (c *ReadDInputsCmd) IsValidRx() bool

func (*ReadDInputsCmd) Rx

func (c *ReadDInputsCmd) Rx() string

func (*ReadDInputsCmd) RxBytes

func (c *ReadDInputsCmd) RxBytes() *[]byte

func (*ReadDInputsCmd) SetAddr

func (c *ReadDInputsCmd) SetAddr(x uint16)

func (*ReadDInputsCmd) SetDevAddr

func (c *ReadDInputsCmd) SetDevAddr(x byte)

func (*ReadDInputsCmd) SetTxId

func (c *ReadDInputsCmd) SetTxId(x uint16)

func (*ReadDInputsCmd) String

func (c *ReadDInputsCmd) String() string

func (*ReadDInputsCmd) Tx

func (c *ReadDInputsCmd) Tx() string

func (*ReadDInputsCmd) TxBytes

func (c *ReadDInputsCmd) TxBytes() []byte

func (*ReadDInputsCmd) TxId

func (c *ReadDInputsCmd) TxId() uint16

type ReadHRegsCmd

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

func NewReadHRegsCmd

func NewReadHRegsCmd(devAddr byte, addr uint16, count uint16) *ReadHRegsCmd

func (*ReadHRegsCmd) Addr

func (c *ReadHRegsCmd) Addr() uint16

func (*ReadHRegsCmd) Bytes

func (c *ReadHRegsCmd) Bytes() []byte

func (*ReadHRegsCmd) Count

func (c *ReadHRegsCmd) Count() int

func (*ReadHRegsCmd) DevAddr

func (c *ReadHRegsCmd) DevAddr() byte

func (*ReadHRegsCmd) Err

func (c *ReadHRegsCmd) Err() error

func (*ReadHRegsCmd) IsValidRx

func (c *ReadHRegsCmd) IsValidRx() bool

func (*ReadHRegsCmd) Reg

func (c *ReadHRegsCmd) Reg(i int) uint16

func (*ReadHRegsCmd) Rx

func (c *ReadHRegsCmd) Rx() string

func (*ReadHRegsCmd) RxBytes

func (c *ReadHRegsCmd) RxBytes() *[]byte

func (*ReadHRegsCmd) SetAddr

func (c *ReadHRegsCmd) SetAddr(x uint16)

func (*ReadHRegsCmd) SetDevAddr

func (c *ReadHRegsCmd) SetDevAddr(x byte)

func (*ReadHRegsCmd) SetTxId

func (c *ReadHRegsCmd) SetTxId(x uint16)

func (*ReadHRegsCmd) String

func (c *ReadHRegsCmd) String() string

func (*ReadHRegsCmd) Tx

func (c *ReadHRegsCmd) Tx() string

func (*ReadHRegsCmd) TxBytes

func (c *ReadHRegsCmd) TxBytes() []byte

func (*ReadHRegsCmd) TxId

func (c *ReadHRegsCmd) TxId() uint16

type ReadIRegsCmd

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

func NewReadIRegsCmd

func NewReadIRegsCmd(devAddr byte, addr uint16, count uint16) *ReadIRegsCmd

func (*ReadIRegsCmd) Addr

func (c *ReadIRegsCmd) Addr() uint16

func (*ReadIRegsCmd) Bytes

func (c *ReadIRegsCmd) Bytes() []byte

func (*ReadIRegsCmd) Count

func (c *ReadIRegsCmd) Count() int

func (*ReadIRegsCmd) DevAddr

func (c *ReadIRegsCmd) DevAddr() byte

func (*ReadIRegsCmd) Err

func (c *ReadIRegsCmd) Err() error

func (*ReadIRegsCmd) IsValidRx

func (c *ReadIRegsCmd) IsValidRx() bool

func (*ReadIRegsCmd) Reg

func (c *ReadIRegsCmd) Reg(i int) uint16

func (*ReadIRegsCmd) Rx

func (c *ReadIRegsCmd) Rx() string

func (*ReadIRegsCmd) RxBytes

func (c *ReadIRegsCmd) RxBytes() *[]byte

func (*ReadIRegsCmd) SetAddr

func (c *ReadIRegsCmd) SetAddr(x uint16)

func (*ReadIRegsCmd) SetDevAddr

func (c *ReadIRegsCmd) SetDevAddr(x byte)

func (*ReadIRegsCmd) SetTxId

func (c *ReadIRegsCmd) SetTxId(x uint16)

func (*ReadIRegsCmd) String

func (c *ReadIRegsCmd) String() string

func (*ReadIRegsCmd) Tx

func (c *ReadIRegsCmd) Tx() string

func (*ReadIRegsCmd) TxBytes

func (c *ReadIRegsCmd) TxBytes() []byte

func (*ReadIRegsCmd) TxId

func (c *ReadIRegsCmd) TxId() uint16

type Scanner

type Scanner struct {
	Controller IController
	Subs       []SubScanner
	// contains filtered or unexported fields
}

func (*Scanner) Run

func (s *Scanner) Run(stop <-chan struct{})

type SubScanner

type SubScanner interface {
	Run(stop <-chan struct{}) <-chan CmdReq
}

type WriteCoilCmd

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

func NewWriteCoilCmd

func NewWriteCoilCmd(devAddr byte, addr uint16, val bool) *WriteCoilCmd

func (*WriteCoilCmd) Addr

func (c *WriteCoilCmd) Addr() uint16

func (*WriteCoilCmd) Coil

func (c *WriteCoilCmd) Coil() bool

func (*WriteCoilCmd) DevAddr

func (c *WriteCoilCmd) DevAddr() byte

func (*WriteCoilCmd) Err

func (c *WriteCoilCmd) Err() error

func (*WriteCoilCmd) IsValidRx

func (c *WriteCoilCmd) IsValidRx() bool

func (*WriteCoilCmd) Rx

func (c *WriteCoilCmd) Rx() string

func (*WriteCoilCmd) RxBytes

func (c *WriteCoilCmd) RxBytes() *[]byte

func (*WriteCoilCmd) SetAddr

func (c *WriteCoilCmd) SetAddr(x uint16)

func (*WriteCoilCmd) SetCoil

func (c *WriteCoilCmd) SetCoil(v bool)

func (*WriteCoilCmd) SetDevAddr

func (c *WriteCoilCmd) SetDevAddr(x byte)

func (*WriteCoilCmd) SetTxId

func (c *WriteCoilCmd) SetTxId(x uint16)

func (*WriteCoilCmd) String

func (c *WriteCoilCmd) String() string

func (*WriteCoilCmd) Tx

func (c *WriteCoilCmd) Tx() string

func (*WriteCoilCmd) TxBytes

func (c *WriteCoilCmd) TxBytes() []byte

func (*WriteCoilCmd) TxId

func (c *WriteCoilCmd) TxId() uint16

type WriteCoilsCmd

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

func NewWriteCoilsCmd

func NewWriteCoilsCmd(devAddr byte, addr uint16, values []bool) *WriteCoilsCmd

func (*WriteCoilsCmd) Addr

func (c *WriteCoilsCmd) Addr() uint16

func (*WriteCoilsCmd) Byte

func (c *WriteCoilsCmd) Byte(i int) byte

func (*WriteCoilsCmd) ByteCount

func (c *WriteCoilsCmd) ByteCount() int

func (*WriteCoilsCmd) Bytes

func (c *WriteCoilsCmd) Bytes() []byte

func (*WriteCoilsCmd) Coil

func (c *WriteCoilsCmd) Coil(i int) bool

func (*WriteCoilsCmd) Count

func (c *WriteCoilsCmd) Count() int

func (*WriteCoilsCmd) DevAddr

func (c *WriteCoilsCmd) DevAddr() byte

func (*WriteCoilsCmd) Err

func (c *WriteCoilsCmd) Err() error

func (*WriteCoilsCmd) IsValidRx

func (c *WriteCoilsCmd) IsValidRx() bool

func (*WriteCoilsCmd) ModifyBytes

func (c *WriteCoilsCmd) ModifyBytes(f func(b []byte))

Warning: care must be taken to not set bit outside valid range.

func (*WriteCoilsCmd) Rx

func (c *WriteCoilsCmd) Rx() string

func (*WriteCoilsCmd) RxBytes

func (c *WriteCoilsCmd) RxBytes() *[]byte

func (*WriteCoilsCmd) SetAddr

func (c *WriteCoilsCmd) SetAddr(x uint16)

func (*WriteCoilsCmd) SetByte

func (c *WriteCoilsCmd) SetByte(i int, b byte)

Warning: care must be taken to not set bit outside valid range.

func (*WriteCoilsCmd) SetCoil

func (c *WriteCoilsCmd) SetCoil(i int, v bool)

func (*WriteCoilsCmd) SetDevAddr

func (c *WriteCoilsCmd) SetDevAddr(x byte)

func (*WriteCoilsCmd) SetTxId

func (c *WriteCoilsCmd) SetTxId(x uint16)

func (*WriteCoilsCmd) String

func (c *WriteCoilsCmd) String() string

func (*WriteCoilsCmd) Tx

func (c *WriteCoilsCmd) Tx() string

func (*WriteCoilsCmd) TxBytes

func (c *WriteCoilsCmd) TxBytes() []byte

func (*WriteCoilsCmd) TxId

func (c *WriteCoilsCmd) TxId() uint16

type WriteRegCmd

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

func NewWriteRegCmd

func NewWriteRegCmd(devAddr byte, addr uint16, val uint16) *WriteRegCmd

func (*WriteRegCmd) Addr

func (c *WriteRegCmd) Addr() uint16

func (*WriteRegCmd) DevAddr

func (c *WriteRegCmd) DevAddr() byte

func (*WriteRegCmd) Err

func (c *WriteRegCmd) Err() error

func (*WriteRegCmd) IsValidRx

func (c *WriteRegCmd) IsValidRx() bool

func (*WriteRegCmd) Reg

func (c *WriteRegCmd) Reg() uint16

func (*WriteRegCmd) Rx

func (c *WriteRegCmd) Rx() string

func (*WriteRegCmd) RxBytes

func (c *WriteRegCmd) RxBytes() *[]byte

func (*WriteRegCmd) SetAddr

func (c *WriteRegCmd) SetAddr(x uint16)

func (*WriteRegCmd) SetDevAddr

func (c *WriteRegCmd) SetDevAddr(x byte)

func (*WriteRegCmd) SetReg

func (c *WriteRegCmd) SetReg(v uint16)

func (*WriteRegCmd) SetTxId

func (c *WriteRegCmd) SetTxId(x uint16)

func (*WriteRegCmd) String

func (c *WriteRegCmd) String() string

func (*WriteRegCmd) Tx

func (c *WriteRegCmd) Tx() string

func (*WriteRegCmd) TxBytes

func (c *WriteRegCmd) TxBytes() []byte

func (*WriteRegCmd) TxId

func (c *WriteRegCmd) TxId() uint16

type WriteRegsCmd

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

func NewWriteRegsCmd

func NewWriteRegsCmd(devAddr byte, addr uint16, values []uint16) *WriteRegsCmd

func (*WriteRegsCmd) Addr

func (c *WriteRegsCmd) Addr() uint16

func (*WriteRegsCmd) Byte

func (c *WriteRegsCmd) Byte(i int) byte

func (*WriteRegsCmd) ByteCount

func (c *WriteRegsCmd) ByteCount() int

func (*WriteRegsCmd) Bytes

func (c *WriteRegsCmd) Bytes() []byte

func (*WriteRegsCmd) Count

func (c *WriteRegsCmd) Count() int

func (*WriteRegsCmd) DevAddr

func (c *WriteRegsCmd) DevAddr() byte

func (*WriteRegsCmd) Err

func (c *WriteRegsCmd) Err() error

func (*WriteRegsCmd) IsValidRx

func (c *WriteRegsCmd) IsValidRx() bool

func (*WriteRegsCmd) ModifyBytes

func (c *WriteRegsCmd) ModifyBytes(f func(b []byte))

func (*WriteRegsCmd) Reg

func (c *WriteRegsCmd) Reg(i int) uint16

func (*WriteRegsCmd) Rx

func (c *WriteRegsCmd) Rx() string

func (*WriteRegsCmd) RxBytes

func (c *WriteRegsCmd) RxBytes() *[]byte

func (*WriteRegsCmd) SetAddr

func (c *WriteRegsCmd) SetAddr(x uint16)

func (*WriteRegsCmd) SetByte

func (c *WriteRegsCmd) SetByte(i int, b byte)

func (*WriteRegsCmd) SetDevAddr

func (c *WriteRegsCmd) SetDevAddr(x byte)

func (*WriteRegsCmd) SetReg

func (c *WriteRegsCmd) SetReg(i int, v uint16)

func (*WriteRegsCmd) SetTxId

func (c *WriteRegsCmd) SetTxId(x uint16)

func (*WriteRegsCmd) String

func (c *WriteRegsCmd) String() string

func (*WriteRegsCmd) Tx

func (c *WriteRegsCmd) Tx() string

func (*WriteRegsCmd) TxBytes

func (c *WriteRegsCmd) TxBytes() []byte

func (*WriteRegsCmd) TxId

func (c *WriteRegsCmd) TxId() uint16

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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