# README
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
- go to example folder:
TODOs
- README
- Modbus RTU
- Example
- More data type
- Logger
# Functions
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
WithBaudRate Set the baud rate of the modbus RTU.
WithBlock Set the read by block or not
If the block is true, ModbusORM will read by block,
otherwise, ModbusORM will read by single point.
WithComAddr Set the com address of the modbus RTU.
WithConnMaxLifetime Set the max connection lifetime of the modbus TCP.
WithDataBits Set the data bits of the modbus RTU.
WithHost Set the host of the modbus TCP.
WithMaxBlockSize Set the max block size of the modbus.
WithMaxGapInBlock Set the max gap in block of the modbus.
WithMaxOpenConns Set the max open connections of the modbus TCP.
WithMaxQuantity Set the max quantity of the modbus.
WithParity Set the parity of the modbus RTU.
WithPort Set the port of the modbus TCP.
WithSlaveID Set the slave id of the modbus.
WithStopBits Set the stop bits of the modbus RTU.
WithTimeout Set the timeout of the modbus.
# Constants
No description provided by the author
No description provided by the author
high byte first.
default.
low byte first.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
# Variables
No description provided by the author
No description provided by the author
# Structs
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
# Type aliases
No description provided by the author
No description provided by the author
OrderType order type.
OriginByte the origin byte.
Point point table.
PointDataType point data type.