Improve modbus configuration, add sunspec and float32 swapped readings (#184)
This commit is contained in:
parent
22d5d9f7b1
commit
71a4afa69a
7 changed files with 172 additions and 127 deletions
|
|
@ -348,6 +348,8 @@ Supported meter models are the same as supported by [MBMD](https://github.com/vo
|
|||
|
||||
Use `value` to define the value to read from the device. All values that are supported by [MBMD](https://github.com/volkszaehler/mbmd/blob/master/meters/measurements.go#L28) are pre-configured.
|
||||
|
||||
In case of SunSpec-compatible inverters, values can also be configured in the form of `model:[block:]point` according to SunSpec definition. For example, a 3-phase inverter's DC power of the 2nd string would be configurable as `103:2:W`.
|
||||
|
||||
#### Manual configuration
|
||||
|
||||
If the Modbus device is not supported by MBMD, the Modbus register can also be manually configured:
|
||||
|
|
@ -359,7 +361,7 @@ register:
|
|||
address: 40070
|
||||
length: 2 # read length in words
|
||||
type: holding # holding or input
|
||||
decode: int32 # int16|32|64, uint16|32|64, float32|64 and u|int32s
|
||||
decode: int32 # int16|32|64, uint16|32|64, float32|64 and u|int32s + float32s
|
||||
scale: -1 # floating point factor applied to result, e.g. for kW to W conversion
|
||||
```
|
||||
|
||||
|
|
|
|||
|
|
@ -4,8 +4,8 @@ import (
|
|||
"fmt"
|
||||
|
||||
"github.com/andig/evcc/api"
|
||||
"github.com/andig/evcc/provider/modbus"
|
||||
"github.com/andig/evcc/util"
|
||||
"github.com/andig/evcc/util/modbus"
|
||||
gridx "github.com/grid-x/modbus"
|
||||
"github.com/volkszaehler/mbmd/meters"
|
||||
)
|
||||
|
|
|
|||
2
go.mod
2
go.mod
|
|
@ -34,7 +34,7 @@ require (
|
|||
github.com/spf13/viper v1.6.3
|
||||
github.com/tcnksm/go-latest v0.0.0-20170313132115-e3007ae9052e
|
||||
github.com/technoweenie/multipartstreamer v1.0.1 // indirect
|
||||
github.com/volkszaehler/mbmd v0.0.0-20200505114042-9732bf1ad846
|
||||
github.com/volkszaehler/mbmd v0.0.0-20200524120609-c683601720ed
|
||||
golang.org/x/crypto v0.0.0-20200429183012-4b2356b1ed79 // indirect
|
||||
golang.org/x/net v0.0.0-20200505041828-1ed23360d12c
|
||||
golang.org/x/sys v0.0.0-20200501145240-bc7a7d42d5c3 // indirect
|
||||
|
|
|
|||
4
go.sum
4
go.sum
|
|
@ -251,8 +251,8 @@ github.com/technoweenie/multipartstreamer v1.0.1 h1:XRztA5MXiR1TIRHxH2uNxXxaIkKQ
|
|||
github.com/technoweenie/multipartstreamer v1.0.1/go.mod h1:jNVxdtShOxzAsukZwTSw6MDx5eUJoiEBsSvzDU9uzog=
|
||||
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
|
||||
github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc=
|
||||
github.com/volkszaehler/mbmd v0.0.0-20200505114042-9732bf1ad846 h1:PwtXvfj0GBgSOFHSxoV+YB/Ii0TV12W17YE844R9w/o=
|
||||
github.com/volkszaehler/mbmd v0.0.0-20200505114042-9732bf1ad846/go.mod h1:MmglPXoZJOT0r+tWwAjaaRWmuopdQOq2tWxbL9zvuxE=
|
||||
github.com/volkszaehler/mbmd v0.0.0-20200524120609-c683601720ed h1:oC2QZlpY+AbEPn/8TWvO4FiXrk9uHhCyT24b6wwa/JA=
|
||||
github.com/volkszaehler/mbmd v0.0.0-20200524120609-c683601720ed/go.mod h1:MmglPXoZJOT0r+tWwAjaaRWmuopdQOq2tWxbL9zvuxE=
|
||||
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU=
|
||||
github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q=
|
||||
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
|
||||
|
|
|
|||
|
|
@ -2,9 +2,8 @@ package meter
|
|||
|
||||
import (
|
||||
"github.com/andig/evcc/api"
|
||||
"github.com/andig/evcc/provider"
|
||||
"github.com/andig/evcc/provider/modbus"
|
||||
"github.com/andig/evcc/util"
|
||||
"github.com/andig/evcc/util/modbus"
|
||||
"github.com/volkszaehler/mbmd/meters"
|
||||
"github.com/volkszaehler/mbmd/meters/rs485"
|
||||
"github.com/volkszaehler/mbmd/meters/sunspec"
|
||||
|
|
@ -16,15 +15,15 @@ type Modbus struct {
|
|||
conn meters.Connection
|
||||
device meters.Device
|
||||
slaveID uint8
|
||||
opPower rs485.Operation
|
||||
opEnergy rs485.Operation
|
||||
opPower modbus.Operation
|
||||
opEnergy modbus.Operation
|
||||
}
|
||||
|
||||
// NewModbusFromConfig creates api.Meter from config
|
||||
func NewModbusFromConfig(log *util.Logger, other map[string]interface{}) api.Meter {
|
||||
cc := struct {
|
||||
provider.ModbusSettings `mapstructure:",squash"`
|
||||
Power, Energy string
|
||||
modbus.Settings `mapstructure:",squash"`
|
||||
Power, Energy string
|
||||
}{}
|
||||
util.DecodeOther(log, other, &cc)
|
||||
|
||||
|
|
@ -66,36 +65,14 @@ func NewModbusFromConfig(log *util.Logger, other map[string]interface{}) api.Met
|
|||
cc.Power = "Power"
|
||||
}
|
||||
|
||||
powerM, err := meters.MeasurementString(cc.Power)
|
||||
if err != nil {
|
||||
if err := modbus.ParseOperation(device, cc.Power, &m.opPower); err != nil {
|
||||
log.FATAL.Fatalf("invalid measurement for power: %s", cc.Power)
|
||||
}
|
||||
|
||||
// for RS485 check if producer supports the measurement
|
||||
m.opPower = rs485.Operation{IEC61850: powerM}
|
||||
if dev, ok := device.(*rs485.RS485); ok {
|
||||
m.opPower = modbus.RS485FindDeviceOp(dev, powerM)
|
||||
|
||||
if m.opPower.IEC61850 == 0 {
|
||||
log.FATAL.Fatalf("unsupported measurement for power: %s", cc.Power)
|
||||
}
|
||||
}
|
||||
|
||||
// decorate energy reading
|
||||
if cc.Energy != "" {
|
||||
energyM, err := meters.MeasurementString(cc.Energy)
|
||||
if err != nil {
|
||||
log.FATAL.Fatalf("invalid measurement for energy: %s", cc.Energy)
|
||||
}
|
||||
|
||||
// for RS485 check if producer supports the measurement
|
||||
m.opEnergy = rs485.Operation{IEC61850: energyM}
|
||||
if dev, ok := device.(*rs485.RS485); ok {
|
||||
m.opEnergy = modbus.RS485FindDeviceOp(dev, energyM)
|
||||
|
||||
if m.opEnergy.IEC61850 == 0 {
|
||||
log.FATAL.Fatalf("unsupported measurement for energy: %s", cc.Energy)
|
||||
}
|
||||
if err := modbus.ParseOperation(device, cc.Energy, &m.opEnergy); err != nil {
|
||||
log.FATAL.Fatalf("invalid measurement for energy: %s", cc.Power)
|
||||
}
|
||||
|
||||
return &ModbusEnergy{m}
|
||||
|
|
@ -105,18 +82,27 @@ func NewModbusFromConfig(log *util.Logger, other map[string]interface{}) api.Met
|
|||
}
|
||||
|
||||
// floatGetter executes configured modbus read operation and implements provider.FloatGetter
|
||||
func (m *Modbus) floatGetter(op rs485.Operation) (float64, error) {
|
||||
func (m *Modbus) floatGetter(op modbus.Operation) (float64, error) {
|
||||
m.conn.Slave(m.slaveID)
|
||||
|
||||
var res meters.MeasurementResult
|
||||
var err error
|
||||
|
||||
if dev, ok := m.device.(*rs485.RS485); ok {
|
||||
res, err = dev.QueryOp(m.conn.ModbusClient(), op)
|
||||
res, err = dev.QueryOp(m.conn.ModbusClient(), op.MBMD)
|
||||
}
|
||||
|
||||
if dev, ok := m.device.(*sunspec.SunSpec); ok {
|
||||
res, err = dev.QueryOp(m.conn.ModbusClient(), op.IEC61850)
|
||||
if op.MBMD.IEC61850 != 0 {
|
||||
res, err = dev.QueryOp(m.conn.ModbusClient(), op.MBMD.IEC61850)
|
||||
} else {
|
||||
res, err = dev.QueryPoint(
|
||||
m.conn.ModbusClient(),
|
||||
op.SunSpec.Model,
|
||||
op.SunSpec.Block,
|
||||
op.SunSpec.Point,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
if err == nil {
|
||||
|
|
|
|||
|
|
@ -4,8 +4,8 @@ import (
|
|||
"fmt"
|
||||
"math"
|
||||
|
||||
"github.com/andig/evcc/provider/modbus"
|
||||
"github.com/andig/evcc/util"
|
||||
"github.com/andig/evcc/util/modbus"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/volkszaehler/mbmd/meters"
|
||||
"github.com/volkszaehler/mbmd/meters/rs485"
|
||||
|
|
@ -18,23 +18,17 @@ type Modbus struct {
|
|||
conn meters.Connection
|
||||
device meters.Device
|
||||
slaveID uint8
|
||||
op rs485.Operation
|
||||
op modbus.Operation
|
||||
scale float64
|
||||
}
|
||||
|
||||
// ModbusSettings combine physical modbus configuration and MBMD model
|
||||
type ModbusSettings struct {
|
||||
Model string
|
||||
modbus.Connection `mapstructure:",squash"`
|
||||
}
|
||||
|
||||
// NewModbusFromConfig creates Modbus plugin
|
||||
func NewModbusFromConfig(log *util.Logger, typ string, other map[string]interface{}) *Modbus {
|
||||
cc := struct {
|
||||
ModbusSettings `mapstructure:",squash"`
|
||||
Register modbus.Register
|
||||
Value string
|
||||
Scale float64
|
||||
modbus.Settings `mapstructure:",squash"`
|
||||
Register modbus.Register
|
||||
Value string
|
||||
Scale float64
|
||||
}{}
|
||||
util.DecodeOther(log, other, &cc)
|
||||
|
||||
|
|
@ -50,7 +44,7 @@ func NewModbusFromConfig(log *util.Logger, typ string, other map[string]interfac
|
|||
|
||||
var err error
|
||||
var device meters.Device
|
||||
var op rs485.Operation
|
||||
var op modbus.Operation
|
||||
|
||||
// model configured
|
||||
if cc.Model != "" {
|
||||
|
|
@ -67,32 +61,20 @@ func NewModbusFromConfig(log *util.Logger, typ string, other map[string]interfac
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
log.FATAL.Fatal(err)
|
||||
}
|
||||
|
||||
// model + value configured
|
||||
if cc.Value != "" {
|
||||
measurement, err := meters.MeasurementString(cc.Value)
|
||||
if err != nil {
|
||||
log.FATAL.Fatalf("invalid measurement %s", cc.Value)
|
||||
}
|
||||
|
||||
// for RS485 check if producer supports the measurement
|
||||
op.IEC61850 = measurement
|
||||
if dev, ok := device.(*rs485.RS485); ok {
|
||||
op = modbus.RS485FindDeviceOp(dev, measurement)
|
||||
|
||||
if op.IEC61850 == 0 {
|
||||
log.FATAL.Fatalf("unsupported measurement value: %s", measurement)
|
||||
}
|
||||
if err := modbus.ParseOperation(device, cc.Value, &op); err != nil {
|
||||
log.FATAL.Fatalf("invalid value %s", cc.Value)
|
||||
}
|
||||
}
|
||||
|
||||
// register configured
|
||||
if cc.Register.Decode != "" {
|
||||
if op, err = modbus.RegisterOperation(cc.Register); err != nil {
|
||||
if op.MBMD, err = modbus.RegisterOperation(cc.Register); err != nil {
|
||||
log.TRACE.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
|
@ -119,29 +101,38 @@ func (m *Modbus) FloatGetter() (float64, error) {
|
|||
var err error
|
||||
|
||||
// if funccode is configured, execute the read directly
|
||||
if m.op.FuncCode != 0 {
|
||||
if op := m.op.MBMD; op.FuncCode != 0 {
|
||||
client := m.conn.ModbusClient()
|
||||
|
||||
var bytes []byte
|
||||
switch m.op.FuncCode {
|
||||
switch op.FuncCode {
|
||||
case rs485.ReadHoldingReg:
|
||||
bytes, err = client.ReadHoldingRegisters(m.op.OpCode, m.op.ReadLen)
|
||||
bytes, err = client.ReadHoldingRegisters(op.OpCode, op.ReadLen)
|
||||
case rs485.ReadInputReg:
|
||||
bytes, err = client.ReadInputRegisters(m.op.OpCode, m.op.ReadLen)
|
||||
bytes, err = client.ReadInputRegisters(op.OpCode, op.ReadLen)
|
||||
default:
|
||||
return 0, fmt.Errorf("unknown function code %d", m.op.FuncCode)
|
||||
return 0, fmt.Errorf("unknown function code %d", op.FuncCode)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return 0, errors.Wrap(err, "read failed")
|
||||
}
|
||||
|
||||
return m.scale * m.op.Transform(bytes), nil
|
||||
return m.scale * op.Transform(bytes), nil
|
||||
}
|
||||
|
||||
// if funccode is not configured, try find the reading on sunspec
|
||||
if dev, ok := m.device.(*sunspec.SunSpec); ok {
|
||||
res, err = dev.QueryOp(m.conn.ModbusClient(), m.op.IEC61850)
|
||||
if m.op.MBMD.IEC61850 != 0 {
|
||||
res, err = dev.QueryOp(m.conn.ModbusClient(), m.op.MBMD.IEC61850)
|
||||
} else {
|
||||
res, err = dev.QueryPoint(
|
||||
m.conn.ModbusClient(),
|
||||
m.op.SunSpec.Model,
|
||||
m.op.SunSpec.Block,
|
||||
m.op.SunSpec.Point,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
if err == nil {
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ package modbus
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/andig/evcc/util"
|
||||
|
|
@ -10,53 +11,10 @@ import (
|
|||
"github.com/volkszaehler/mbmd/meters/sunspec"
|
||||
)
|
||||
|
||||
// Register contains the ModBus register configuration
|
||||
type Register struct {
|
||||
Address, Length uint16
|
||||
Type string
|
||||
Decode string
|
||||
}
|
||||
|
||||
// RegisterOperation creates a read operation from a register definition
|
||||
func RegisterOperation(r Register) (rs485.Operation, error) {
|
||||
op := rs485.Operation{
|
||||
OpCode: r.Address,
|
||||
ReadLen: r.Length,
|
||||
}
|
||||
|
||||
switch strings.ToLower(r.Type) {
|
||||
case "holding":
|
||||
op.FuncCode = rs485.ReadHoldingReg
|
||||
case "input":
|
||||
op.FuncCode = rs485.ReadInputReg
|
||||
default:
|
||||
return rs485.Operation{}, fmt.Errorf("invalid register type: %s", r.Type)
|
||||
}
|
||||
|
||||
switch strings.ToLower(r.Decode) {
|
||||
case "float32", "ieee754":
|
||||
op.Transform = rs485.RTUIeee754ToFloat64
|
||||
case "float64":
|
||||
op.Transform = rs485.RTUUint64ToFloat64
|
||||
case "uint16":
|
||||
op.Transform = rs485.RTUUint16ToFloat64
|
||||
case "uint32":
|
||||
op.Transform = rs485.RTUUint32ToFloat64
|
||||
case "uint32s":
|
||||
op.Transform = rs485.RTUUint32ToFloat64Swapped
|
||||
case "uint64":
|
||||
op.Transform = rs485.RTUUint64ToFloat64
|
||||
case "int16":
|
||||
op.Transform = rs485.RTUInt16ToFloat64
|
||||
case "int32":
|
||||
op.Transform = rs485.RTUInt32ToFloat64
|
||||
case "int32s":
|
||||
op.Transform = rs485.RTUInt32ToFloat64Swapped
|
||||
default:
|
||||
return rs485.Operation{}, fmt.Errorf("invalid register decoding: %s", r.Decode)
|
||||
}
|
||||
|
||||
return op, nil
|
||||
// Settings combine physical modbus configuration and MBMD model
|
||||
type Settings struct {
|
||||
Model string
|
||||
Connection `mapstructure:",squash"`
|
||||
}
|
||||
|
||||
// Connection contains the ModBus connection configuration
|
||||
|
|
@ -129,15 +87,123 @@ func IsRS485(model string) bool {
|
|||
}
|
||||
|
||||
// RS485FindDeviceOp checks is RS485 device supports operation
|
||||
func RS485FindDeviceOp(device *rs485.RS485, measurement meters.Measurement) (op rs485.Operation) {
|
||||
func RS485FindDeviceOp(device *rs485.RS485, measurement meters.Measurement) (op rs485.Operation, err error) {
|
||||
ops := device.Producer().Produce()
|
||||
|
||||
for _, o := range ops {
|
||||
if o.IEC61850 == measurement {
|
||||
op = o
|
||||
break
|
||||
for _, op := range ops {
|
||||
if op.IEC61850 == measurement {
|
||||
return op, nil
|
||||
}
|
||||
}
|
||||
|
||||
return op
|
||||
return op, fmt.Errorf("unsupported measurement: %s", measurement.String())
|
||||
}
|
||||
|
||||
// Register contains the ModBus register configuration
|
||||
type Register struct {
|
||||
Address uint16 // Length uint16
|
||||
Type string
|
||||
Decode string
|
||||
}
|
||||
|
||||
// RegisterOperation creates a read operation from a register definition
|
||||
func RegisterOperation(r Register) (rs485.Operation, error) {
|
||||
op := rs485.Operation{
|
||||
OpCode: r.Address,
|
||||
ReadLen: 2,
|
||||
}
|
||||
|
||||
switch strings.ToLower(r.Type) {
|
||||
case "holding":
|
||||
op.FuncCode = rs485.ReadHoldingReg
|
||||
case "input":
|
||||
op.FuncCode = rs485.ReadInputReg
|
||||
default:
|
||||
return rs485.Operation{}, fmt.Errorf("invalid register type: %s", r.Type)
|
||||
}
|
||||
|
||||
switch strings.ToLower(r.Decode) {
|
||||
case "float32", "ieee754":
|
||||
op.Transform = rs485.RTUIeee754ToFloat64
|
||||
case "float32s", "ieee754s":
|
||||
op.Transform = rs485.RTUIeee754ToFloat64Swapped
|
||||
case "float64":
|
||||
op.Transform = rs485.RTUUint64ToFloat64
|
||||
op.ReadLen = 4
|
||||
case "uint16":
|
||||
op.Transform = rs485.RTUUint16ToFloat64
|
||||
op.ReadLen = 1
|
||||
case "uint32":
|
||||
op.Transform = rs485.RTUUint32ToFloat64
|
||||
case "uint32s":
|
||||
op.Transform = rs485.RTUUint32ToFloat64Swapped
|
||||
case "uint64":
|
||||
op.Transform = rs485.RTUUint64ToFloat64
|
||||
op.ReadLen = 4
|
||||
case "int16":
|
||||
op.Transform = rs485.RTUInt16ToFloat64
|
||||
op.ReadLen = 1
|
||||
case "int32":
|
||||
op.Transform = rs485.RTUInt32ToFloat64
|
||||
case "int32s":
|
||||
op.Transform = rs485.RTUInt32ToFloat64Swapped
|
||||
default:
|
||||
return rs485.Operation{}, fmt.Errorf("invalid register decoding: %s", r.Decode)
|
||||
}
|
||||
|
||||
return op, nil
|
||||
}
|
||||
|
||||
// SunSpecOperation is a sunspec modbus operation
|
||||
type SunSpecOperation struct {
|
||||
Model, Block int
|
||||
Point string
|
||||
}
|
||||
|
||||
// ParsePoint parses sunspec point from string
|
||||
func ParsePoint(selector string) (model int, block int, point string, err error) {
|
||||
err = fmt.Errorf("invalid point: %s", selector)
|
||||
|
||||
el := strings.Split(selector, ":")
|
||||
if len(el) < 2 || len(el) > 3 {
|
||||
return
|
||||
}
|
||||
|
||||
if model, err = strconv.Atoi(el[0]); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if len(el) == 3 {
|
||||
// block is the middle element
|
||||
if block, err = strconv.Atoi(el[1]); err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
point = el[len(el)-1]
|
||||
|
||||
return model, block, point, nil
|
||||
}
|
||||
|
||||
// Operation is a register-based or sunspec modbus operation
|
||||
type Operation struct {
|
||||
MBMD rs485.Operation
|
||||
SunSpec SunSpecOperation
|
||||
}
|
||||
|
||||
// ParseOperation parses an MBMD measurement or SunsSpec point definition into a modbus operation
|
||||
func ParseOperation(dev meters.Device, measurement string, op *Operation) (err error) {
|
||||
// if measurement cannot be parsed it could be SunSpec model/block/point
|
||||
if op.MBMD.IEC61850, err = meters.MeasurementString(measurement); err != nil {
|
||||
opSunSpec := &SunSpecOperation{}
|
||||
opSunSpec.Model, opSunSpec.Block, opSunSpec.Point, err = ParsePoint(measurement)
|
||||
return err
|
||||
}
|
||||
|
||||
// for RS485 check if producer supports the measurement
|
||||
if dev, ok := dev.(*rs485.RS485); ok {
|
||||
op.MBMD, err = RS485FindDeviceOp(dev, op.MBMD.IEC61850)
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue