Documentation ¶
Overview ¶
Package modbus provides a client for modbus TCP and RTU/ASCII.contain modbus TCP svc
Index ¶
- Constants
- Variables
- func SetSpecialAddressMax(addr byte)
- type ASCIIClientProvider
- func (sf *ASCIIClientProvider) Close() error
- func (sf *ASCIIClientProvider) Connect() error
- func (sf ASCIIClientProvider) Debug(format string, v ...interface{})
- func (sf ASCIIClientProvider) Error(format string, v ...interface{})
- func (sf *ASCIIClientProvider) IsConnected() bool
- func (sf *ASCIIClientProvider) LogMode(enable bool)
- func (sf *ASCIIClientProvider) Send(slaveID byte, request ProtocolDataUnit) (ProtocolDataUnit, error)
- func (sf *ASCIIClientProvider) SendFrame(aduRequest []byte, readLen int) (aduResponse []byte, err error)
- func (sf *ASCIIClientProvider) SendPdu(slaveID byte, pduRequest []byte) ([]byte, error)
- func (sf *ASCIIClientProvider) SendRawFrame(aduRequest []byte) (aduResponse []byte, err error)
- func (sf *ASCIIClientProvider) SetAutoReconnect(cnt byte)
- func (sf *ASCIIClientProvider) SetLogProvider(p LogProvider)
- type Client
- type ClientProvider
- type ExceptionError
- type FunctionHandler
- type GENClientProvider
- func (sf *GENClientProvider) Close() error
- func (sf *GENClientProvider) Connect() error
- func (sf GENClientProvider) Debug(format string, v ...interface{})
- func (sf GENClientProvider) Error(format string, v ...interface{})
- func (sf *GENClientProvider) IsConnected() bool
- func (sf *GENClientProvider) LogMode(enable bool)
- func (sf *GENClientProvider) Send(slaveID byte, request ProtocolDataUnit) (ProtocolDataUnit, error)
- func (sf *GENClientProvider) SendFrame(aduRequest []byte, readLen int) (aduResponse []byte, err error)
- func (sf *GENClientProvider) SendPdu(slaveID byte, pduRequest []byte) ([]byte, error)
- func (sf *GENClientProvider) SendRawFrame(aduRequest []byte) (aduResponse []byte, err error)
- func (sf *GENClientProvider) SetAutoReconnect(cnt byte)
- func (sf *GENClientProvider) SetLogProvider(p LogProvider)
- type LogProvider
- type NodeRegister
- func (sf *NodeRegister) MaskWriteHolding(address, andMask, orMask uint16) error
- func (sf *NodeRegister) ReadCoils(address, quality uint16) ([]byte, error)
- func (sf *NodeRegister) ReadDiscretes(address, quality uint16) ([]byte, error)
- func (sf *NodeRegister) ReadHoldings(address, quality uint16) ([]uint16, error)
- func (sf *NodeRegister) ReadHoldingsBytes(address, quality uint16) ([]byte, error)
- func (sf *NodeRegister) ReadInputs(address, quality uint16) ([]uint16, error)
- func (sf *NodeRegister) ReadInputsBytes(address, quality uint16) ([]byte, error)
- func (sf *NodeRegister) ReadSingleCoil(address uint16) (bool, error)
- func (sf *NodeRegister) ReadSingleDiscrete(address uint16) (bool, error)
- func (sf *NodeRegister) SetSlaveID(id byte)
- func (sf *NodeRegister) SlaveID() byte
- func (sf *NodeRegister) WriteCoils(address, quality uint16, valBuf []byte) error
- func (sf *NodeRegister) WriteDiscretes(address, quality uint16, valBuf []byte) error
- func (sf *NodeRegister) WriteHoldings(address uint16, valBuf []uint16) error
- func (sf *NodeRegister) WriteHoldingsBytes(address, quality uint16, valBuf []byte) error
- func (sf *NodeRegister) WriteInputs(address uint16, valBuf []uint16) error
- func (sf *NodeRegister) WriteInputsBytes(address, quality uint16, regBuf []byte) error
- func (sf *NodeRegister) WriteSingleCoil(address uint16, val bool) error
- func (sf *NodeRegister) WriteSingleDiscrete(address uint16, val bool) error
- type OnConnectHandler
- type OnConnectionLostHandler
- type OnKeepAliveHandler
- type ProtocolDataUnit
- type RTUClientProvider
- func (sf *RTUClientProvider) Close() error
- func (sf *RTUClientProvider) Connect() error
- func (sf RTUClientProvider) Debug(format string, v ...interface{})
- func (sf RTUClientProvider) Error(format string, v ...interface{})
- func (sf *RTUClientProvider) IsConnected() bool
- func (sf *RTUClientProvider) LogMode(enable bool)
- func (sf *RTUClientProvider) Send(slaveID byte, request ProtocolDataUnit) (ProtocolDataUnit, error)
- func (sf *RTUClientProvider) SendFrame(aduRequest []byte, readLen int) (aduResponse []byte, err error)
- func (sf *RTUClientProvider) SendPdu(slaveID byte, pduRequest []byte) ([]byte, error)
- func (sf *RTUClientProvider) SendRawFrame(aduRequest []byte) (aduResponse []byte, err error)
- func (sf *RTUClientProvider) SetAutoReconnect(cnt byte)
- func (sf *RTUClientProvider) SetLogProvider(p LogProvider)
- type ServerSession
- func (sf ServerSession) AddNodes(nodes ...*NodeRegister)
- func (sf ServerSession) Debug(format string, v ...interface{})
- func (sf ServerSession) DeleteAllNode()
- func (sf ServerSession) DeleteNode(slaveID byte)
- func (sf ServerSession) Error(format string, v ...interface{})
- func (sf ServerSession) GetNode(slaveID byte) (*NodeRegister, error)
- func (sf ServerSession) GetNodeList() []*NodeRegister
- func (sf *ServerSession) LogMode(enable bool)
- func (sf ServerSession) Range(f func(slaveID byte, node *NodeRegister) bool)
- func (sf ServerSession) RegisterFunctionHandler(funcCode uint8, function FunctionHandler)
- func (sf *ServerSession) SetLogProvider(p LogProvider)
- func (sf *ServerSession) SetReadTimeout(t time.Duration)
- func (sf *ServerSession) SetWriteTimeout(t time.Duration)
- type TCPClientProvider
- func (sf *TCPClientProvider) Close() error
- func (sf *TCPClientProvider) Connect() error
- func (sf TCPClientProvider) Debug(format string, v ...interface{})
- func (sf TCPClientProvider) Error(format string, v ...interface{})
- func (sf *TCPClientProvider) IsConnected() bool
- func (sf *TCPClientProvider) LogMode(enable bool)
- func (sf *TCPClientProvider) Send(slaveID byte, request ProtocolDataUnit) (ProtocolDataUnit, error)
- func (sf *TCPClientProvider) SendFrame(aduRequest []byte, readLen int) (aduResponse []byte, err error)
- func (sf *TCPClientProvider) SendPdu(slaveID byte, pduRequest []byte) ([]byte, error)
- func (sf *TCPClientProvider) SendRawFrame(aduRequest []byte) (aduResponse []byte, err error)
- func (sf *TCPClientProvider) SetAutoReconnect(cnt byte)
- func (sf *TCPClientProvider) SetLogProvider(p LogProvider)
- type TCPServer
- func (sf TCPServer) AddNodes(nodes ...*NodeRegister)
- func (sf *TCPServer) Close() error
- func (sf TCPServer) Debug(format string, v ...interface{})
- func (sf TCPServer) DeleteAllNode()
- func (sf TCPServer) DeleteNode(slaveID byte)
- func (sf TCPServer) Error(format string, v ...interface{})
- func (sf TCPServer) GetNode(slaveID byte) (*NodeRegister, error)
- func (sf TCPServer) GetNodeList() []*NodeRegister
- func (sf *TCPServer) ListenAndServe(addr string) error
- func (sf *TCPServer) LogMode(enable bool)
- func (sf TCPServer) Range(f func(slaveID byte, node *NodeRegister) bool)
- func (sf TCPServer) RegisterFunctionHandler(funcCode uint8, function FunctionHandler)
- func (sf *TCPServer) SetLogProvider(p LogProvider)
- func (sf *TCPServer) SetReadTimeout(t time.Duration)
- func (sf *TCPServer) SetWriteTimeout(t time.Duration)
- type TCPServerSpecial
- func (sf TCPServerSpecial) AddNodes(nodes ...*NodeRegister)
- func (sf *TCPServerSpecial) AddRemoteServer(server string) error
- func (sf *TCPServerSpecial) Close() error
- func (sf TCPServerSpecial) Debug(format string, v ...interface{})
- func (sf TCPServerSpecial) DeleteAllNode()
- func (sf TCPServerSpecial) DeleteNode(slaveID byte)
- func (sf *TCPServerSpecial) EnableAutoReconnect(b bool)
- func (sf TCPServerSpecial) Error(format string, v ...interface{})
- func (sf TCPServerSpecial) GetNode(slaveID byte) (*NodeRegister, error)
- func (sf TCPServerSpecial) GetNodeList() []*NodeRegister
- func (sf *TCPServerSpecial) IsClosed() bool
- func (sf *TCPServerSpecial) IsConnected() bool
- func (sf *TCPServerSpecial) LogMode(enable bool)
- func (sf TCPServerSpecial) Range(f func(slaveID byte, node *NodeRegister) bool)
- func (sf TCPServerSpecial) RegisterFunctionHandler(funcCode uint8, function FunctionHandler)
- func (sf *TCPServerSpecial) SetConnectTimeout(t time.Duration)
- func (sf *TCPServerSpecial) SetConnectionLostHandler(f OnConnectionLostHandler)
- func (sf *TCPServerSpecial) SetKeepAlive(b bool, t time.Duration, f OnKeepAliveHandler)
- func (sf *TCPServerSpecial) SetLogProvider(p LogProvider)
- func (sf *TCPServerSpecial) SetOnConnectHandler(f OnConnectHandler)
- func (sf *TCPServerSpecial) SetReconnectInterval(t time.Duration)
- func (sf *TCPServerSpecial) SetTLSConfig(t *tls.Config)
- func (sf *TCPServerSpecial) Start() error
- func (sf *TCPServerSpecial) UnderlyingConn() net.Conn
Constants ¶
const ( FuncReadMinSize = 4 // 读操作 最小数据域个数 FuncWriteMinSize = 4 // 写操作 最小数据域个数 FuncWriteMultiMinSize = 5 // 写多个操作 最小数据域个数 FuncReadWriteMinSize = 9 // 读写操作 最小数据域个数 FuncMaskWriteMinSize = 6 // 屏蔽写操作 最小数据域个数 )
pdu数据域 各功能码要求的最小长度
const ( AddressBroadCast = 0 AddressMin = 0 )
proto address limit
const ( // Bits ReadBitsQuantityMin = 1 // 0x0001 ReadBitsQuantityMax = 2000 // 0x07d0 WriteBitsQuantityMin = 1 // 1 WriteBitsQuantityMax = 1968 // 0x07b0 // 16 Bits ReadRegQuantityMin = 1 // 1 ReadRegQuantityMax = 125 // 0x007d WriteRegQuantityMin = 1 // 1 WriteRegQuantityMax = 123 // 0x007b ReadWriteOnReadRegQuantityMin = 1 // 1 ReadWriteOnReadRegQuantityMax = 125 // 0x007d ReadWriteOnWriteRegQuantityMin = 1 // 1 ReadWriteOnWriteRegQuantityMax = 121 // 0x0079 )
proto register limit
const ( // Bit access FuncCodeReadDiscreteInputs = 2 FuncCodeReadCoils = 1 FuncCodeWriteSingleCoil = 5 FuncCodeWriteMultipleCoils = 15 // 16-bit access FuncCodeReadInputRegisters = 4 FuncCodeReadHoldingRegisters = 3 FuncCodeWriteSingleRegister = 6 FuncCodeWriteMultipleRegisters = 16 FuncCodeReadWriteMultipleRegisters = 23 FuncCodeMaskWriteRegister = 22 FuncCodeReadFIFOQueue = 24 FuncCodeOtherReportSlaveID = 17 )
Function Code
const ( ExceptionCodeIllegalFunction = 1 ExceptionCodeIllegalDataAddress = 2 ExceptionCodeIllegalDataValue = 3 ExceptionCodeServerDeviceFailure = 4 ExceptionCodeAcknowledge = 5 ExceptionCodeServerDeviceBusy = 6 ExceptionCodeNegativeAcknowledge = 7 ExceptionCodeMemoryParityError = 8 ExceptionCodeGatewayTargetDeviceFailedToRespond = 11 )
Exception Code
const ( // SerialDefaultTimeout Serial Default timeout SerialDefaultTimeout = 1 * time.Second // SerialDefaultAutoReconnect Serial Default auto reconnect count SerialDefaultAutoReconnect = 0 )
const ( // TCPDefaultTimeout TCP Default timeout TCPDefaultTimeout = 1 * time.Second // TCPDefaultAutoReconnect TCP Default auto reconnect count TCPDefaultAutoReconnect = 1 )
const ( TCPDefaultReadTimeout = 60 * time.Second TCPDefaultWriteTimeout = 1 * time.Second )
TCP Default read & write timeout
const ( DefaultConnectTimeout = 15 * time.Second DefaultReconnectInterval = 1 * time.Minute DefaultKeepAliveInterval = 30 * time.Second )
defined default value
Variables ¶
var AddressMax byte = addressMax
AddressMax proto address max limit you can change with SetSpecialAddressMax, when your device_info have address upon addressMax
var ErrClosedConnection = errors.New("use of closed connection")
ErrClosedConnection 连接已关闭
Functions ¶
func SetSpecialAddressMax ¶
func SetSpecialAddressMax(addr byte)
Types ¶
type ASCIIClientProvider ¶
type ASCIIClientProvider struct {
// contains filtered or unexported fields
}
ASCIIClientProvider implements ClientProvider interface.
func NewASCIIClientProvider ¶
func NewASCIIClientProvider() *ASCIIClientProvider
NewASCIIClientProvider allocates and initializes a ASCIIClientProvider. it will use default /dev/ttyS0 19200 8 1 N and timeout 1000
func (*ASCIIClientProvider) Close ¶
func (sf *ASCIIClientProvider) Close() error
Close close current connection.
func (*ASCIIClientProvider) Connect ¶
func (sf *ASCIIClientProvider) Connect() error
Connect try to connect the remote svc
func (ASCIIClientProvider) Debug ¶
func (sf ASCIIClientProvider) Debug(format string, v ...interface{})
Debug Log DEBUG level message.
func (ASCIIClientProvider) Error ¶
func (sf ASCIIClientProvider) Error(format string, v ...interface{})
Error Log ERROR level message.
func (*ASCIIClientProvider) IsConnected ¶
func (sf *ASCIIClientProvider) IsConnected() bool
IsConnected returns a bool signifying whether the client is connected or not.
func (*ASCIIClientProvider) LogMode ¶
func (sf *ASCIIClientProvider) LogMode(enable bool)
LogMode set enable or disable log output when you has set logger
func (*ASCIIClientProvider) Send ¶
func (sf *ASCIIClientProvider) Send(slaveID byte, request ProtocolDataUnit) (ProtocolDataUnit, error)
Send request to the remote svc,it implements on SendRawFrame
func (*ASCIIClientProvider) SendFrame ¶
func (sf *ASCIIClientProvider) SendFrame(aduRequest []byte, readLen int) (aduResponse []byte, err error)
func (*ASCIIClientProvider) SendPdu ¶
func (sf *ASCIIClientProvider) SendPdu(slaveID byte, pduRequest []byte) ([]byte, error)
SendPdu send pdu request to the remote svc
func (*ASCIIClientProvider) SendRawFrame ¶
func (sf *ASCIIClientProvider) SendRawFrame(aduRequest []byte) (aduResponse []byte, err error)
SendRawFrame send Adu frame
func (*ASCIIClientProvider) SetAutoReconnect ¶
func (sf *ASCIIClientProvider) SetAutoReconnect(cnt byte)
SetAutoReconnect set auto reconnect count if cnt == 0, disable auto reconnect if cnt > 0 ,enable auto reconnect,but max 6
func (*ASCIIClientProvider) SetLogProvider ¶
func (sf *ASCIIClientProvider) SetLogProvider(p LogProvider)
SetLogProvider overwrite log provider
type Client ¶
type Client interface { ClientProvider // ReadCoils reads from 1 to 2000 contiguous status of coils in a // remote device_info and returns coil status. ReadCoils(slaveID byte, address, quantity uint16) (results []byte, err error) // ReadDiscreteInputs reads from 1 to 2000 contiguous status of // discrete inputs in a remote device_info and returns input status. ReadDiscreteInputs(slaveID byte, address, quantity uint16) (results []byte, err error) // WriteSingleCoil write a single output to either ON or OFF in a // remote device_info and returns success or failed. WriteSingleCoil(slaveID byte, address uint16, isOn bool) error // WriteMultipleCoils forces each coil in a sequence of coils to either // ON or OFF in a remote device_info and returns success or failed. WriteMultipleCoils(slaveID byte, address, quantity uint16, value []byte) error // ReadInputRegisters reads from 1 to 125 contiguous input registers in // a remote device_info and returns input registers. ReadInputRegistersBytes(slaveID byte, address, quantity uint16) (results []byte, err error) // ReadInputRegisters reads from 1 to 125 contiguous input registers in // a remote device_info and returns input registers. ReadInputRegisters(slaveID byte, address, quantity uint16) (results []uint16, err error) // ReadHoldingRegistersBytes reads the contents of a contiguous block of // holding registers in a remote device_info and returns register value. ReadHoldingRegistersBytes(slaveID byte, address, quantity uint16) (results []byte, err error) // ReadHoldingRegisters reads the contents of a contiguous block of // holding registers in a remote device_info and returns register value. ReadHoldingRegisters(slaveID byte, address, quantity uint16) (results []uint16, err error) // WriteSingleRegister writes a single holding register in a remote // device_info and returns success or failed. WriteSingleRegister(slaveID byte, address, value uint16) error // WriteMultipleRegisters writes a block of contiguous registers // (1 to 123 registers) in a remote device_info and returns success or failed. WriteMultipleRegisters(slaveID byte, address, quantity uint16, value []byte) error // ReadWriteMultipleRegisters performs a combination of one read // operation and one write operation. It returns read registers value. ReadWriteMultipleRegistersBytes(slaveID byte, readAddress, readQuantity, writeAddress, writeQuantity uint16, value []byte) (results []byte, err error) // ReadWriteMultipleRegisters performs a combination of one read // operation and one write operation. It returns read registers value. ReadWriteMultipleRegisters(slaveID byte, readAddress, readQuantity, writeAddress, writeQuantity uint16, value []byte) (results []uint16, err error) // MaskWriteRegister modify the contents of a specified holding // register using a combination of an AND mask, an OR mask, and the // register's current contents. The function returns success or failed. MaskWriteRegister(slaveID byte, address, andMask, orMask uint16) error //ReadFIFOQueue reads the contents of a First-In-First-Out (FIFO) queue // of register in a remote device_info and returns FIFO value register. ReadFIFOQueue(slaveID byte, address uint16) (results []byte, err error) }
Client interface
func NewClient ¶
func NewClient(p ClientProvider) Client
NewClient creates a new modbus client with given backend handler.
type ClientProvider ¶
type ClientProvider interface { // Connect try to connect the remote svc Connect() error // IsConnected returns a bool signifying whether // the client is connected or not. IsConnected() bool // SetAutoReconnect set auto reconnect count // if cnt == 0, disable auto reconnect // if cnt > 0 ,enable auto reconnect,but max 6 SetAutoReconnect(cnt byte) // LogMode set enable or diable log output when you has set logger LogMode(enable bool) // SetLogProvider set logger provider SetLogProvider(p LogProvider) // Close disconnect the remote svc Close() error // Send request to the remote svc,it implements on SendRawFrame Send(slaveID byte, request ProtocolDataUnit) (ProtocolDataUnit, error) // SendPdu send pdu request to the remote svc SendPdu(slaveID byte, pduRequest []byte) (pduResponse []byte, err error) // SendRawFrame send raw frame to the remote svc SendRawFrame(aduRequest []byte) (aduResponse []byte, err error) SendFrame(aduRequest []byte, readLen int) (aduResponse []byte, err error) }
ClientProvider is the interface implements underlying methods.
type ExceptionError ¶
type ExceptionError struct {
ExceptionCode byte
}
ExceptionError implements error interface.
func (*ExceptionError) Error ¶
func (e *ExceptionError) Error() string
Error converts known modbus exception code to error message.
type FunctionHandler ¶
type FunctionHandler func(reg *NodeRegister, data []byte) ([]byte, error)
FunctionHandler 功能码对应的函数回调 data 仅pdu数据域 不含功能码, return pdu 数据域,不含功能码
type GENClientProvider ¶
type GENClientProvider struct {
// contains filtered or unexported fields
}
RTUClientProvider implements ClientProvider interface.
func NewGENClientProvider ¶
func NewGENClientProvider() *GENClientProvider
NewRTUClientProvider allocates and initializes a RTUClientProvider. it will use default /dev/ttyS0 19200 8 1 N and timeout 1000
func (*GENClientProvider) Close ¶
func (sf *GENClientProvider) Close() error
Close close current connection.
func (*GENClientProvider) Connect ¶
func (sf *GENClientProvider) Connect() error
Connect try to connect the remote svc
func (GENClientProvider) Debug ¶
func (sf GENClientProvider) Debug(format string, v ...interface{})
Debug Log DEBUG level message.
func (GENClientProvider) Error ¶
func (sf GENClientProvider) Error(format string, v ...interface{})
Error Log ERROR level message.
func (*GENClientProvider) IsConnected ¶
func (sf *GENClientProvider) IsConnected() bool
IsConnected returns a bool signifying whether the client is connected or not.
func (*GENClientProvider) LogMode ¶
func (sf *GENClientProvider) LogMode(enable bool)
LogMode set enable or disable log output when you has set logger
func (*GENClientProvider) Send ¶
func (sf *GENClientProvider) Send(slaveID byte, request ProtocolDataUnit) (ProtocolDataUnit, error)
Send request to the remote svc,it implements on SendRawFrame
func (*GENClientProvider) SendFrame ¶
func (sf *GENClientProvider) SendFrame(aduRequest []byte, readLen int) (aduResponse []byte, err error)
func (*GENClientProvider) SendPdu ¶
func (sf *GENClientProvider) SendPdu(slaveID byte, pduRequest []byte) ([]byte, error)
SendPdu send pdu request to the remote svc
func (*GENClientProvider) SendRawFrame ¶
func (sf *GENClientProvider) SendRawFrame(aduRequest []byte) (aduResponse []byte, err error)
SendRawFrame send Adu frame
func (*GENClientProvider) SetAutoReconnect ¶
func (sf *GENClientProvider) SetAutoReconnect(cnt byte)
SetAutoReconnect set auto reconnect count if cnt == 0, disable auto reconnect if cnt > 0 ,enable auto reconnect,but max 6
func (*GENClientProvider) SetLogProvider ¶
func (sf *GENClientProvider) SetLogProvider(p LogProvider)
SetLogProvider overwrite log provider
type LogProvider ¶
type LogProvider interface { Error(format string, v ...interface{}) Debug(format string, v ...interface{}) }
LogProvider RFC5424 log message levels only Debug and Error
type NodeRegister ¶
type NodeRegister struct {
// contains filtered or unexported fields
}
NodeRegister 节点寄存器
func NewNodeRegister ¶
func NewNodeRegister(slaveID byte, coilsAddrStart, coilsQuantity, discreteAddrStart, discreteQuantity, inputAddrStart, inputQuantity, holdingAddrStart, holdingQuantity uint16) *NodeRegister
NewNodeRegister 创建一个modbus子节点寄存器列表
func (*NodeRegister) MaskWriteHolding ¶
func (sf *NodeRegister) MaskWriteHolding(address, andMask, orMask uint16) error
MaskWriteHolding 屏蔽写保持寄存器 (val & andMask) | (orMask & ^andMask)
func (*NodeRegister) ReadCoils ¶
func (sf *NodeRegister) ReadCoils(address, quality uint16) ([]byte, error)
ReadCoils 读线圈,返回值
func (*NodeRegister) ReadDiscretes ¶
func (sf *NodeRegister) ReadDiscretes(address, quality uint16) ([]byte, error)
ReadDiscretes 读离散量
func (*NodeRegister) ReadHoldings ¶
func (sf *NodeRegister) ReadHoldings(address, quality uint16) ([]uint16, error)
ReadHoldings 读保持寄存器,仅返回寄存器值
func (*NodeRegister) ReadHoldingsBytes ¶
func (sf *NodeRegister) ReadHoldingsBytes(address, quality uint16) ([]byte, error)
ReadHoldingsBytes 读保持寄存器,仅返回寄存器值
func (*NodeRegister) ReadInputs ¶
func (sf *NodeRegister) ReadInputs(address, quality uint16) ([]uint16, error)
ReadInputs 读输入寄存器
func (*NodeRegister) ReadInputsBytes ¶
func (sf *NodeRegister) ReadInputsBytes(address, quality uint16) ([]byte, error)
ReadInputsBytes 读输入寄存器
func (*NodeRegister) ReadSingleCoil ¶
func (sf *NodeRegister) ReadSingleCoil(address uint16) (bool, error)
ReadSingleCoil 读单个线圈
func (*NodeRegister) ReadSingleDiscrete ¶
func (sf *NodeRegister) ReadSingleDiscrete(address uint16) (bool, error)
ReadSingleDiscrete 读单个离散量
func (*NodeRegister) WriteCoils ¶
func (sf *NodeRegister) WriteCoils(address, quality uint16, valBuf []byte) error
WriteCoils 写线圈
func (*NodeRegister) WriteDiscretes ¶
func (sf *NodeRegister) WriteDiscretes(address, quality uint16, valBuf []byte) error
WriteDiscretes 写离散量
func (*NodeRegister) WriteHoldings ¶
func (sf *NodeRegister) WriteHoldings(address uint16, valBuf []uint16) error
WriteHoldings 写保持寄存器
func (*NodeRegister) WriteHoldingsBytes ¶
func (sf *NodeRegister) WriteHoldingsBytes(address, quality uint16, valBuf []byte) error
WriteHoldingsBytes 写保持寄存器
func (*NodeRegister) WriteInputs ¶
func (sf *NodeRegister) WriteInputs(address uint16, valBuf []uint16) error
WriteInputs 写输入寄存器
func (*NodeRegister) WriteInputsBytes ¶
func (sf *NodeRegister) WriteInputsBytes(address, quality uint16, regBuf []byte) error
WriteInputsBytes 写输入寄存器
func (*NodeRegister) WriteSingleCoil ¶
func (sf *NodeRegister) WriteSingleCoil(address uint16, val bool) error
WriteSingleCoil 写单个线圈
func (*NodeRegister) WriteSingleDiscrete ¶
func (sf *NodeRegister) WriteSingleDiscrete(address uint16, val bool) error
WriteSingleDiscrete 写单个离散量
type OnConnectHandler ¶
type OnConnectHandler func(c *TCPServerSpecial) error
OnConnectHandler when connected it will be call
type OnConnectionLostHandler ¶
type OnConnectionLostHandler func(c *TCPServerSpecial)
OnConnectionLostHandler when Connection lost it will be call
type OnKeepAliveHandler ¶
type OnKeepAliveHandler func(c *TCPServerSpecial)
OnKeepAliveHandler keep alive function
type ProtocolDataUnit ¶
ProtocolDataUnit (PDU) is independent of underlying communication layers.
type RTUClientProvider ¶
type RTUClientProvider struct {
// contains filtered or unexported fields
}
RTUClientProvider implements ClientProvider interface.
func NewRTUClientProvider ¶
func NewRTUClientProvider() *RTUClientProvider
NewRTUClientProvider allocates and initializes a RTUClientProvider. it will use default /dev/ttyS0 19200 8 1 N and timeout 1000
func (*RTUClientProvider) Close ¶
func (sf *RTUClientProvider) Close() error
Close close current connection.
func (*RTUClientProvider) Connect ¶
func (sf *RTUClientProvider) Connect() error
Connect try to connect the remote svc
func (RTUClientProvider) Debug ¶
func (sf RTUClientProvider) Debug(format string, v ...interface{})
Debug Log DEBUG level message.
func (RTUClientProvider) Error ¶
func (sf RTUClientProvider) Error(format string, v ...interface{})
Error Log ERROR level message.
func (*RTUClientProvider) IsConnected ¶
func (sf *RTUClientProvider) IsConnected() bool
IsConnected returns a bool signifying whether the client is connected or not.
func (*RTUClientProvider) LogMode ¶
func (sf *RTUClientProvider) LogMode(enable bool)
LogMode set enable or disable log output when you has set logger
func (*RTUClientProvider) Send ¶
func (sf *RTUClientProvider) Send(slaveID byte, request ProtocolDataUnit) (ProtocolDataUnit, error)
Send request to the remote svc,it implements on SendRawFrame
func (*RTUClientProvider) SendFrame ¶
func (sf *RTUClientProvider) SendFrame(aduRequest []byte, readLen int) (aduResponse []byte, err error)
func (*RTUClientProvider) SendPdu ¶
func (sf *RTUClientProvider) SendPdu(slaveID byte, pduRequest []byte) ([]byte, error)
SendPdu send pdu request to the remote svc
func (*RTUClientProvider) SendRawFrame ¶
func (sf *RTUClientProvider) SendRawFrame(aduRequest []byte) (aduResponse []byte, err error)
SendRawFrame send Adu frame
func (*RTUClientProvider) SetAutoReconnect ¶
func (sf *RTUClientProvider) SetAutoReconnect(cnt byte)
SetAutoReconnect set auto reconnect count if cnt == 0, disable auto reconnect if cnt > 0 ,enable auto reconnect,but max 6
func (*RTUClientProvider) SetLogProvider ¶
func (sf *RTUClientProvider) SetLogProvider(p LogProvider)
SetLogProvider overwrite log provider
type ServerSession ¶
type ServerSession struct {
// contains filtered or unexported fields
}
ServerSession tcp svc session
func (ServerSession) AddNodes ¶
func (sf ServerSession) AddNodes(nodes ...*NodeRegister)
AddNodes 增加节点
func (ServerSession) Debug ¶
func (sf ServerSession) Debug(format string, v ...interface{})
Debug Log DEBUG level message.
func (ServerSession) DeleteNode ¶
func (sf ServerSession) DeleteNode(slaveID byte)
DeleteNode 删除一个节点
func (ServerSession) Error ¶
func (sf ServerSession) Error(format string, v ...interface{})
Error Log ERROR level message.
func (ServerSession) GetNode ¶
func (sf ServerSession) GetNode(slaveID byte) (*NodeRegister, error)
GetNode 获取一个节点
func (ServerSession) GetNodeList ¶
func (sf ServerSession) GetNodeList() []*NodeRegister
GetNodeList 获取节点列表
func (*ServerSession) LogMode ¶
func (sf *ServerSession) LogMode(enable bool)
LogMode set enable or disable log output when you has set logger
func (ServerSession) Range ¶
func (sf ServerSession) Range(f func(slaveID byte, node *NodeRegister) bool)
Range 扫描节点 same as sync map range
func (ServerSession) RegisterFunctionHandler ¶
func (sf ServerSession) RegisterFunctionHandler(funcCode uint8, function FunctionHandler)
RegisterFunctionHandler 注册回调函数
func (*ServerSession) SetLogProvider ¶
func (sf *ServerSession) SetLogProvider(p LogProvider)
SetLogProvider overwrite log provider
func (*ServerSession) SetReadTimeout ¶
func (sf *ServerSession) SetReadTimeout(t time.Duration)
SetReadTimeout set read timeout
func (*ServerSession) SetWriteTimeout ¶
func (sf *ServerSession) SetWriteTimeout(t time.Duration)
SetWriteTimeout set write timeout
type TCPClientProvider ¶
type TCPClientProvider struct { Address string // Connect & Read timeout Timeout time.Duration // contains filtered or unexported fields }
TCPClientProvider implements ClientProvider interface.
func NewTCPClientProvider ¶
func NewTCPClientProvider(address string) *TCPClientProvider
NewTCPClientProvider allocates a new TCPClientProvider.
func (*TCPClientProvider) Close ¶
func (sf *TCPClientProvider) Close() error
Close closes current connection.
func (*TCPClientProvider) Connect ¶
func (sf *TCPClientProvider) Connect() error
Connect establishes a new connection to the address in Address. Connect and Close are exported so that multiple requests can be done with one session
func (TCPClientProvider) Debug ¶
func (sf TCPClientProvider) Debug(format string, v ...interface{})
Debug Log DEBUG level message.
func (TCPClientProvider) Error ¶
func (sf TCPClientProvider) Error(format string, v ...interface{})
Error Log ERROR level message.
func (*TCPClientProvider) IsConnected ¶
func (sf *TCPClientProvider) IsConnected() bool
IsConnected returns a bool signifying whether the client is connected or not.
func (*TCPClientProvider) LogMode ¶
func (sf *TCPClientProvider) LogMode(enable bool)
LogMode set enable or disable log output when you has set logger
func (*TCPClientProvider) Send ¶
func (sf *TCPClientProvider) Send(slaveID byte, request ProtocolDataUnit) (ProtocolDataUnit, error)
Send the request to tcp and get the response
func (*TCPClientProvider) SendFrame ¶
func (sf *TCPClientProvider) SendFrame(aduRequest []byte, readLen int) (aduResponse []byte, err error)
func (*TCPClientProvider) SendPdu ¶
func (sf *TCPClientProvider) SendPdu(slaveID byte, pduRequest []byte) ([]byte, error)
SendPdu send pdu request to the remote svc
func (*TCPClientProvider) SendRawFrame ¶
func (sf *TCPClientProvider) SendRawFrame(aduRequest []byte) (aduResponse []byte, err error)
SendRawFrame send raw adu request frame
func (*TCPClientProvider) SetAutoReconnect ¶
func (sf *TCPClientProvider) SetAutoReconnect(cnt byte)
SetAutoReconnect set auto reconnect retry count
func (*TCPClientProvider) SetLogProvider ¶
func (sf *TCPClientProvider) SetLogProvider(p LogProvider)
SetLogProvider overwrite log provider
type TCPServer ¶
type TCPServer struct {
// contains filtered or unexported fields
}
TCPServer modbus tcp svc
func NewTCPServer ¶
func NewTCPServer() *TCPServer
NewTCPServer the modbus svc listening on "address:port".
func (TCPServer) Debug ¶
func (sf TCPServer) Debug(format string, v ...interface{})
Debug Log DEBUG level message.
func (TCPServer) Error ¶
func (sf TCPServer) Error(format string, v ...interface{})
Error Log ERROR level message.
func (TCPServer) GetNode ¶
func (sf TCPServer) GetNode(slaveID byte) (*NodeRegister, error)
GetNode 获取一个节点
func (*TCPServer) ListenAndServe ¶
ListenAndServe 服务
func (*TCPServer) LogMode ¶
func (sf *TCPServer) LogMode(enable bool)
LogMode set enable or disable log output when you has set logger
func (TCPServer) Range ¶
func (sf TCPServer) Range(f func(slaveID byte, node *NodeRegister) bool)
Range 扫描节点 same as sync map range
func (TCPServer) RegisterFunctionHandler ¶
func (sf TCPServer) RegisterFunctionHandler(funcCode uint8, function FunctionHandler)
RegisterFunctionHandler 注册回调函数
func (*TCPServer) SetLogProvider ¶
func (sf *TCPServer) SetLogProvider(p LogProvider)
SetLogProvider overwrite log provider
func (*TCPServer) SetReadTimeout ¶
SetReadTimeout set read timeout
func (*TCPServer) SetWriteTimeout ¶
SetWriteTimeout set write timeout
type TCPServerSpecial ¶
type TCPServerSpecial struct { ServerSession TLSConfig *tls.Config // contains filtered or unexported fields }
TCPServerSpecial modbus tcp svc special
func NewTCPServerSpecial ¶
func NewTCPServerSpecial() *TCPServerSpecial
NewTCPServerSpecial new tcp svc special
func (TCPServerSpecial) AddNodes ¶
func (sf TCPServerSpecial) AddNodes(nodes ...*NodeRegister)
AddNodes 增加节点
func (*TCPServerSpecial) AddRemoteServer ¶
func (sf *TCPServerSpecial) AddRemoteServer(server string) error
AddRemoteServer adds a broker URI to the list of brokers to be used. The format should be scheme://host:port Default values for hostname is "127.0.0.1", for schema is "tcp://". An example broker URI would look like: tcp://foobar.com:1204
func (TCPServerSpecial) Debug ¶
func (sf TCPServerSpecial) Debug(format string, v ...interface{})
Debug Log DEBUG level message.
func (TCPServerSpecial) DeleteAllNode ¶
func (sf TCPServerSpecial) DeleteAllNode()
DeleteAllNode 删除所有节点
func (TCPServerSpecial) DeleteNode ¶
func (sf TCPServerSpecial) DeleteNode(slaveID byte)
DeleteNode 删除一个节点
func (*TCPServerSpecial) EnableAutoReconnect ¶
func (sf *TCPServerSpecial) EnableAutoReconnect(b bool)
EnableAutoReconnect enable auto reconnect
func (TCPServerSpecial) Error ¶
func (sf TCPServerSpecial) Error(format string, v ...interface{})
Error Log ERROR level message.
func (TCPServerSpecial) GetNode ¶
func (sf TCPServerSpecial) GetNode(slaveID byte) (*NodeRegister, error)
GetNode 获取一个节点
func (TCPServerSpecial) GetNodeList ¶
func (sf TCPServerSpecial) GetNodeList() []*NodeRegister
GetNodeList 获取节点列表
func (*TCPServerSpecial) IsClosed ¶
func (sf *TCPServerSpecial) IsClosed() bool
IsClosed check svc is closed
func (*TCPServerSpecial) IsConnected ¶
func (sf *TCPServerSpecial) IsConnected() bool
IsConnected check connect is online
func (*TCPServerSpecial) LogMode ¶
func (sf *TCPServerSpecial) LogMode(enable bool)
LogMode set enable or disable log output when you has set logger
func (TCPServerSpecial) Range ¶
func (sf TCPServerSpecial) Range(f func(slaveID byte, node *NodeRegister) bool)
Range 扫描节点 same as sync map range
func (TCPServerSpecial) RegisterFunctionHandler ¶
func (sf TCPServerSpecial) RegisterFunctionHandler(funcCode uint8, function FunctionHandler)
RegisterFunctionHandler 注册回调函数
func (*TCPServerSpecial) SetConnectTimeout ¶
func (sf *TCPServerSpecial) SetConnectTimeout(t time.Duration)
SetConnectTimeout set tcp connect the host timeout
func (*TCPServerSpecial) SetConnectionLostHandler ¶
func (sf *TCPServerSpecial) SetConnectionLostHandler(f OnConnectionLostHandler)
SetConnectionLostHandler set connection lost handler
func (*TCPServerSpecial) SetKeepAlive ¶
func (sf *TCPServerSpecial) SetKeepAlive(b bool, t time.Duration, f OnKeepAliveHandler)
SetKeepAlive set keep alive enable, alive timex and handler
func (*TCPServerSpecial) SetLogProvider ¶
func (sf *TCPServerSpecial) SetLogProvider(p LogProvider)
SetLogProvider overwrite log provider
func (*TCPServerSpecial) SetOnConnectHandler ¶
func (sf *TCPServerSpecial) SetOnConnectHandler(f OnConnectHandler)
SetOnConnectHandler set on connect handler
func (*TCPServerSpecial) SetReconnectInterval ¶
func (sf *TCPServerSpecial) SetReconnectInterval(t time.Duration)
SetReconnectInterval set tcp reconnect the host interval when connect failed after try
func (*TCPServerSpecial) SetTLSConfig ¶
func (sf *TCPServerSpecial) SetTLSConfig(t *tls.Config)
SetTLSConfig set tls config
func (*TCPServerSpecial) Start ¶
func (sf *TCPServerSpecial) Start() error
Start start the svc,and return quickly,if it nil,the svc will connecting background,other failed
func (*TCPServerSpecial) UnderlyingConn ¶
func (sf *TCPServerSpecial) UnderlyingConn() net.Conn
UnderlyingConn got underlying tcp conn