Documentation
¶
Overview ¶
Package modbus contains modbus RTU/TCP client/server code.
Index ¶
- Constants
- Variables
- func CheckRtuCrc(packet []byte) error
- func DecodeASCIIByte(data []byte) (byte, []byte, error)
- func DecodeASCIIByteEnd(data []byte) (byte, []byte, error)
- func Float32ToRegs(in []float32) []uint16
- func Float32ToRegsSwapWords(in []float32) []uint16
- func Int32ToRegs(in []int32) []uint16
- func Int32ToRegsSwapWords(in []int32) []uint16
- func PutUint16Array(value ...uint16) []byte
- func RegsToFloat32(in []uint16) []float32
- func RegsToFloat32SwapWords(in []uint16) []float32
- func RegsToInt16(in []uint16) []int16
- func RegsToInt32(in []uint16) []int32
- func RegsToInt32SwapWords(in []uint16) []int32
- func RegsToUint32(in []uint16) []uint32
- func RegsToUint32SwapWords(in []uint16) []uint32
- func RtuCrc(buf []byte) uint16
- func Uint16Array(data []byte) []uint16
- func Uint32ToRegs(in []uint32) []uint16
- func Uint32ToRegsSwapRegs(in []uint32) []uint16
- type ASCIIADU
- type Client
- func (c *Client) Close() error
- func (c *Client) ReadCoils(id byte, coil, count uint16) ([]bool, error)
- func (c *Client) ReadDiscreteInputs(id byte, input, count uint16) ([]bool, error)
- func (c *Client) ReadHoldingRegs(id byte, reg, count uint16) ([]uint16, error)
- func (c *Client) ReadInputRegs(id byte, reg, count uint16) ([]uint16, error)
- func (c *Client) SetDebugLevel(debug int)
- func (c *Client) WriteSingleCoil(id byte, coil uint16, v bool) error
- func (c *Client) WriteSingleReg(id byte, reg, value uint16) error
- type ExceptionCode
- type FuncReadHoldingRegisterResponse
- type FuncReadHoldingRegistersRequest
- type FuncWriteMultipleRegisterRequest
- type FunctionCode
- type Modbus
- type PDU
- func ReadCoils(address uint16, count uint16) PDU
- func ReadDiscreteInputs(address uint16, count uint16) PDU
- func ReadHoldingRegs(address uint16, count uint16) PDU
- func ReadInputRegs(address uint16, count uint16) PDU
- func WriteSingleCoil(address uint16, v bool) PDU
- func WriteSingleReg(address, value uint16) PDU
- type RTU
- type Reg
- type RegProvider
- type Regs
- func (r *Regs) AddCoil(num int)
- func (r *Regs) AddReg(address int, count int)
- func (r *Regs) AddRegValueValidator(address int, validate func(uint16) bool) error
- func (r *Regs) ReadCoil(num int) (bool, error)
- func (r *Regs) ReadDiscreteInput(num int) (bool, error)
- func (r *Regs) ReadInputReg(address int) (uint16, error)
- func (r *Regs) ReadReg(address int) (uint16, error)
- func (r *Regs) ReadRegFloat32(address int) (float32, error)
- func (r *Regs) ReadRegInt32(address int) (int32, error)
- func (r *Regs) ReadRegUint32(address int) (uint32, error)
- func (r *Regs) WriteCoil(num int, value bool) error
- func (r *Regs) WriteReg(address int, value uint16) error
- func (r *Regs) WriteRegFloat32(address int, value float32) error
- func (r *Regs) WriteRegInt32(address int, value int32) error
- func (r *Regs) WriteRegUint32(address int, value uint32) error
- type RtuADU
- type Server
- type TCP
- type TCPADU
- type TCPServer
- type Transport
- type TransportClientServer
- type TransportType
Constants ¶
const ( WriteCoilValueOn uint16 = 0xff00 WriteCoilValueOff uint16 = 0 )
define valid values for write coil
Variables ¶
var ErrCRC = errors.New("CRC error")
ErrCRC is returned if a crc check fails
var ErrNotEnoughData = errors.New("Not enough data to calculate CRC")
ErrNotEnoughData is returned if not enough data
var ErrUnknownRegister = errors.New("unknown register")
ErrUnknownRegister is returned if a validator is added on a register that has not been added.
Functions ¶
func DecodeASCIIByte ¶
DecodeASCIIByte converts type ascii hex bytes to a binary byte
func DecodeASCIIByteEnd ¶
DecodeASCIIByteEnd converts type ascii hex bytes to a binary byte. This function takes from the end of the slice
func Float32ToRegs ¶
Float32ToRegs converts float32 values to modbus regs
func Float32ToRegsSwapWords ¶
Float32ToRegsSwapWords converts float32 values to modbus regs and swaps the words
func Int32ToRegs ¶
Int32ToRegs converts int32 values to modbus regs
func Int32ToRegsSwapWords ¶
Int32ToRegsSwapWords converts int32 values to modbus regs
func PutUint16Array ¶
PutUint16Array creates a sequence of uint16 data.
func RegsToFloat32 ¶
RegsToFloat32 converts modbus regs to float32 values
func RegsToFloat32SwapWords ¶
RegsToFloat32SwapWords converts modbus regs to float32 values
func RegsToInt16 ¶
RegsToInt16 converts modbus regs to int16 values
func RegsToInt32 ¶
RegsToInt32 converts modbus regs to int32 values
func RegsToInt32SwapWords ¶
RegsToInt32SwapWords converts modbus regs to int32 values
func RegsToUint32 ¶
RegsToUint32 converts modbus regs to uint32 values
func RegsToUint32SwapWords ¶
RegsToUint32SwapWords converts modbus regs to uint32 values
func Uint16Array ¶
Uint16Array unpacks 16 bit data values from a buffer (in big endian format)
func Uint32ToRegs ¶
Uint32ToRegs converts uint32 values to modbus regs
func Uint32ToRegsSwapRegs ¶
Uint32ToRegsSwapRegs converts uint32 values to modbus regs
Types ¶
type ASCIIADU ¶
type ASCIIADU struct { Address byte FunctionCode FunctionCode Data []byte LRC byte End []byte // should be "\r\n" }
ASCIIADU is a modbus protocol data unit
func DecodeASCIIPDU ¶
DecodeASCIIPDU decodes a ASCII modbus packet
func (*ASCIIADU) DecodeFunctionData ¶
DecodeFunctionData extracts the function data from the PDU
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client defines a Modbus client (master)
func NewClient ¶
NewClient is used to create a new modbus client port must return an entire packet for each Read(). github.com/simpleiot/simpleiot/respreader is a good way to do this.
func (*Client) ReadDiscreteInputs ¶
ReadDiscreteInputs is used to read modbus discrete inputs
func (*Client) ReadHoldingRegs ¶
ReadHoldingRegs is used to read modbus coils
func (*Client) ReadInputRegs ¶
ReadInputRegs is used to read modbus coils
func (*Client) SetDebugLevel ¶
SetDebugLevel allows you to change debug level on the fly
func (*Client) WriteSingleCoil ¶
WriteSingleCoil is used to read modbus coils
type ExceptionCode ¶
type ExceptionCode byte
ExceptionCode represents a modbus exception code
const ( ExcIllegalFunction ExceptionCode = 1 ExcIllegalAddress ExceptionCode = 2 ExcIllegalValue ExceptionCode = 3 ExcServerDeviceFailure ExceptionCode = 4 ExcAcknowledge ExceptionCode = 5 ExcServerDeviceBusy ExceptionCode = 6 ExcMemoryParityError ExceptionCode = 8 ExcGatewayTargetFailedToRespond ExceptionCode = 0x0b )
Defined valid exception codes
func (ExceptionCode) Error ¶
func (e ExceptionCode) Error() string
type FuncReadHoldingRegisterResponse ¶
type FuncReadHoldingRegisterResponse struct { FunctionCode FunctionCode RegCount byte RegValues []uint16 }
FuncReadHoldingRegisterResponse response to read holding reg
type FuncReadHoldingRegistersRequest ¶
type FuncReadHoldingRegistersRequest struct { FunctionCode FunctionCode StartingAddress uint16 RegCount uint16 }
FuncReadHoldingRegistersRequest represents the request to read holding reg
type FuncWriteMultipleRegisterRequest ¶
type FuncWriteMultipleRegisterRequest struct { FunctionCode FunctionCode StartingAddress uint16 RegCount uint16 ByteCount byte RegValues []uint16 }
FuncWriteMultipleRegisterRequest represents the request to write multiple regs
type FunctionCode ¶
type FunctionCode byte
FunctionCode represents a modbus function code
const ( // Bit access FuncCodeReadDiscreteInputs FunctionCode = 2 FuncCodeReadCoils FunctionCode = 1 FuncCodeWriteSingleCoil FunctionCode = 5 FuncCodeWriteMultipleCoils FunctionCode = 15 // 16-bit access FuncCodeReadInputRegisters FunctionCode = 4 FuncCodeReadHoldingRegisters FunctionCode = 3 FuncCodeWriteSingleRegister FunctionCode = 6 FuncCodeWriteMultipleRegisters FunctionCode = 16 FuncCodeReadWriteMultipleRegisters FunctionCode = 23 FuncCodeMaskWriteRegister FunctionCode = 22 FuncCodeReadFIFOQueue FunctionCode = 24 )
Defined valid function codes
type Modbus ¶
type Modbus struct {
// contains filtered or unexported fields
}
Modbus is a type that implements modbus ascii communication. Currently, only "sniffing" a network is implemented
type PDU ¶
type PDU struct { FunctionCode FunctionCode Data []byte }
PDU for Modbus packets
func ReadDiscreteInputs ¶
ReadDiscreteInputs creates PDU to read descrete inputs
func ReadHoldingRegs ¶
ReadHoldingRegs creates a PDU to read a holding regs
func ReadInputRegs ¶
ReadInputRegs creates a PDU to read input regs
func WriteSingleCoil ¶
WriteSingleCoil creates PDU to read coils
func WriteSingleReg ¶
WriteSingleReg creates PDU to read coils
func (*PDU) ProcessRequest ¶
func (p *PDU) ProcessRequest(regs RegProvider, changedRegs *types.ChangedRegisters) (bool, PDU, error)
ProcessRequest a modbus request. Registers are read and written through the server interface argument. This function returns any register changes, the modbus respose, and any errors
func (*PDU) RespReadBits ¶
RespReadBits reads coils and discrete inputs from a response PDU.
func (*PDU) RespReadRegs ¶
RespReadRegs reads register values from a response PDU.
type RTU ¶
type RTU struct {
// contains filtered or unexported fields
}
RTU defines an RTU connection
type RegProvider ¶
type RegProvider interface { ReadReg(address int) (uint16, error) WriteReg(address int, value uint16) error ReadInputReg(address int) (uint16, error) ReadDiscreteInput(num int) (bool, error) ReadCoil(num int) (bool, error) WriteCoil(num int, value bool) error }
RegProvider is the interface for a register provider. Regs is the canonical implementation.
type Regs ¶
type Regs struct {
// contains filtered or unexported fields
}
Regs represents all registers in a modbus device and provides functions to read/write 16-bit and bit values. This register module assumes all register types map into one address space as described in the modbus spec (http://www.modbus.org/docs/Modbus_Application_Protocol_V1_1b3.pdf) on page 6 and 7. All operations on Regs are threadsafe and protected by a mutex.
func (*Regs) AddCoil ¶
AddCoil is used to add a discrete io to the register map. Note coils are aliased on top of other registers, so coil 20 would be register 1 bit 4 (16 + 4 = 20).
func (*Regs) AddReg ¶
AddReg is used to add a modbus register to the server. the callback function is called when the reg is updated The register can be updated by word or bit operations.
func (*Regs) AddRegValueValidator ¶
AddRegValueValidator is used to add a validator function to a modbus register. The validator function is called when a modbus client tries to write a value. If the value is invalid, ExcIllegalValue (modbus exception 3) is returned to the client.
func (*Regs) ReadDiscreteInput ¶
ReadDiscreteInput gets a discrete input
func (*Regs) ReadInputReg ¶
ReadInputReg is used to read a modbus input register
func (*Regs) ReadRegFloat32 ¶
ReadRegFloat32 reads a float32 from regs
func (*Regs) ReadRegInt32 ¶
ReadRegInt32 reads a int32 from regs
func (*Regs) ReadRegUint32 ¶
ReadRegUint32 reads a uint32 from regs
func (*Regs) WriteRegFloat32 ¶
WriteRegFloat32 writes a float32 to regs
func (*Regs) WriteRegInt32 ¶
WriteRegInt32 writes a int32 to regs
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server defines a server (slave) Current Server only supports Modbus RTU, but could be expanded to do ASCII and TCP.
func NewServer ¶
NewServer creates a new server instance port must return an entire packet for each Read(). github.com/simpleiot/simpleiot/respreader is a good way to do this.
func (*Server) Listen ¶
func (s *Server) Listen(errorCallback func(error), changesCallback func(changedRegs *types.ChangedRegisters), done func())
Listen starts the server and listens for modbus requests this function does not return unless an error occurs The listen function supports various debug levels: 1 - dump packets 9 - dump raw data
type TCP ¶
type TCP struct {
// contains filtered or unexported fields
}
TCP defines an TCP connection
type TCPServer ¶
type TCPServer struct {
// contains filtered or unexported fields
}
TCPServer listens for new connections and then starts a modbus listener on the port.
func NewTCPServer ¶
NewTCPServer starts a new TCP modbus server
func (*TCPServer) InitializeServer ¶
func (*TCPServer) Listen ¶
func (ts *TCPServer) Listen(errorCallback func(error), changesCallback func(changedRegs *types.ChangedRegisters), done func())
Listen starts the server and listens for modbus requests this function does not return unless an error occurs The listen function supports various debug levels: 1 - dump packets 9 - dump raw data
type Transport ¶
type Transport interface { io.ReadWriteCloser Encode(byte, PDU) ([]byte, error) Decode([]byte) (byte, PDU, error) Type() TransportType }
Transport defines an interface that various transports (RTU, TCP, etc) implement and can be passed to clients/servers
type TransportClientServer ¶
type TransportClientServer string
TransportClientServer defines if transport is being used for a client or server
const ( TransportClient TransportClientServer = "client" TransportServer TransportClientServer = "server" )
define valid client server types
type TransportType ¶
type TransportType string
TransportType defines a modbus transport type
const ( TransportTypeTCP TransportType = "tcp" TransportTypeRTU TransportType = "rtu" )
define valid transport types