modbus

package module
v0.0.0-...-7c577ff Latest Latest
Warning

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

Go to latest
Published: Sep 13, 2018 License: MPL-2.0 Imports: 9 Imported by: 0

README

logo

Goldfish GoDoc Build Status

Goldfish is a Go package for creating Modbus TCP servers. See example/main.go for an example.

License

Goldfish is licensed under Mozilla Public License © 2017 Advanced Climate Systems.

Documentation

Index

Constants

View Source
const (
	// ReadCoils is Modbus function code 1.
	ReadCoils uint8 = iota + 1

	// ReadDiscreteInputs is Modbus function code 2.
	ReadDiscreteInputs

	// ReadHoldingRegisters is Modbus function code 3.
	ReadHoldingRegisters

	// ReadInputRegisters is Modbus function code 4.
	ReadInputRegisters

	// WriteSingleCoil is Modbus function code 5.
	WriteSingleCoil

	// WriteSingleRegister is Modbus function code 6.
	WriteSingleRegister

	// WriteMultipleCoils is Modbus function code 15.
	WriteMultipleCoils = 15 + 1

	// WriteMultipleRegisters is Modbus function code 16.
	WriteMultipleRegisters
)

Variables

View Source
var (
	// IllegalFunctionError with exception code 1, is returned when the
	// function received is not an allowable action fwith the slave.
	IllegalFunctionError = Error{Code: 1, /* contains filtered or unexported fields */}

	// IllegalAddressError with exception code 2, is returned when the
	// address received is not an allowable address fwith the slave.
	IllegalAddressError = Error{Code: 2, /* contains filtered or unexported fields */}

	// IllegalDataValueError with exception code 3, is returned if the
	// request contains an value that is not allowable fwith the slave.
	IllegalDataValueError = Error{Code: 3, /* contains filtered or unexported fields */}

	// SlaveDeviceFailureError with exception code 4, is returned when
	// the server isn't able to handle the request.
	SlaveDeviceFailureError = Error{Code: 4, /* contains filtered or unexported fields */}

	// AcknowledgeError with exception code 5, is returned when the
	// server has received the request successfully, but needs a long time
	// to process the request.
	AcknowledgeError = Error{Code: 5, /* contains filtered or unexported fields */}

	// SlaveDeviceBusyError with exception 6, is returned when master is
	// busy processing a long-running command.
	SlaveDeviceBusyError = Error{Code: 6, /* contains filtered or unexported fields */}

	// NegativeAcknowledgeError with exception code 7, is returned for an
	// unsuccessful programming request using function code 13 or 14.
	NegativeAcknowledgeError = Error{Code: 7, /* contains filtered or unexported fields */}

	// MemoryParityError with exception code 8 is returned to indicate that
	// the extended file area failed to pass a consistency check. May only
	// returned for requests with function code 20 or 21.
	MemoryParityError = Error{Code: 8, /* contains filtered or unexported fields */}

	// GatewayPathUnavailableError with exception code 10 indicates that
	// the gateway was unable to allocate an internal communication path
	// from the input port to the output port for processing the request.
	GatewayPathUnavailableError = Error{Code: 10, /* contains filtered or unexported fields */}

	// GatewayTargetDeviceFailedToRespondError with exception code 11
	// indicates that the device is not present on the network.
	GatewayTargetDeviceFailedToRespondError = Error{Code: 11, /* contains filtered or unexported fields */}
)

Functions

This section is empty.

Types

type Error

type Error struct {
	// Code contains the Modbus exception code.
	Code uint8
	// contains filtered or unexported fields
}

Error represesents a Modbus protocol error.

func (Error) Error

func (e Error) Error() string

type Handler

type Handler interface {
	ServeModbus(w io.Writer, r Request)
}

A Handler responds to a Modbus request.

type MBAP

type MBAP struct {
	// TransactionID identifies a request/response transaction.
	TransactionID uint16

	// ProtocolID defines the protocol, for Modbus it's always 0.
	ProtocolID uint16

	// Length shows how much bytes are following.
	Length uint16

	// UnitID or slave id identifies a slave.
	UnitID uint8
}

MBAP is the Modbus Application Header. Only Modbus TCP/IP message have an MBAP header. The MBAP header has 4 fields with a total length of 7 bytes.

func (*MBAP) MarshalBinary

func (m *MBAP) MarshalBinary() ([]byte, error)

MarshalBinary marshals a MBAP to it binary form.

func (*MBAP) UnmarshalBinary

func (m *MBAP) UnmarshalBinary(b []byte) error

UnmarshalBinary unmarshals a binary representation of MBAP.

type ReadHandler

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

ReadHandler can be used to respond on Modbus request with function codes 1, 2, 3 and 4.

func NewReadHandler

func NewReadHandler(h ReadHandlerFunc) *ReadHandler

NewReadHandler creates a new ReadHandler.

func (ReadHandler) ServeModbus

func (h ReadHandler) ServeModbus(w io.Writer, req Request)

ServeModbus writes a Modbus response.

type ReadHandlerFunc

type ReadHandlerFunc func(unitID, start, quantity int) ([]Value, error)

ReadHandlerFunc is an adapter to allow the use of ordinary functions as handlers for Modbus read functions.

type Request

type Request struct {
	// Request is a Modbus request.
	MBAP

	FunctionCode uint8
	Data         []byte
}

Request is a Modbus request.

func (*Request) UnmarshalBinary

func (r *Request) UnmarshalBinary(b []byte) error

UnmarshalBinary unmarshals binary representation of Request.

type Response

type Response struct {
	MBAP
	FunctionCode uint8
	Data         []byte
	// contains filtered or unexported fields
}

Response is a Modbus response.

func NewErrorResponse

func NewErrorResponse(r Request, err error) *Response

NewErrorResponse creates a error response.

func NewResponse

func NewResponse(r Request, data []byte) *Response

NewResponse creates a Response for a Request.

func (*Response) MarshalBinary

func (r *Response) MarshalBinary() ([]byte, error)

MarshalBinary marshals a Response to it binary form.

type Server

type Server struct {
	ErrorLog *log.Logger
	// contains filtered or unexported fields
}

Server is a Modbus server listens on a port and responds on incoming Modbus requests.

func NewServer

func NewServer(address string) (*Server, error)

NewServer creates a new server on given address.

func (*Server) Handle

func (s *Server) Handle(functionCode uint8, h Handler)

Handle registers the handler for the given function code.

func (*Server) Listen

func (s *Server) Listen()

Listen start listening for requests.

func (*Server) SetTimeout

func (s *Server) SetTimeout(t time.Duration)

SetTimeout sets the timeout, which is the maximum duraion a request can take.

type Signedness

type Signedness int

Signedness controls the signedness of values for Writehandler's. A value can be unsigned (capabale of representing only non-negative integers) or signed (capable of representing negative integers as well).

const (
	// Unsigned set signedness to unsigned.
	Unsigned Signedness = iota

	// Signed sets signedness to signed.
	Signed
)

type Value

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

Value is a value an integer ranging from range of -32768 through 65535.

func NewValue

func NewValue(v int) (Value, error)

NewValue creates a Value. It returns an error when given value is outside range of -32768 through 65535.

func (*Value) Get

func (v *Value) Get() int

Get returns the value.

func (*Value) MarshalBinary

func (v *Value) MarshalBinary() ([]byte, error)

MarshalBinary marshals a Value into byte slice with length of 2 bytes.

func (*Value) Set

func (v *Value) Set(value int) error

Set sets the value. It returns an error when given value is outside range of -32768 through 65535.

func (*Value) UnmarshalBinary

func (v *Value) UnmarshalBinary(d []byte, s Signedness) error

UnmarshalBinary value from a byte slice.

type WriteHandler

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

WriteHandler can be used to respond on Modbus request with function codes 5 and 6.

func NewWriteHandler

func NewWriteHandler(h WriteHandlerFunc, s Signedness) *WriteHandler

NewWriteHandler creates a new WriteHandler.

func (WriteHandler) ServeModbus

func (h WriteHandler) ServeModbus(w io.Writer, req Request)

ServeModbus handles a Modbus request and returns a response.

type WriteHandlerFunc

type WriteHandlerFunc func(unitID, start int, values []Value) error

WriteHandlerFunc is an adapter to allow the use of ordinary functions as handlers for Modbus write functions.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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