rtu

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: 12 Imported by: 0

README

modbus/rtu

Go Report Card Go Reference

The modbus/rtu package is a library for using Modbus RTU protocol in Go. Currently, it supports only the Modbus Master role for controlling Modbus Slave/Device. This allows you to build software that communicates with Modbus-enabled devices via a serial port (RS-232/RS-485).

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

Documentation

Index

Constants

View Source
const (
	NoParity   = Parity(serial.NoParity)
	OddParity  = Parity(serial.OddParity)
	EvenParity = Parity(serial.EvenParity)
)
View Source
const (
	SERIAL_TIMEOUT = 30 * time.Millisecond
	SERIAL_WAIT    = 30 * time.Millisecond
	BAUDRATE       = 9600
)
View Source
const (
	TIMEOUT = time.Second
)

Variables

View Source
var (
	ErrorLogFunc func(string, ...interface{})
	InfoLogFunc  func(string, ...interface{})
	DebugLogFunc func(string, ...interface{})
)
View Source
var ErrTimeout = errors.New("timeout")

Functions

func SetChecksum

func SetChecksum(b []byte)

Types

type BadRxErr

type BadRxErr []byte

func (BadRxErr) Error

func (e BadRxErr) Error() string

type Cmd

type Cmd interface {
	TxBytes() []byte
	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 Controller

type Controller struct {
	Port    PortOpener
	Timeout time.Duration
	// contains filtered or unexported fields
}

func (*Controller) Close

func (c *Controller) Close()

func (*Controller) Send

func (c *Controller) Send(cmd Cmd) 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 OpenErr

type OpenErr struct {
	Dev string
	Err error
}

func (OpenErr) Error

func (e OpenErr) Error() string

func (OpenErr) Unwrap

func (e OpenErr) Unwrap() error

type Parity

type Parity serial.Parity

func (Parity) IsValid

func (p Parity) IsValid() bool

func (Parity) MarshalText

func (p Parity) MarshalText() ([]byte, error)

func (Parity) String

func (p Parity) String() string

func (*Parity) UnmarshalText

func (p *Parity) UnmarshalText(b []byte) error

type PortOpener

type PortOpener interface {
	Open(bool) (io.ReadWriteCloser, time.Duration, error)
}

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

func (c *ReadCoilsCmd) String() string

func (*ReadCoilsCmd) Tx

func (c *ReadCoilsCmd) Tx() string

func (*ReadCoilsCmd) TxBytes

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

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

func (c *ReadDInputsCmd) String() string

func (*ReadDInputsCmd) Tx

func (c *ReadDInputsCmd) Tx() string

func (*ReadDInputsCmd) TxBytes

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

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

func (c *ReadHRegsCmd) String() string

func (*ReadHRegsCmd) Tx

func (c *ReadHRegsCmd) Tx() string

func (*ReadHRegsCmd) TxBytes

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

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

func (c *ReadIRegsCmd) String() string

func (*ReadIRegsCmd) Tx

func (c *ReadIRegsCmd) Tx() string

func (*ReadIRegsCmd) TxBytes

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

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 SerialPort

type SerialPort struct {
	Dev      string
	Timeout  time.Duration
	Wait     time.Duration
	Baudrate int
	Parity   Parity
}

func (*SerialPort) Open

func (p *SerialPort) Open(
	repeat bool,
) (io.ReadWriteCloser, time.Duration, error)

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

func (c *WriteCoilCmd) String() string

func (*WriteCoilCmd) Tx

func (c *WriteCoilCmd) Tx() string

func (*WriteCoilCmd) TxBytes

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

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

func (c *WriteCoilsCmd) SetCoils(coils []bool)

func (*WriteCoilsCmd) SetDevAddr

func (c *WriteCoilsCmd) SetDevAddr(x byte)

func (*WriteCoilsCmd) String

func (c *WriteCoilsCmd) String() string

func (*WriteCoilsCmd) Tx

func (c *WriteCoilsCmd) Tx() string

func (*WriteCoilsCmd) TxBytes

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

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

func (c *WriteRegCmd) String() string

func (*WriteRegCmd) Tx

func (c *WriteRegCmd) Tx() string

func (*WriteRegCmd) TxBytes

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

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

func (c *WriteRegsCmd) String() string

func (*WriteRegsCmd) Tx

func (c *WriteRegsCmd) Tx() string

func (*WriteRegsCmd) TxBytes

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

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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