Giter Club home page Giter Club logo

go-modbus's Introduction

go-modbus

Build Status GoDoc Coverage Status

DONT USE IT: This was a proof-of-concept!

a free Modbus library for Go.

Usage

Modbus Master (Client)

package main

import (
  "fmt"
  "github.com/flosse/go-modbus"
)

func main(){
  master := modbus.NewTcpClient("127.0.0.1", 502)
}

High Level API

/* read-only */
di := master.DiscreteInput(7)
state, err := di.Test()

/* read-write */
coil := master.Coil(2)
state, err = coil.Test()
err = coil.Set()
err = coil.Clear()
err = coil.Toggle()

/* read-only */
roRegister := master.InputRegister(0x2000)
value, err := roRegister.Read()

/* multiple ro registers */
multRoRegister := master.InputRegisters(0x1000,7)
values, err    := roRegister.Read()
myString, err  := multRoRegister.ReadString()

/* read-write */
register   := master.HoldingRegister(0x0900)
value, err := register.Read()
err = register.Write(0x435)

/* multiple rw registers */
multRwRegisters := master.HoldingRegisters(0x9000, 3)
values, err     := multRwRegisters.Read()
aString, err    := multRwRegisters.ReadString()
err := multRwRegisters.Write(uint16{3,2,1})
err =  multRwRegisters.WriteString("foo")

Low Level API

/* Bit access */

// read three read-only bits
res, err := master.ReadDiscreteInputs(0x0800, 3)
// res could be [true, false, false]

// read 5 read-write bits
res, err = master.ReadCoils(0x02, 2)
// res could be [false, true]

// set the coil at address 0x0734
err = master.WriteSingleCoil(0x734, true)

// set/clear multiple coils at address 0x0002
err = master.WriteMultipleCoils(2, []bool{false, true, true})

/* 16 bits access */

// read three read-only registers
res, err = master.ReadInputRegisters(0x12, 3)
// res could be [334, 912, 0]

// read two read-write registers
res, err = master.ReadHoldingRegisters(0x00, 2)
// res could be [9, 42]

// write a value to a single register
err = master.WriteSingleRegister(0x07, 9923)

// write values to multiple registers
err = master.WriteMultipleRegisters(0x03, []uint16{9,0,66})

// read two and write three values within one transaction
res, err = master.ReadWriteMultipleRegisters(0x0065, 2, 0x0800, []uint16{0,7,33})
// res could be [0, 88]

Run Tests

go get github.com/smartystreets/goconvey
go test

or run

$GOPATH/bin/goconvey

and open http://localhost:8080 in your browser

License

This library is licensed under the MIT license

Credits

This library is inspired by this modbus library.

go-modbus's People

Contributors

flosse avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.