rct

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2021 License: LGPL-2.1 Imports: 6 Imported by: 6

README

rct

A library for communication with solar power inverters of the RCT power brand. Tested with the RCT PS 6.0 solar power inverter, battery and grid power sensor.

RCT power is a registered trademark of RCT Power GmbH. This library is not provided by, endorsed by, supported by or affiliated with the company in any way.

It is provided without any warranties, entirely for use at your own risk under a LGPL 2.1 license.

Usage

Install via go get github.com/mlnoga/rct.

Use like this:

package main

import (
  "rct"
  "time"
  "fmt"
)

func main() {
  conn, err:=rct.NewConnection("my-RCT-hostname-or-IP-address", time.Second*2)
  if err!=nil {
    fmt.Println(err)
    return
  }
  defer conn.Close()
  
  a, err:=rct.QueryFloat32(rct.SolarGenAPowerW)
  if err!=nil {
    fmt.Println(err)
    return
  }

  fmt.Printf("%s is %.0fV\n", string(rct.SolarGenAPowerW), a)
}

Architecture

  • datagram.go defines basic constants like commands, on-device identifiers and datagram packets; as well as conversions of datagram payloads to golang types
  • crc.go defines the cyclic redundancy check algorithm to ensure data integrity used by the RCT
  • build.go defines a datagram builder for assembling datagrams to send
  • parse.go defines a datagram parser which parses incoming bytes into datagrams
  • connection.go ties builders and parsers into a bidirectional connection with the device, and defines convenience methods to synchronously query identifiers

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CRC

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

Checksum for a RCT datagram

func NewCRC

func NewCRC() (c *CRC)

Returns a new CRC

func (*CRC) Get

func (c *CRC) Get() uint16

Finalizes CRC, padding if required, and returns value

func (*CRC) Reset

func (c *CRC) Reset()

Resets the CRC

func (*CRC) Update

func (c *CRC) Update(b byte)

Updates CRC with the given byte

type Cache

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

A datagram cache

func NewCache

func NewCache(timeout time.Duration) (cache *Cache)

Creates a new datagram cache

func (*Cache) Get

func (c *Cache) Get(i Identifier) (dg *Datagram, ok bool)

Returns cache entry for the given identifier, if still valid under timeout

func (*Cache) Put

func (c *Cache) Put(dg *Datagram)

Puts given datagram into the cache, for the identifier contained in the datagram

type Command

type Command uint8

Command type for the RCT device

const (
	Read Command = iota + 1
	Write
	LongWrite
	Reserved1
	Response
	LongResponse
	Reserved2
	ReadPeriodically
	Extension = iota + 0x3c - 0x09
)

Command values for the RCT device

func (Command) String

func (c Command) String() string

Converts a RCT command to human-readable represenation

type Connection

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

Connection to a RCT device

func NewConnection

func NewConnection(host string, timeout time.Duration) (conn *Connection, err error)

Creates a new connection to a RCT device at the given address

func (*Connection) Close

func (c *Connection) Close()

Closes the RCT device connection

func (*Connection) Query

func (c *Connection) Query(id Identifier) (dg *Datagram, err error)

Queries the given identifier on the RCT device, returning its value as a datagram

func (*Connection) QueryFloat32

func (c *Connection) QueryFloat32(id Identifier) (val float32, err error)

Queries the given identifier on the RCT device, returning its value as a float32

func (*Connection) QueryUint16

func (c *Connection) QueryUint16(id Identifier) (val uint16, err error)

Queries the given identifier on the RCT device, returning its value as a uint16

func (*Connection) QueryUint8

func (c *Connection) QueryUint8(id Identifier) (val uint8, err error)

Queries the given identifier on the RCT device, returning its value as a uint8

func (*Connection) Receive

func (c *Connection) Receive() (dg *Datagram, err error)

Receives an RCT response via the connection

func (*Connection) Send

func (c *Connection) Send(rdb *DatagramBuilder) (n int, err error)

Sends the given RCT datagram via the connection

type Datagram

type Datagram struct {
	Cmd  Command
	Id   Identifier
	Data []byte
}

A RCT datagram

func (*Datagram) Float32

func (d *Datagram) Float32() (val float32, err error)

Returns datagram body value as a float32

func (*Datagram) String

func (d *Datagram) String() string

Prints a RCT datagram in a human-readable representation

func (*Datagram) Uint16

func (d *Datagram) Uint16() (val uint16, err error)

Returns datagram body value as a uint16

func (*Datagram) Uint8

func (d *Datagram) Uint8() (val uint8, err error)

Returns datagram body value as a uint8

type DatagramBuilder

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

Builds RCT datagrams into an internal buffer, with escaping and CRC correction

func NewDatagramBuilder

func NewDatagramBuilder() (b *DatagramBuilder)

Returns a new DatagramBuilder

func (*DatagramBuilder) Build

func (rdb *DatagramBuilder) Build(dg *Datagram)

Builds a complete datagram into the buffer

func (*DatagramBuilder) Bytes

func (r *DatagramBuilder) Bytes() []byte

Returns the datagram built so far as an array of bytes

func (*DatagramBuilder) Reset

func (rdb *DatagramBuilder) Reset()

Resets the internal buffer and CRC

func (*DatagramBuilder) String

func (r *DatagramBuilder) String() string

Converts the datagram into a string representation for printing

func (*DatagramBuilder) WriteByte

func (rdb *DatagramBuilder) WriteByte(b byte)

Adds a byte to the internal buffer, handling escaping and CRC calculation

func (*DatagramBuilder) WriteByteUnescapedNoCRC

func (rdb *DatagramBuilder) WriteByteUnescapedNoCRC(b byte)

Adds a byte to the internal buffer, without escaping or CRC calculation

func (*DatagramBuilder) WriteCRC

func (rdb *DatagramBuilder) WriteCRC()

Writes the CRC into the current datastream, handling CRC calcuation padding to an even number of bytes

type DatagramParser

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

A parser for RCT datagrams

func NewDatagramParser

func NewDatagramParser() (p *DatagramParser)

Returns a new datagram parser

func (*DatagramParser) Parse

func (p *DatagramParser) Parse() (dg *Datagram, err error)

Parses a given transmission into a datagram

func (*DatagramParser) Reset

func (p *DatagramParser) Reset()

Resets the state, without reallocating the buffer

type Identifier

type Identifier uint32

Identifier type for variables on the RCT device

const (
	// power
	//
	SolarGenAPowerW  Identifier = 0xB5317B78 // float32
	SolarGenBPowerW  Identifier = 0xAA9AA253 // float32
	BatteryPowerW    Identifier = 0x400f015b // float32, positive = discharge, negative = charge
	InverterACPowerW Identifier = 0xDB2D69AE // float32
	RealPowerW       Identifier = 0x4E49AEC5 // float32
	TotalGridPowerW  Identifier = 0x91617C58 // float32, positive = taken from grid, negative = feed into grid
	BatterySoC       Identifier = 0x959930BF // float32, range 0 ... 1

	// voltage
	//
	SolarGenAVoltage Identifier = 0xB298395D // float32
	SolarGenBVoltage Identifier = 0x5BB8075A // float32
	BatteryVoltage   Identifier = 0xA7FA5C5D // float32

	// energy
	//
	TotalEnergyWh           Identifier = 0xB1EF67CE // float32
	TotalEnergySolarGenAWh  Identifier = 0xFC724A9E // float32
	TotalEnergySolarGenBWh  Identifier = 0x68EEFD3D // float32
	TotalEnergyBattInWh     Identifier = 0x5570401B // float32
	TotalEnergyBattOutWh    Identifier = 0xA9033880 // float32
	TotalEnergyHouseholdWh  Identifier = 0xEFF4B537 // float32
	TotalEnergyGridWh       Identifier = 0xA59C8428 // float32
	TotalEnergyGridFeedInWh Identifier = 0x44D4C533 // float32
	TotalEnergyGridLoadWh   Identifier = 0x62FBE7DC // float32

	// other
	//
	InverterState             Identifier = 0x5F33284E // uint8
	BatteryCapacityAh         Identifier = 0xB57B59BD // float32
	BatteryTemperatureC       Identifier = 0x902AFAFB // float32
	BatterySoCTarget          Identifier = 0x8B9FF008 // float32 0 ... 1
	BatterySoCTargetHigh      Identifier = 0xB84A38AB // float32 0 ... 1
	BatterySoCTargetMin       Identifier = 0xCE266F0F // float32 0 ... 1
	BatterySoCTargetMinIsland Identifier = 0x8EBF9574 // float32 0 ... 1
)

Identifier values for variables on the RCT device

func (Identifier) String

func (i Identifier) String() string

Converts an identifier to a human-readable representation

type InverterStates

type InverterStates uint8

Inverter state type for InverterState responses from the RCT

const (
	StateStandby InverterStates = iota
	StateInitialization
	StateStandby2
	StateEfficiency
	StateInsulationCheck
	StateIslandCheck
	StatePowerCheck
	StateSymmetry
	StateRelayTest
	StateGridPassive
	StatePrepareBattPassive
	StateBattPassive
	StateHWCheck
	StateFeedIn
)

Inverter state values for InverterState responses from the RCT

func (InverterStates) String

func (i InverterStates) String() string

Converts an inverter state value to a human-readable string

type ParserState

type ParserState int

State machine type for the RCT datagram parser

const (
	AwaitingStart ParserState = iota
	AwaitingCmd
	AwaitingLen
	AwaitingId0
	AwaitingId1
	AwaitingId2
	AwaitingId3
	AwaitingData
	AwaitingCrc0
	AwaitingCrc1
	Done
)

State machine values for the RCT datagram parser

Jump to

Keyboard shortcuts

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