lishuangquan1987 / modbusplus

this is base on [goburrow/modbus](<https://github.com/goburrow/modbus>)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

modbus-plus

it seems that goburrow/modbus is no longger maintained any more.So this repository is the supplement of goburrow/modbus

What Different?

  1. we can change the slaveId on every call

    for example:

    goburrow/modbus:

    ReadHoldingRegisters(address, quantity uint16) (results []byte, err error)

    lishuangquan1987/modbusplus:

    ReadHoldingRegisters(address uint16, count uint16, slaveId byte) ([]byte, error)
  2. Auto convert []byte to uint16,and the quantity is the length of value

    for example:

    goburrow/modbus:

    WriteMultipleRegisters(address, quantity uint16, value []byte) (results []byte, err error)

    lishuangquan1987/modbusplus:

    WriteMultipleRegisters(address uint16, value []uint16, slaveId byte) ([]byte, error)

Usage

import (
	"fmt"
	"time"

	"github.com/goburrow/modbus"
	"github.com/lishuangquan1987/modbusplus"
)

// Modbus RTUClientHandler/ASCIIClientHandler/TCPClientHandler, it is the same as goburrow/modbus
handler := modbus.NewRTUClientHandler("COM9")
handler.BaudRate = 9600
handler.DataBits = 8
handler.Parity = "N"
handler.StopBits = 1
handler.SlaveId = 1
handler.Timeout = 5 * time.Second

err := handler.Connect()
if err != nil {
    fmt.Printf("connect fail:%v", err)
}
defer handler.Close()

//use the modbusplus to create client
client := modbusplus.NewClient(handler)

//we can change slave id every call
for i := 0; i < 100; i++ {
    results, err := client.ReadHoldingRegisters(0x2050, 1, byte(i))
    if err != nil {
        fmt.Printf("%v", results)
    }
}

About

this is base on [goburrow/modbus](<https://github.com/goburrow/modbus>)


Languages

Language:Go 100.0%