mbmaster

package module
v0.0.0-...-1956d2c Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 13, 2026 License: MIT Imports: 6 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Simple modbusRTU master tool

Quick start

install

go get -u github.com/ka1hung/mbmaster

simple example

package main

import (
    "log"
    "time"

    "github.com/ka1hung/mbmaster"
)

func main() {
    mb := mbmaster.NewMaster("COM3", 9600, time.Second)
    // read
    log.Println(mb.ReadCoil(1, 0, 11))   //func1
    log.Println(mb.ReadCoilIn(1, 0, 11)) //func2
    log.Println(mb.ReadReg(1, 0, 11))    //func3
    log.Println(mb.ReadRegIn(1, 1, 11))  //func4

    // write
    log.Println(mb.WriteCoil(1, 120, true))                       //func5
    log.Println(mb.WriteCoils(1, 122, []bool{true, false, true})) //func15
    log.Println(mb.WriteReg(1, 0, 123))                           //func6
    log.Println(mb.WriteRegs(1, 1, []uint16{1, 2, 3}))            //func16
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
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 CrcAppend

func CrcAppend(data []byte) []byte

CrcAppend return []byte whitch append crc16 at last of the slice

func CrcCheck

func CrcCheck(data []byte) bool

CrcCheck return checking result

func ExtractFrame

func ExtractFrame(buf, req []byte) ([]byte, bool)

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

func Query(m *Master, data []byte) ([]byte, error)

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

func ResponseLen(req []byte) int

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 NewMaster

func NewMaster(com string, br int, timeout time.Duration) *Master

NewMaster creates a new Modbus Master config.

func (*Master) ReadCoil

func (m *Master) ReadCoil(id uint8, addr uint16, leng uint16) ([]bool, error)

ReadCoil mdbus function 1 query and return []uint16

func (*Master) ReadCoilIn

func (m *Master) ReadCoilIn(id uint8, addr uint16, leng uint16) ([]bool, error)

ReadCoilIn mdbus function 2 query and return []uint16

func (*Master) ReadReg

func (m *Master) ReadReg(id uint8, addr uint16, leng uint16) ([]uint16, error)

ReadReg mdbus function 3 query and return []uint16

func (*Master) ReadRegIn

func (m *Master) ReadRegIn(id uint8, addr uint16, leng uint16) ([]uint16, error)

ReadRegIn mdbus function 4 query and return []uint16

func (*Master) WriteCoil

func (m *Master) WriteCoil(id uint8, addr uint16, data bool) error

WriteCoil mdbus function 5 query and return []uint16

func (*Master) WriteCoils

func (m *Master) WriteCoils(id uint8, addr uint16, data []bool) error

WriteCoils mdbus function 15(0x0f) query and return []uint16

func (*Master) WriteReg

func (m *Master) WriteReg(id uint8, addr uint16, data uint16) error

WriteReg mdbus function 6 query and return []uint16

func (*Master) WriteRegs

func (m *Master) WriteRegs(id uint8, addr uint16, data []uint16) error

WriteRegs mdbus function 16(0x10) query and return []uint16

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址