Documentation
¶
Index ¶
- Variables
- func CrcAppend(data []byte) []byte
- func CrcCheck(data []byte) bool
- func ExtractFrame(buf, req []byte) ([]byte, bool)
- func Query(m *Master, data []byte) ([]byte, error)
- func ResponseLen(req []byte) int
- type Master
- func (m *Master) ReadCoil(id uint8, addr uint16, leng uint16) ([]bool, error)
- func (m *Master) ReadCoilIn(id uint8, addr uint16, leng uint16) ([]bool, error)
- func (m *Master) ReadReg(id uint8, addr uint16, leng uint16) ([]uint16, error)
- func (m *Master) ReadRegIn(id uint8, addr uint16, leng uint16) ([]uint16, error)
- func (m *Master) WriteCoil(id uint8, addr uint16, data bool) error
- func (m *Master) WriteCoils(id uint8, addr uint16, data []bool) error
- func (m *Master) WriteReg(id uint8, addr uint16, data uint16) error
- func (m *Master) WriteRegs(id uint8, addr uint16, data []uint16) error
Constants ¶
This section is empty.
Variables ¶
var ErrCRC = errors.New("CRC Error")
ErrCRC is returned (wrapped, with the raw bytes) when no valid response frame could be found in the bytes received from the slave.
Functions ¶
func ExtractFrame ¶
ExtractFrame searches buf for a valid RTU response to req (request without CRC) and returns a copy of it, CRC included. Noise bytes before and after the frame (e.g. an RS-485 driver glitch when the transceiver releases the bus) are ignored.
A normal response must match the slave id, function code, the expected byte count / echoed fields, and the CRC; an exception response must match id, fc|0x80 and the CRC. Requiring all of these makes it practically impossible for line noise to be mistaken for a frame.
func Query ¶
Query Query sends an RTU request (data = request without CRC) and returns the response frame with CRC. Noise bytes before or after the response are discarded; see ExtractFrame.
func ResponseLen ¶
ResponseLen returns the length (including CRC) of a normal RTU response to req, where req is the request frame without CRC. It returns 0 when the function code is not one the length can be derived for.
Types ¶
type Master ¶
type Master struct {
Comport string
BaudRate int
DataBits int
StopBits int
Parity string
Timeout time.Duration
// OnNoise, if set, is called when a valid response was received together
// with extra noise bytes (raw = everything read, frame = the valid frame).
// The query still succeeds; this is for diagnosing RS-485 wiring issues.
OnNoise func(raw, frame []byte)
}
Master Modbus Master config
func (*Master) ReadCoilIn ¶
ReadCoilIn mdbus function 2 query and return []uint16
func (*Master) WriteCoils ¶
WriteCoils mdbus function 15(0x0f) query and return []uint16