solarman

package module
v1.0.7 Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2026 License: MIT Imports: 10 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Solarman

Solarman is a Go package for reading and writing Modbus registers over TCP for SolarMan-based inverters.

Features

  • Read group of registers
  • Write to group of registers
  • Easy Get/Set inverter internal clock using pre-defined functions
  • Convert retrieved signed-values to float
  • Extended bytestream debug
  • Meta control to work with various invertors (set StartMarker EndMarker ReqControlCode ResControlCode)
  • Modbus device address of the inverter behind the logger, default 0x01 (SetDeviceAddress)

Basic usage

package main

import (
    "fmt"
    "os"

    "github.com/snowirbis/solarman"
)

var (
    loggerAddress     = "192.168.1.18:8899"
    loggerSN          = uint32(2900000000)
    connectionTimeout = 5
)

// Before running, make sure you don't have
// any other connections to the logger on port 8899

func main() {

    deye := solarman.Init(loggerAddress, loggerSN, connectionTimeout)
    deye.SetDebug(true)

    // defaults defined in frame.go
    // type FrameMeta struct {
    //    	StartMarker    byte   // SolarMan V5 payload starting marker
    //    	EndMarker      byte   // SolarMan V5 payload ending marker
    //    	ReqControlCode uint16 // SolarMan V5 request control code
    //    	ResControlCode uint16 // SolarMan V5 response control code
    // }
    //
    // var DefaultMeta = FrameMeta{
    // 	StartMarker:    0xA5,
    // 	EndMarker:      0x15,
    // 	ReqControlCode: 0x4510,
    // 	ResControlCode: 0x1510,
    // }

    // If meta for your datalogger differs from default, set it here
    deye.SetMeta(0xA5, 0x15, 0x4510, 0x1510)

    // solar strings current voltage for
    // tested with Deye SUN-6K-SG03LP1-EU
    // for basic map of registers see "examples/registers"
    startRegister := 0x6d
    registerCount := 3

    // Read 3 registers starting from 0x6d
    data, err := deye.Read(startRegister, registerCount)
    if err != nil {
	fmt.Println("Error reading registers:", err)
	os.Exit(1)
    }

    // Returning addressed map [register]value
    fmt.Printf("Read registers from 0x%X: %v\n", startRegister, data)

    //Output: Read registers from 0x6D: map[109:385 110:0 111:392]

}

Extended usage

See "examples"

Acknowledgements

This project was inspired by xThaid/inverterlogger, which implemented Modbus register reading.
This package extends functionality by adding write support, improved concurrency, debugging options and simple inverter clock management.

Final Warning!

This code has the ability to write to the inverter's registers.
Please use the write methods with caution, as improper use may lead to equipment failure!

Disclaimer

The author is not responsible for any damage caused by the use of this code, including but not limited to:

  • Harm to your equipment or third-party devices
  • Damage to private or public property
  • Physical or psychological injuries sustained by:
    • You
    • Your relatives & loved ones
    • Pets, wild or domestic animals
    • Politicians, corporate executives, employees
    • Any other individuals

Use at your own risk!

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultMeta = FrameMeta{
	StartMarker:    0xA5,
	EndMarker:      0x15,
	ReqControlCode: 0x4510,
	ResControlCode: 0x1510,
}

Functions

This section is empty.

Types

type Frame

type Frame struct {
	PayloadLength  uint16
	ReqControlCode uint16
	ResControlCode uint16
	SerialNumber   uint16
	DeviceSN       uint32
	Payload        []byte
}

func (*Frame) MarshalBinary

func (f *Frame) MarshalBinary(inv *InverterLogger) ([]byte, error)

func (*Frame) UnmarshalBinary

func (f *Frame) UnmarshalBinary(inv *InverterLogger, data []byte) error

type FrameMeta

type FrameMeta struct {
	StartMarker    byte   // SolarMan V5 payload starting marker
	EndMarker      byte   // SolarMan V5 payload ending marker
	ReqControlCode uint16 // SolarMan V5 request control code
	ResControlCode uint16 // SolarMan V5 response control code
}

type InverterLogger

type InverterLogger struct {
	LoggerAddress  string
	LoggerSerialN  uint32
	DeviceAddress  uint8 // Modbus device address, defaults to 0x01
	DebugEnable    bool
	SequenceNumber uint32
	Timeout        time.Duration
	Meta           FrameMeta

	// OnUnsolicited, if set, is invoked for every frame received from the logger
	// whose control code is not the expected response control code — e.g. the
	// logger's periodic heartbeat (0x4710) or data-report frames. Such frames are
	// skipped while waiting for the matching response. The callback is dispatched
	// after the internal lock has been released, so it is free to call back into
	// the logger (e.g. issue another read).
	OnUnsolicited func(controlCode uint16, frame []byte)
	// contains filtered or unexported fields
}

func Init

func Init(address string, sn uint32, timeout int) *InverterLogger

func (*InverterLogger) Close added in v1.0.5

func (inv *InverterLogger) Close() error

func (*InverterLogger) GetDateTime

func (inv *InverterLogger) GetDateTime(startRegister int) (time.Time, error)

func (*InverterLogger) GetNextSequenceNumber

func (inv *InverterLogger) GetNextSequenceNumber() uint16

func (*InverterLogger) NewFrame

func (inv *InverterLogger) NewFrame(deviceSN uint32, payload []byte) *Frame

func (*InverterLogger) NewReadRequestPayload added in v1.0.5

func (inv *InverterLogger) NewReadRequestPayload(startReg, regCount uint16) *ReadRequestPayload

func (*InverterLogger) NewWriteRequestPayload

func (inv *InverterLogger) NewWriteRequestPayload(registerAddress uint16, values []uint16) *WriteRequestPayload

func (*InverterLogger) Read

func (inv *InverterLogger) Read(startReg, regCnt int) (map[int]uint16, error)

func (*InverterLogger) SetDateTime

func (inv *InverterLogger) SetDateTime(startRegister int, setTime time.Time) (int, int, time.Time, error)

func (*InverterLogger) SetDebug

func (inv *InverterLogger) SetDebug(enable bool)

func (*InverterLogger) SetDeviceAddress added in v1.0.7

func (inv *InverterLogger) SetDeviceAddress(address uint8)

SetDeviceAddress sets the Modbus device address of the inverter behind the logger.

func (*InverterLogger) SetMeta

func (inv *InverterLogger) SetMeta(StartMarker byte, EndMarker byte, ReqControlCode uint16, ResControlCode uint16)

func (*InverterLogger) SignedToFloat

func (inv *InverterLogger) SignedToFloat(data uint16) float64

func (*InverterLogger) Write

func (inv *InverterLogger) Write(startRegister int, values []int) (int, int, error)

type ReadRequestPayload

type ReadRequestPayload struct {
	FrameType    uint8
	SensorType   uint16
	DeliveryTime uint32
	PowerOnTime  uint32
	OffsetTime   uint32

	DeviceAddress uint8  // device address, usually 0x01
	FunctionCode  uint8  // 0x03 – reading registers
	StartReg      uint16 // address of the first register
	RegCount      uint16 // number of registers to read
}

func (*ReadRequestPayload) MarshalBinary

func (r *ReadRequestPayload) MarshalBinary(inv *InverterLogger) ([]byte, error)

type ResponsePayload

type ResponsePayload struct {
	FrameType    uint8
	StatusCode   uint8
	DeliveryTime uint32
	PowerOnTime  uint32
	OffsetTime   uint32

	DeviceAddress uint8
	FunctionCode  uint8
	ValueLength   uint8
	Value         []byte
}

func (*ResponsePayload) UnmarshalBinary

func (r *ResponsePayload) UnmarshalBinary(inv *InverterLogger, data []byte) error

full frame unmarshaling

type WriteRequestPayload

type WriteRequestPayload struct {
	FrameType    uint8  // Example: 0x02
	SensorType   uint16 // Example: 0x0000
	DeliveryTime uint32 // Timestamp of delivery (if needed)
	PowerOnTime  uint32 // Device uptime (if needed)
	OffsetTime   uint32 // Time offset (if needed)

	DeviceAddress   uint8    // Device address, usually 0x01
	FunctionCode    uint8    // Function code for writing multiple registers = 0x10
	RegisterAddress uint16   // Starting register address
	RegisterValues  []uint16 // List of values to write (2 bytes per register)
}

func (*WriteRequestPayload) MarshalBinary

func (w *WriteRequestPayload) MarshalBinary() ([]byte, error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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