Giter Site home page Giter Site logo

modbus-orm's Introduction

ModbusORM

Object Relational Mapping (ORM) for Modbus

What is ModbusORM

ModbusORM is a golang package allows you to read/write Modbus data by struct with tag (morm).

Usage

  • Define the points
    // modbusorm.Point is a map with string key and modbusorm.PointDetails value.
    // key is the point name, which will be used in tag (morm)
    point := modbusorm.Point{
    	"voltage": modbusorm.PointDetails{
    		// Address of this point.
    		Addr: 100,
    		// Quantity of this poiont.
    		Quantity: 1,
    		// Coefficient of this point. Default 1.
    		// For example:
    		//      if read 10 from modbus server,
    		//      and coefficient is 0.1,
    		//      then you will get 1 in result.
    		Coefficient: 0.1,
    		// Data type of this point
    		//      U16, S16, U32, S32
    		DataType: modbusorm.PointDataTypeU16,
    	},
    }
  • Define a struct with morm tag.
    type Data struct {
        Voltage     *float64             `morm:"voltage"`
        Temperature float64              `morm:"temperature"`
        Star        []float64            `morm:"star"`
        Origin      modbusorm.OriginByte `morm:"origin"`
        Word        string               `morm:"word"`
        Unknown     *float64             `morm:"unkonwn"`
    }
  • Read/Write with your modbus server.
    // new
    conn := modbusorm.NewModbusTCP(
    	// Host of modbus server.
    	"localhost",
    	// Port of modbus server.
    	1502,
    	// Point define before.
    	point,
    	// Block mode setting. Default false.
    	//  With block mode, ModbusORM will try to read data by block,
    	//  rather than by single point.
    	//  If set to true, two more parameters is avaliable.
    	modbusorm.WithBlock(true),
    	// Max block size. Default 100.
    	//  Only work with block mode.
    	modbusorm.WithMaxBlockSize(100),
    	// Max gap in block. Default 10.
    	//  Only work with block mode.
    	modbusorm.WithMaxGapInBlock(10),
    	// timeout setting.
    	modbusorm.WithTimeout(10*time.Second),
    	// max open connections in connection pool.
    	modbusorm.WithMaxOpenConns(3),
    	// max connection lifetime in connection pool.
    	modbusorm.WithConnMaxLifetime(30*time.Minute),
    )
    // connect
    conn.Conn()
    // read
    data := &Data{}
    conn.GetValues(context.Background(), data)
  • See more details in _example

Demo

  • Modbus TCP
    • go to example folder: cd _example/modbus_tcp
    • start a demo server: go run server.go
    • start a demo client: go run client.go

TODOs

  • README
  • Modbus RTU
  • Example
  • More data type
  • Logger

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.