meters

package
v0.0.0-...-33463eb Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 11, 2024 License: BSD-3-Clause Imports: 7 Imported by: 29

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNaN indicates a NaN reading result
	ErrNaN = errors.New("NaN value")

	// ErrPartiallyOpened indicates a partially opened device
	ErrPartiallyOpened = errors.New("Device partially opened")
)

Functions

func MeasurementStrings

func MeasurementStrings() []string

MeasurementStrings returns a slice of all String values of the enum

func NewASCIIClientHandler

func NewASCIIClientHandler(device string, baudrate int, comset string) *modbus.ASCIIClientHandler

NewASCIIClientHandler creates a serial line ASCII modbus handler

func NewASCIIOverTCPClientHandler

func NewASCIIOverTCPClientHandler(device string) *modbus.ASCIIOverTCPClientHandler

NewASCIIOverTCPClientHandler creates a TCP modbus handler

func NewClientHandler

func NewClientHandler(device string, baudrate int, comset string) *modbus.RTUClientHandler

NewClientHandler creates a serial line RTU modbus handler

func NewRTUOverTCPClientHandler

func NewRTUOverTCPClientHandler(device string) *modbus.RTUOverTCPClientHandler

NewRTUOverTCPClientHandler creates a RTU over TCP modbus handler

func NewTCPClientHandler

func NewTCPClientHandler(device string) *modbus.TCPClientHandler

NewTCPClientHandler creates a TCP modbus handler

Types

type ASCII

type ASCII struct {
	Client  modbus.Client
	Handler *modbus.ASCIIClientHandler
	// contains filtered or unexported fields
}

ASCII is an ASCII modbus connection

func (*ASCII) Close

func (b *ASCII) Close()

Close closes the modbus connection. This forces the modbus client to reopen the connection before the next bus operations.

func (*ASCII) ConnectDelay

func (b *ASCII) ConnectDelay(delay time.Duration)

ConnectDelay sets the the initial delay after connecting before starting communication

func (*ASCII) Logger

func (b *ASCII) Logger(l Logger)

Logger sets a logging instance for physical bus operations

func (*ASCII) ModbusClient

func (b *ASCII) ModbusClient() modbus.Client

ModbusClient returns the RTU modbus client

func (*ASCII) Slave

func (b *ASCII) Slave(deviceID uint8)

Slave sets the modbus device id for the following operations

func (*ASCII) String

func (b *ASCII) String() string

String returns the bus device

func (*ASCII) Timeout

func (b *ASCII) Timeout(timeout time.Duration) time.Duration

Timeout sets the modbus timeout

type ASCIIOverTCP

type ASCIIOverTCP struct {
	Client  modbus.Client
	Handler *modbus.ASCIIOverTCPClientHandler
	// contains filtered or unexported fields
}

ASCIIOverTCP is an ASCII encoder over a TCP modbus connection

func (*ASCIIOverTCP) Close

func (b *ASCIIOverTCP) Close()

Close closes the modbus connection. This forces the modbus client to reopen the connection before the next bus operations.

func (*ASCIIOverTCP) ConnectDelay

func (b *ASCIIOverTCP) ConnectDelay(delay time.Duration)

ConnectDelay sets the the initial delay after connecting before starting communication

func (*ASCIIOverTCP) Logger

func (b *ASCIIOverTCP) Logger(l Logger)

Logger sets a logging instance for physical bus operations

func (*ASCIIOverTCP) ModbusClient

func (b *ASCIIOverTCP) ModbusClient() modbus.Client

ModbusClient returns the TCP modbus client

func (*ASCIIOverTCP) Slave

func (b *ASCIIOverTCP) Slave(deviceID uint8)

Slave sets the modbus device id for the following operations

func (*ASCIIOverTCP) String

func (b *ASCIIOverTCP) String() string

String returns the bus connection address (TCP)

func (*ASCIIOverTCP) Timeout

func (b *ASCIIOverTCP) Timeout(timeout time.Duration) time.Duration

Timeout sets the modbus timeout

type Connection

type Connection interface {
	// ModbusClient returns the underlying modbus client
	ModbusClient() modbus.Client

	// Slave sets the modbus device id for the following operations
	Slave(deviceID uint8)

	// Timeout sets the modbus timeout
	Timeout(timeout time.Duration) time.Duration

	// ConnectDelay sets the the initial delay after connecting before starting communication
	ConnectDelay(delay time.Duration)

	// Close closes the modbus connection.
	// This forces the modbus client to reopen the connection before the next bus operations.
	Close()

	// Logger sets a logging instance for physical bus operations
	Logger(l Logger)

	// String returns the bus device (RTU) or bus connection address (TCP)
	String() string
}

Connection encapsulates a physical modbus connection, either RTU or TCP

func NewASCII

func NewASCII(device string, baudrate int, comset string) Connection

NewASCII creates a RTU modbus client

func NewASCIIOverTCP

func NewASCIIOverTCP(address string) Connection

NewASCIIOverTCP creates a TCP modbus client

func NewMock

func NewMock(address string) Connection

NewMock creates a mock modbus client

func NewRTU

func NewRTU(device string, baudrate int, comset string) Connection

NewRTU creates a RTU modbus client

func NewRTUOverTCP

func NewRTUOverTCP(address string) Connection

NewRTUOverTCP creates a TCP modbus client

func NewTCP

func NewTCP(address string) Connection

NewTCP creates a TCP modbus client

type Device

type Device interface {
	// Initialize prepares the device for usage. Any setup or initialization should be done here.
	// It requires that the client has the correct device id applied.
	Initialize(client modbus.Client) error

	// Descriptor returns the device descriptor. Since this method does not have
	// bus access the descriptor should be prepared during initialization.
	Descriptor() DeviceDescriptor

	// Probe tests if a basic register, typically VoltageL1, can be read.
	// It requires that the client has the correct device id applied.
	Probe(client modbus.Client) (MeasurementResult, error)

	// Query retrieves all registers that the device supports.
	// It requires that the client has the correct device id applied.
	Query(client modbus.Client) ([]MeasurementResult, error)
}

Device is a modbus device that can be described, probed and queried

type DeviceDescriptor

type DeviceDescriptor struct {
	Type         string
	Manufacturer string
	Model        string
	Options      string
	Version      string
	Serial       string
	SubDevice    int
}

DeviceDescriptor describes a device

type Logger

type Logger interface {
	Printf(format string, v ...interface{})
}

Logger is an injectable logger for grid-x modbus implementation

type Manager

type Manager struct {
	Conn Connection
	// contains filtered or unexported fields
}

Manager handles devices attached to a connection

func NewManager

func NewManager(conn Connection) *Manager

NewManager creates a new connection manager instance. connection managers operate devices on a connection instance

func (*Manager) Add

func (m *Manager) Add(id uint8, dev Device) error

Add adds device to the device manager at specified device id

func (*Manager) All

func (m *Manager) All(cb func(uint8, Device))

All iterates over all devices and executes the callback per device.

func (*Manager) Count

func (m *Manager) Count() int

Count returns the number of devices attached to the connection

func (*Manager) Find

func (m *Manager) Find(cb func(uint8, Device) bool) bool

Find iterates over devices and executes the callback per device until true is returned.

type Measurement

type Measurement int

Measurement is the type of measurement, i.e. the physical property being measued in common notation

const (
	Frequency Measurement

	Current
	CurrentL1
	CurrentL2
	CurrentL3

	// phases and sums
	Voltage
	VoltageL1
	VoltageL2
	VoltageL3

	Power // synonymous ActivePower
	PowerL1
	PowerL2
	PowerL3

	ImportPower
	ImportPowerL1
	ImportPowerL2
	ImportPowerL3

	ExportPower
	ExportPowerL1
	ExportPowerL2
	ExportPowerL3

	ReactivePower
	ReactivePowerL1
	ReactivePowerL2
	ReactivePowerL3

	ApparentPower
	ApparentPowerL1
	ApparentPowerL2
	ApparentPowerL3

	Cosphi
	CosphiL1
	CosphiL2
	CosphiL3

	THD
	THDL1
	THDL2
	THDL3

	// energy
	Sum // synonymous ActiveEnergy
	SumT1
	SumT2
	SumL1
	SumL2
	SumL3

	Import
	ImportT1
	ImportT2
	ImportL1
	ImportL2
	ImportL3

	Export
	ExportT1
	ExportT2
	ExportL1
	ExportL2
	ExportL3

	ReactiveSum
	ReactiveSumT1
	ReactiveSumT2
	ReactiveSumL1
	ReactiveSumL2
	ReactiveSumL3

	ReactiveImport
	ReactiveImportT1
	ReactiveImportT2
	ReactiveImportL1
	ReactiveImportL2
	ReactiveImportL3

	ReactiveExport
	ReactiveExportT1
	ReactiveExportT2
	ReactiveExportL1
	ReactiveExportL2
	ReactiveExportL3

	// DC
	DCCurrent
	DCVoltage
	DCPower
	HeatSinkTemp

	// Strings
	DCCurrentS1
	DCVoltageS1
	DCPowerS1
	DCEnergyS1
	DCCurrentS2
	DCVoltageS2
	DCPowerS2
	DCEnergyS2
	DCCurrentS3
	DCVoltageS3
	DCPowerS3
	DCEnergyS3
	DCCurrentS4
	DCVoltageS4
	DCPowerS4
	DCEnergyS4

	// Battery
	ChargeState
	BatteryVoltage

	PhaseAngle
)

func MeasurementString

func MeasurementString(s string) (Measurement, error)

MeasurementString retrieves an enum value from the enum constants string name. Throws an error if the param is not part of the enum.

func MeasurementValues

func MeasurementValues() []Measurement

MeasurementValues returns all values of the enum

func (*Measurement) Description

func (m *Measurement) Description() string

Description returns a measurements human-readable name

func (*Measurement) DescriptionAndUnit

func (m *Measurement) DescriptionAndUnit() (string, string)

DescriptionAndUnit returns a measurements human-readable name and its unit

func (Measurement) IsAMeasurement

func (i Measurement) IsAMeasurement() bool

IsAMeasurement returns "true" if the value is listed in the enum definition. "false" otherwise

func (*Measurement) MarshalText

func (m *Measurement) MarshalText() (text []byte, err error)

MarshalText implements encoding.TextMarshaler

func (Measurement) String

func (i Measurement) String() string

type MeasurementResult

type MeasurementResult struct {
	Measurement
	Value     float64
	Timestamp time.Time
}

MeasurementResult is the result of modbus read operation

func (MeasurementResult) String

func (r MeasurementResult) String() string

type Mock

type Mock struct {
	Client modbus.Client
	// contains filtered or unexported fields
}

Mock mocks a modbus connection

func (*Mock) Close

func (b *Mock) Close()

Close closes the modbus connection.

func (*Mock) ConnectDelay

func (b *Mock) ConnectDelay(delay time.Duration)

ConnectDelay sets the the initial delay after connecting before starting communication

func (*Mock) Logger

func (b *Mock) Logger(l Logger)

Logger sets a logging instance for physical bus operations

func (*Mock) ModbusClient

func (b *Mock) ModbusClient() modbus.Client

ModbusClient returns the mock modbus client

func (*Mock) Slave

func (b *Mock) Slave(deviceID uint8)

Slave sets the modbus device id for the following operations

func (*Mock) String

func (b *Mock) String() string

String returns "simulate" as bus address

func (*Mock) Timeout

func (b *Mock) Timeout(timeout time.Duration) time.Duration

Timeout sets the modbus timeout

type MockClient

type MockClient struct {
	// contains filtered or unexported fields
}

MockClient is a mock modbus client for testing that is able to simulate devices and errors

func NewMockClient

func NewMockClient(errorRate int32) *MockClient

NewMockClient creates a mock modbus client

func (*MockClient) MaskWriteRegister

func (c *MockClient) MaskWriteRegister(address, andMask, orMask uint16) (results []byte, err error)

MaskWriteRegister implements modbus.Client

func (*MockClient) ReadCoils

func (c *MockClient) ReadCoils(address, quantity uint16) (results []byte, err error)

ReadCoils implements modbus.Client

func (*MockClient) ReadDiscreteInputs

func (c *MockClient) ReadDiscreteInputs(address, quantity uint16) (results []byte, err error)

ReadDiscreteInputs implements modbus.Client

func (*MockClient) ReadFIFOQueue

func (c *MockClient) ReadFIFOQueue(address uint16) (results []byte, err error)

ReadFIFOQueue implements modbus.Client

func (*MockClient) ReadHoldingRegisters

func (c *MockClient) ReadHoldingRegisters(address, quantity uint16) (results []byte, err error)

ReadHoldingRegisters implements modbus.Client

func (*MockClient) ReadInputRegisters

func (c *MockClient) ReadInputRegisters(address, quantity uint16) (results []byte, err error)

ReadInputRegisters implements modbus.Client

func (*MockClient) ReadWriteMultipleRegisters

func (c *MockClient) ReadWriteMultipleRegisters(readAddress, readQuantity, writeAddress, writeQuantity uint16, value []byte) (results []byte, err error)

ReadWriteMultipleRegisters implements modbus.Client

func (*MockClient) WriteMultipleCoils

func (c *MockClient) WriteMultipleCoils(address, quantity uint16, value []byte) (results []byte, err error)

WriteMultipleCoils implements modbus.Client

func (*MockClient) WriteMultipleRegisters

func (c *MockClient) WriteMultipleRegisters(address, quantity uint16, value []byte) (results []byte, err error)

WriteMultipleRegisters implements modbus.Client

func (*MockClient) WriteSingleCoil

func (c *MockClient) WriteSingleCoil(address, value uint16) (results []byte, err error)

WriteSingleCoil implements modbus.Client

func (*MockClient) WriteSingleRegister

func (c *MockClient) WriteSingleRegister(address, value uint16) (results []byte, err error)

WriteSingleRegister implements modbus.Client

type RTU

type RTU struct {
	Client  modbus.Client
	Handler *modbus.RTUClientHandler
	// contains filtered or unexported fields
}

RTU is an RTU modbus connection

func (*RTU) Close

func (b *RTU) Close()

Close closes the modbus connection. This forces the modbus client to reopen the connection before the next bus operations.

func (*RTU) ConnectDelay

func (b *RTU) ConnectDelay(delay time.Duration)

ConnectDelay sets the the initial delay after connecting before starting communication

func (*RTU) Logger

func (b *RTU) Logger(l Logger)

Logger sets a logging instance for physical bus operations

func (*RTU) ModbusClient

func (b *RTU) ModbusClient() modbus.Client

ModbusClient returns the RTU modbus client

func (*RTU) Slave

func (b *RTU) Slave(deviceID uint8)

Slave sets the modbus device id for the following operations

func (*RTU) String

func (b *RTU) String() string

String returns the bus device

func (*RTU) Timeout

func (b *RTU) Timeout(timeout time.Duration) time.Duration

Timeout sets the modbus timeout

type RTUOverTCP

type RTUOverTCP struct {
	Client  modbus.Client
	Handler *modbus.RTUOverTCPClientHandler
	// contains filtered or unexported fields
}

RTUOverTCP is a RTU encoder over a TCP modbus connection

func (*RTUOverTCP) Close

func (b *RTUOverTCP) Close()

Close closes the modbus connection. This forces the modbus client to reopen the connection before the next bus operations.

func (*RTUOverTCP) ConnectDelay

func (b *RTUOverTCP) ConnectDelay(delay time.Duration)

ConnectDelay sets the the initial delay after connecting before starting communication

func (*RTUOverTCP) Logger

func (b *RTUOverTCP) Logger(l Logger)

Logger sets a logging instance for physical bus operations

func (*RTUOverTCP) ModbusClient

func (b *RTUOverTCP) ModbusClient() modbus.Client

ModbusClient returns the TCP modbus client

func (*RTUOverTCP) Slave

func (b *RTUOverTCP) Slave(deviceID uint8)

Slave sets the modbus device id for the following operations

func (*RTUOverTCP) String

func (b *RTUOverTCP) String() string

String returns the bus connection address (TCP)

func (*RTUOverTCP) Timeout

func (b *RTUOverTCP) Timeout(timeout time.Duration) time.Duration

Timeout sets the modbus timeout

type TCP

type TCP struct {
	Client  modbus.Client
	Handler *modbus.TCPClientHandler
	// contains filtered or unexported fields
}

TCP is a TCP modbus connection

func (*TCP) Close

func (b *TCP) Close()

Close closes the modbus connection. This forces the modbus client to reopen the connection before the next bus operations.

func (*TCP) ConnectDelay

func (b *TCP) ConnectDelay(delay time.Duration)

ConnectDelay sets the the initial delay after connecting before starting communication

func (*TCP) Logger

func (b *TCP) Logger(l Logger)

Logger sets a logging instance for physical bus operations

func (*TCP) ModbusClient

func (b *TCP) ModbusClient() modbus.Client

ModbusClient returns the TCP modbus client

func (*TCP) Slave

func (b *TCP) Slave(deviceID uint8)

Slave sets the modbus device id for the following operations

func (*TCP) String

func (b *TCP) String() string

String returns the bus connection address (TCP)

func (*TCP) Timeout

func (b *TCP) Timeout(timeout time.Duration) time.Duration

Timeout sets the modbus timeout

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL