Documentation ¶
Overview ¶
Package mbserver implments a Modbus server (slave).
Example ¶
Start a Modbus server and use a client to write to and read from the serer.
// Start the server. serv := NewServer() err := serv.ListenTCP("127.0.0.1:1502") if err != nil { log.Printf("%v\n", err) return } defer serv.Close() // Wait for the server to start time.Sleep(1 * time.Millisecond) // Connect a client. handler := modbus.NewTCPClientHandler("localhost:1502") err = handler.Connect() if err != nil { log.Printf("%v\n", err) return } defer handler.Close() client := modbus.NewClient(handler) // Write some registers. _, err = client.WriteMultipleRegisters(0, 3, []byte{0, 3, 0, 4, 0, 5}) if err != nil { log.Printf("%v\n", err) } // Read those registers back. results, err := client.ReadHoldingRegisters(0, 3) if err != nil { log.Printf("%v\n", err) } fmt.Printf("results %v\n", results)
Output: results [0 3 0 4 0 5]
Index ¶
- func BytesToUint16(bytes []byte) []uint16
- func SetDataWithRegisterAndNumber(frame Framer, register uint16, number uint16)
- func SetDataWithRegisterAndNumberAndBytes(frame Framer, register uint16, number uint16, bytes []byte)
- func SetDataWithRegisterAndNumberAndValues(frame Framer, register uint16, number uint16, values []uint16)
- func Uint16ToBytes(values []uint16) []byte
- type ContextFunctionHandler
- type Exception
- func GetException(frame Framer) (exception Exception)
- func ReadCoils(s *Server, frame Framer) ([]byte, *Exception)
- func ReadDiscreteInputs(s *Server, frame Framer) ([]byte, *Exception)
- func ReadHoldingRegisters(s *Server, frame Framer) ([]byte, *Exception)
- func ReadInputRegisters(s *Server, frame Framer) ([]byte, *Exception)
- func WriteHoldingRegister(s *Server, frame Framer) ([]byte, *Exception)
- func WriteHoldingRegisters(s *Server, frame Framer) ([]byte, *Exception)
- func WriteMultipleCoils(s *Server, frame Framer) ([]byte, *Exception)
- func WriteSingleCoil(s *Server, frame Framer) ([]byte, *Exception)
- type Framer
- type FunctionHandler
- type RTUFrame
- func (frame *RTUFrame) Bytes() []byte
- func (frame *RTUFrame) Copy() Framer
- func (frame *RTUFrame) GetData() []byte
- func (frame *RTUFrame) GetFunction() uint8
- func (frame *RTUFrame) GetUnitID() uint8
- func (frame *RTUFrame) SetData(data []byte)
- func (frame *RTUFrame) SetException(exception *Exception)
- type Request
- type Server
- func (s *Server) Close()
- func (s *Server) ListenRTU(serialConfig *serial.Config) error
- func (s *Server) ListenTCP(endpoint string) (err error)
- func (s *Server) ListenTLS(endpoint, key, crt, ca string) error
- func (s *Server) RegisterContextFunctionHandler(code uint8, handler ContextFunctionHandler)
- func (s *Server) RegisterFunctionHandler(code uint8, handler FunctionHandler)
- type TCPFrame
- func (frame *TCPFrame) Bytes() []byte
- func (frame *TCPFrame) Copy() Framer
- func (frame *TCPFrame) GetData() []byte
- func (frame *TCPFrame) GetFunction() uint8
- func (frame *TCPFrame) GetUnitID() uint8
- func (frame *TCPFrame) SetData(data []byte)
- func (frame *TCPFrame) SetException(exception *Exception)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BytesToUint16 ¶
BytesToUint16 converts a big endian array of bytes to an array of unit16s
func SetDataWithRegisterAndNumber ¶
SetDataWithRegisterAndNumber sets the RTUFrame Data byte field to hold a register and number of registers
func SetDataWithRegisterAndNumberAndBytes ¶
func SetDataWithRegisterAndNumberAndBytes(frame Framer, register uint16, number uint16, bytes []byte)
SetDataWithRegisterAndNumberAndBytes sets the TCPFrame Data byte field to hold a register and number of registers and coil bytes
func SetDataWithRegisterAndNumberAndValues ¶
func SetDataWithRegisterAndNumberAndValues(frame Framer, register uint16, number uint16, values []uint16)
SetDataWithRegisterAndNumberAndValues sets the TCPFrame Data byte field to hold a register and number of registers and values
func Uint16ToBytes ¶
Uint16ToBytes converts an array of uint16s to a big endian array of bytes
Types ¶
type ContextFunctionHandler ¶ added in v0.2.0
ContextFunctionHandler defines a function type for defining external Modbus function code handlers with a Context.
type Exception ¶
type Exception uint8
Exception codes.
var ( // Success operation successful. Success Exception // IllegalFunction function code received in the query is not recognized or allowed by slave. IllegalFunction Exception = 1 // IllegalDataAddress data address of some or all the required entities are not allowed or do not exist in slave. IllegalDataAddress Exception = 2 // IllegalDataValue value is not accepted by slave. IllegalDataValue Exception = 3 // SlaveDeviceFailure Unrecoverable error occurred while slave was attempting to perform requested action. SlaveDeviceFailure Exception = 4 // AcknowledgeSlave has accepted request and is processing it, but a long duration of time is required. This response is returned to prevent a timeout error from occurring in the master. Master can next issue a Poll Program Complete message to determine whether processing is completed. AcknowledgeSlave Exception = 5 // SlaveDeviceBusy is engaged in processing a long-duration command. Master should retry later. SlaveDeviceBusy Exception = 6 // NegativeAcknowledge Slave cannot perform the programming functions. Master should request diagnostic or error information from slave. NegativeAcknowledge Exception = 7 // MemoryParityError Slave detected a parity error in memory. Master can retry the request, but service may be required on the slave device. MemoryParityError Exception = 8 GatewayPathUnavailable Exception = 10 // GatewayTargetDeviceFailedtoRespond Specialized for Modbus gateways. Sent when slave fails to respond. GatewayTargetDeviceFailedtoRespond Exception = 11 )
func GetException ¶
GetException retunrns the Modbus exception or Success (indicating not exception).
func ReadDiscreteInputs ¶
ReadDiscreteInputs function 2, reads discrete inputs from internal memory.
func ReadHoldingRegisters ¶
ReadHoldingRegisters function 3, reads holding registers from internal memory.
func ReadInputRegisters ¶
ReadInputRegisters function 4, reads input registers from internal memory.
func WriteHoldingRegister ¶
WriteHoldingRegister function 6, write a holding register to internal memory.
func WriteHoldingRegisters ¶
WriteHoldingRegisters function 16, writes holding registers to internal memory.
func WriteMultipleCoils ¶
WriteMultipleCoils function 15, writes holding registers to internal memory.
func WriteSingleCoil ¶
WriteSingleCoil function 5, write a coil to internal memory.
type Framer ¶
type Framer interface { Bytes() []byte Copy() Framer GetUnitID() uint8 GetData() []byte GetFunction() uint8 SetException(exception *Exception) SetData(data []byte) }
Framer is the interface that wraps Modbus frames.
type FunctionHandler ¶
FunctionHandler defines a function type for defining custom Modbus function code handlers.
type RTUFrame ¶
RTUFrame is the Modbus TCP frame.
func NewRTUFrame ¶
NewRTUFrame converts a packet to a Modbus TCP frame.
func (*RTUFrame) GetFunction ¶
GetFunction returns the Modbus function code.
func (*RTUFrame) SetData ¶
SetData sets the RTUFrame Data byte field and updates the frame length accordingly.
func (*RTUFrame) SetException ¶
SetException sets the Modbus exception code in the frame.
type Request ¶
type Request struct {
// contains filtered or unexported fields
}
Request contains the connection and Modbus frame.
type Server ¶
type Server struct { // Debug enables more verbose messaging. Debug bool DiscreteInputs []byte Coils []byte HoldingRegisters []uint16 InputRegisters []uint16 // contains filtered or unexported fields }
Server is a Modbus slave with allocated memory for discrete inputs, coils, etc.
func NewServerWithDefaults ¶ added in v0.2.0
func NewServerWithDefaults() *Server
NewServer creates a new Modbus server (slave) with default function handlers and registers.
func (*Server) Close ¶
func (s *Server) Close()
Close stops listening to TCP/IP ports and closes serial ports.
func (*Server) ListenRTU ¶
ListenRTU starts the Modbus server listening to a serial device. For example: err := s.ListenRTU(&serial.Config{Address: "/dev/ttyUSB0"})
func (*Server) ListenTLS ¶ added in v0.2.0
ListenTLS starts the Modbus server listening securely on "address:port", using the key, certificate, and CA certificate at the paths provided.
func (*Server) RegisterContextFunctionHandler ¶ added in v0.2.0
func (s *Server) RegisterContextFunctionHandler(code uint8, handler ContextFunctionHandler)
RegisterContextFunctionHandler registers a new external ContextFunctionHandler.
func (*Server) RegisterFunctionHandler ¶
func (s *Server) RegisterFunctionHandler(code uint8, handler FunctionHandler)
RegisterFunctionHandler override the default behavior for a given Modbus function.
Example ¶
Override the default ReadDiscreteInputs funtion.
serv := NewServer() // Override ReadDiscreteInputs function. serv.RegisterFunctionHandler(2, func(s *Server, frame Framer) ([]byte, *Exception) { register, numRegs, endRegister := registerAddressAndNumber(frame) // Check the request is within the allocated memory if endRegister > 65535 { return []byte{}, &IllegalDataAddress } dataSize := numRegs / 8 if (numRegs % 8) != 0 { dataSize++ } data := make([]byte, 1+dataSize) data[0] = byte(dataSize) for i := range s.DiscreteInputs[register:endRegister] { // Return all 1s, regardless of the value in the DiscreteInputs array. shift := uint(i) % 8 data[1+i/8] |= byte(1 << shift) } return data, &Success }) // Start the server. err := serv.ListenTCP("localhost:4321") if err != nil { log.Printf("%v\n", err) return } defer serv.Close() // Wait for the server to start time.Sleep(1 * time.Millisecond) // Connect a client. handler := modbus.NewTCPClientHandler("localhost:4321") err = handler.Connect() if err != nil { log.Printf("%v\n", err) return } defer handler.Close() client := modbus.NewClient(handler) // Read discrete inputs. results, err := client.ReadDiscreteInputs(0, 16) if err != nil { log.Printf("%v\n", err) } fmt.Printf("results %v\n", results)
Output: results [255 255]
type TCPFrame ¶
type TCPFrame struct { TransactionIdentifier uint16 ProtocolIdentifier uint16 Length uint16 Device uint8 Function uint8 Data []byte }
TCPFrame is the Modbus TCP frame.
func NewTCPFrame ¶
NewTCPFrame converts a packet to a Modbus TCP frame.
func (*TCPFrame) GetFunction ¶
GetFunction returns the Modbus function code.
func (*TCPFrame) SetData ¶
SetData sets the TCPFrame Data byte field and updates the frame length accordingly.
func (*TCPFrame) SetException ¶
SetException sets the Modbus exception code in the frame.