Documentation ¶
Overview ¶
Package modbus implements a threadsafe modbus library.
Getting Started ¶
Start by initializing a valid ConnectionSettings object and passing it to GetClientHandle. If successful, the error will be <nil> and you can use ClientHandle.Send(Query) to transmit Queries.
Concurrent Access ¶
The ClientHandle can be used in multiple goroutines concurrently as long as ClientHandle.Close() has yet to be called. To allow for each goroutine to call ClientHandle.Close() asynchronously, multiple ClientHandles for the same ConnectionSettings can be acquired using repeated calls to GetClientHandle. The clients are hashed by their ConnectionSettings.Host string, and ConnectionSettings must match exactly for multiple ClientHandles to be returned.
Cleanup ¶
After all ClientHandles for a given client with the corresponding ConnectionSettings have been closed, the client is automatically shutdown.
Synchronous Access ¶
The RTU, ASCII and TCP Packagers implement the modbus protocol and can be used directly to send Queries. This is intended to allow more flexible use of the underlying modbus library, such as in simple programs that don't require concurrency.
Index ¶
- Constants
- Variables
- type ASCIIPackager
- type ClientHandle
- type ConnectionSettings
- type FunctionCode
- type Mode
- type Packager
- type Query
- func MaskWriteRegister(slaveID byte, address, andMask, orMask uint16) (Query, error)
- func ReadCoils(slaveID byte, address, quantity uint16) (Query, error)
- func ReadDiscreteInputs(slaveID byte, address, quantity uint16) (Query, error)
- func ReadHoldingRegisters(slaveID byte, address, quantity uint16) (Query, error)
- func ReadInputRegisters(slaveID byte, address, quantity uint16) (Query, error)
- func ReadQuery(slaveID byte, fCode FunctionCode, address, quantity uint16) (Query, error)
- func WriteMultipleCoils(slaveID byte, address, quantity uint16, value []uint16) (Query, error)
- func WriteMultipleQuery(slaveID byte, fCode FunctionCode, address, quantity uint16, values []uint16) (Query, error)
- func WriteMultipleRegisters(slaveID byte, address, quantity uint16, value []uint16) (Query, error)
- func WriteSingleCoil(slaveID byte, address uint16, value bool) (Query, error)
- func WriteSingleQuery(slaveID byte, fCode FunctionCode, address, value uint16) (Query, error)
- func WriteSingleRegister(slaveID byte, address, value uint16) (Query, error)
- type RTUPackager
- type TCPPackager
- type Transporter
Constants ¶
const ( MaxRTUSize = 512 MaxASCIISize = 512 MaxTCPSize = 260 )
MaxRTUSize MaxASCIISize and MaxTCPSize define the maximum allowable number of byes in a single Modbus packet.
const ( FunctionReadCoils FunctionCode = 0x01 FunctionReadDiscreteInputs = 0x02 FunctionReadHoldingRegisters = 0x03 FunctionReadInputRegisters = 0x04 FunctionWriteSingleCoil = 0x05 FunctionWriteSingleRegister = 0x06 FunctionWriteMultipleCoils = 0x0F FunctionWriteMultipleRegisters = 0x10 FunctionMaskWriteRegister = 0x16 )
Modbus Function Codes
Variables ¶
var FunctionCodes = map[string]FunctionCode{}
FunctionCodes maps FunctionCodes by their FunctionName, i.e. the inverse of the FunctionNames map
var FunctionNames = map[FunctionCode]string{ FunctionReadCoils: "ReadCoils", FunctionReadDiscreteInputs: "ReadDiscreteInputs", FunctionReadHoldingRegisters: "ReadHoldingRegisters", FunctionReadInputRegisters: "ReadInputRegisters", FunctionWriteSingleCoil: "WriteSingleCoil", FunctionWriteSingleRegister: "WriteSingleRegister", FunctionWriteMultipleCoils: "WriteMultipleCoils", FunctionWriteMultipleRegisters: "WriteMultipleRegisters", FunctionMaskWriteRegister: "MaskWriteRegister", }
FunctionNames maps function name strings by their Function Code
var ModeByName = map[string]Mode{}
ModeByName maps ModeNames to their Mode, i.e. the inverse of ModeNames.
ModeNames maps Mode to a string description
Functions ¶
This section is empty.
Types ¶
type ASCIIPackager ¶
ASCIIPackager implements the Packager interface for Modbus ASCII.
func NewASCIIPackager ¶
func NewASCIIPackager(c ConnectionSettings) (*ASCIIPackager, error)
NewASCIIPackager returns a new, ready to use ASCIIPackager with the given ConnectionSettings.
type ClientHandle ¶
type ClientHandle interface { // Send sends the Query to the underlying client for transmission and // waits for the response data. Send(q Query) ([]byte, error) // Close closes the ClientHandle. Once all ClientHandles for a given Client // have been closed, the Client will shutdown. Close() error // GetConnectionSettings returns the ConnectionSettings for the client // associated with this ClientHandle. GetConnectionSettings() ConnectionSettings }
ClientHandle provides a handle for sending Queries to a Client.
func GetClientHandle ¶
func GetClientHandle(cs ConnectionSettings) (ClientHandle, error)
GetClientHandle returns a new ClientHandle for a client with the given ConnectionSettings. ConnectionSettings with the same Host string must also match exactly for a new ClientHandle to be returned. The client is shutdown after all ClientHandles have been Closed. After a client for a given Host has been shutdown it can be reopened with different ConnectionSettings.
type ConnectionSettings ¶
ConnectionSettings holds all connection settings. For ModeTCP the Host is the FQDN or IP address AND the port number. For ModeRTU and ModeASCII the Host string holds the full path to the serial device (Linux) or the name of the COM port (Windows) and BaudRate must be specified. The Timeout is the response timeout for the the underlying connection.
type Packager ¶
type Packager interface { Send(q Query) ([]byte, error) Transporter SetDebug(debug bool) }
Packager generates the raw bytes of a Modbus packet for a given Query, transmits the Query on the underlying Transporter interface, and returns and parses the response data. A Packager is implemented for the three modbus Modes: ASCIIPackager, RTUPackager and TCPPackager.
func NewPackager ¶
func NewPackager(cs ConnectionSettings) (Packager, error)
NewPackager returns a Packager according to the cs.Mode.
type Query ¶
type Query struct { FunctionCode SlaveID byte Address uint16 Quantity uint16 Values []uint16 }
Query contains the necessary data for a Packager to construct and execute a Modbus query.
func MaskWriteRegister ¶
MaskWriteRegister constructs a MaskWriteRegister Query object.
func ReadDiscreteInputs ¶
ReadDiscreteInputs constructs a ReadDiscreteInputs Query object.
func ReadHoldingRegisters ¶
ReadHoldingRegisters constructs a ReadHoldingRegisters Query object.
func ReadInputRegisters ¶
ReadInputRegisters constructs a ReadInputRegisters Query object.
func ReadQuery ¶
func ReadQuery(slaveID byte, fCode FunctionCode, address, quantity uint16) (Query, error)
ReadQuery constructs a Query where isReadFunction(fCode) is true.
func WriteMultipleCoils ¶
WriteMultipleCoils constructs a WriteMultipleCoils Query object.
func WriteMultipleQuery ¶
func WriteMultipleQuery(slaveID byte, fCode FunctionCode, address, quantity uint16, values []uint16) (Query, error)
WriteMultipleQuery constructs a Query where isWriteMultipleFunction(fCode) is true.
func WriteMultipleRegisters ¶
WriteMultipleRegisters constructs a WriteMultipleRegisters Query object.
func WriteSingleCoil ¶
WriteSingleCoil constructs a WriteSingleCoil Query object.
func WriteSingleQuery ¶
func WriteSingleQuery(slaveID byte, fCode FunctionCode, address, value uint16) (Query, error)
WriteSingleQuery constructs a Query where isWriteSingleFunction(fCode) is true.
func WriteSingleRegister ¶
WriteSingleRegister constructs a WriteSingleRegister Query object.
func (Query) IsValid ¶
IsValid returns a bool representing whether the Query is well constructed with a supported FunctionCode and appropriate Quantity and len(Values). If the query is invalid, IsValid returns false, and an error describing the reason for not passing. Otherwise it returns true, nil.
type RTUPackager ¶
RTUPackager implements the Packager interface for Modbus RTU.
func NewRTUPackager ¶
func NewRTUPackager(c ConnectionSettings) (*RTUPackager, error)
NewRTUPackager returns a new, ready to use RTUPackager with the given ConnectionSettings.
type TCPPackager ¶
TCPPackager implements the Packager interface for Modbus TCP.
func NewTCPPackager ¶
func NewTCPPackager(c ConnectionSettings) (*TCPPackager, error)
NewTCPPackager returns a new, ready to use TCPPackager with the given ConnectionSettings.