Documentation
¶
Index ¶
- Constants
- Variables
- func CheckRtuCrc(packet []byte) error
- func DecodeASCIIByte(data []byte) (byte, []byte, error)
- func DecodeASCIIByteEnd(data []byte) (byte, []byte, error)
- func HexDump(data []byte) string
- func PutUint16Array(value ...uint16) []byte
- func RtuCrc(buf []byte) uint16
- func RtuEncode(id byte, pdu PDU) ([]byte, error)
- func Uint16Array(data []byte) []uint16
- type ASCIIADU
- type Client
- type FuncReadHoldingRegisterResponse
- type FuncReadHoldingRegistersRequest
- type FuncWriteMultipleRegisterRequest
- type FunctionCode
- type IoSim
- type IoSimPort
- type Modbus
- type PDU
- type Reg
- type RegChange
- type Regs
- type RtuADU
- type Server
Constants ¶
const ( // Bit access FuncCodeReadDiscreteInputs FunctionCode = 2 FuncCodeReadCoils = 1 FuncCodeWriteSingleCoil = 5 FuncCodeWriteMultipleCoils = 15 // 16-bit access FuncCodeReadInputRegisters = 4 FuncCodeReadHoldingRegisters = 3 FuncCodeWriteSingleRegister = 6 FuncCodeWriteMultipleRegisters = 16 FuncCodeReadWriteMultipleRegisters = 23 FuncCodeMaskWriteRegister = 22 FuncCodeReadFIFOQueue = 24 )
Defined valid function codes
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
Functions ¶
func CheckRtuCrc ¶ added in v0.0.2
CheckRtuCrc returns error if CRC fails
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 PutUint16Array ¶ added in v0.0.2
PutUint16Array creates a sequence of uint16 data.
func Uint16Array ¶ added in v0.0.2
Uint16Array unpacks 16 bit data values from a buffer (in big endian format)
Types ¶
type ASCIIADU ¶ added in v0.0.2
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 ¶ added in v0.0.2
DecodeFunctionData extracts the function data from the PDU
type Client ¶ added in v0.0.2
type Client struct {
// contains filtered or unexported fields
}
Client defines a Modbus client (master)
func NewClient ¶ added in v0.0.2
func NewClient(port io.ReadWriter) *Client
NewClient is used to create a new modbus client
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 IoSim ¶ added in v0.0.2
type IoSim struct {
// contains filtered or unexported fields
}
IoSim simulates a serial port and provides a io.ReadWriter for both ends
func (*IoSim) GetA ¶ added in v0.0.2
func (is *IoSim) GetA() io.ReadWriter
GetA returns the A port from a IoSim
func (*IoSim) GetB ¶ added in v0.0.2
func (is *IoSim) GetB() io.ReadWriter
GetB returns the B port from a IoSim
type IoSimPort ¶ added in v0.0.2
type IoSimPort struct {
// contains filtered or unexported fields
}
IoSimPort one end of a IoSim
func NewIoSimPort ¶ added in v0.0.2
NewIoSimPort returns a new port of an IoSim
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 ReadHoldingRegs ¶ added in v0.0.2
ReadHoldingRegs creates a PDU to read a holding regs
func (*PDU) ProcessRequest ¶ added in v0.0.2
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 ¶ added in v0.0.2
RespReadBits reads coils and discrete inputs from a response PDU.
type RegChange ¶ added in v0.0.2
RegChange is a type that describes a modbus register change the address, old value and new value of the register are provided. This allows application software to take action when things change.
type Regs ¶ added in v0.0.2
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 ¶ added in v0.0.2
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 ¶ added in v0.0.2
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) ReadCoil ¶ added in v0.0.2
ReadCoil gets a coil value (can also be used for discrete inputs)
type Server ¶ added in v0.0.2
type Server struct {
Regs Regs
// 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.