mbmaster

package module
v0.0.0-...-a248e70 Latest Latest
Warning

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

Go to latest
Published: Apr 20, 2022 License: MIT Imports: 5 Imported by: 0

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

This section is empty.

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 Query

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

Query

Types

type Master

type Master struct {
	Comport  string
	BaudRate int
	DataBits int
	StopBits int
	Parity   string
	Timeout  time.Duration
}

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