comlynx

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 14, 2026 License: MIT Imports: 9 Imported by: 0

README

comlynx-go

Go client library for reading values from Danfoss TLX inverters over ComLynx (RS485/TCP bridge).

Requirements

  • Go 1.22+
  • Access to a ComLynx transport:
    • Local serial device (for example /dev/ttyUSB0), or
    • TCP endpoint exposing a raw RS485 bridge

Install

go get github.com/evcc-io/comlynx

Run tests

go test ./...

Quick example

package main

import (
	"log"

	comlynx "github.com/evcc-io/comlynx"
)

func main() {
	client, err := comlynx.New(nil, comlynx.Config{
		Device: "/dev/ttyUSB0",
	})
	if err != nil {
		log.Fatal(err)
	}
	defer client.Close()

	// Set the inverter destination after discovery (example values).
	client.SetDestination(comlynx.Address{Network: 0x0c, Subnet: 0x06, Node: 0xb1})

	value, err := client.Read(comlynx.ParamGridPowerTotal)
	if err != nil {
		log.Fatal(err)
	}

	log.Printf("grid power total: %d", value)
}

Notes

  • Client.Read returns the raw 32-bit value reported by the inverter.
  • Client.Ping can be used for address discovery on the ComLynx bus.

Acknowledgments

Protocol reference: yvesf/pycomlynx.

Documentation

Overview

Package comlynx implements the Danfoss ComLynx RS485 protocol used by TripleLynx TLX PV inverters. The protocol is PPP-style HDLC framing (RFC 1662) carrying Embedded CAN Kingdom parameter messages.

The initial framing details (captured frame layout and byte-level examples) were ported from https://github.com/AMajland/Danfoss-TLX.

Index

Constants

View Source
const (
	ParamTotalEnergy uint16 = 0x0102 // lifetime production, raw value in Wh

	ParamGridPowerTotal uint16 = 0x0246 // W
	ParamGridPowerL1    uint16 = 0x0242 // W
	ParamGridPowerL2    uint16 = 0x0243 // W
	ParamGridPowerL3    uint16 = 0x0244 // W

	ParamGridVoltageL1 uint16 = 0x023c // V * 10
	ParamGridVoltageL2 uint16 = 0x023d // V * 10
	ParamGridVoltageL3 uint16 = 0x023e // V * 10

	ParamGridCurrentL1 uint16 = 0x023f // A * 1000 (mA)
	ParamGridCurrentL2 uint16 = 0x0240 // A * 1000 (mA)
	ParamGridCurrentL3 uint16 = 0x0241 // A * 1000 (mA)

	ParamOperatingMode uint16 = 0x0a02 // enum (0-9=off, 10-49=boot, 50-59=connecting, 60-69=on-grid, 70-79=failsafe, 80-89=off-comm)
)

TLX parameter IDs. Sourced from AMajland/Danfoss-TLX (confirmed on TLX 6/8/ 10/12.5/15 kW hardware) and cross-checked against yvesf/pycomlynx.

View Source
const DefaultBaudrate = 19200

DefaultBaudrate is the RS485 baud rate used by all TLX variants.

View Source
const DefaultRetries = 2

DefaultRetries is the number of attempts before returning an error.

View Source
const DefaultTimeout = 2 * time.Second

DefaultTimeout is the per-request timeout applied to each read.

Variables

View Source
var Broadcast = Address{Network: 0x0f, Subnet: 0x0f, Node: 0xff}

Broadcast is the ComLynx broadcast address. Any ComLynx node replies to a request targeted at it.

View Source
var DefaultSource = Address{Network: 0x00, Subnet: 0x00, Node: 0x02}

DefaultSource is the source address evcc advertises on the bus. Matches the hard-coded value used by AMajland/Danfoss-TLX, known to be accepted by TLX inverters.

Functions

This section is empty.

Types

type Address

type Address struct {
	Network byte // 0..15
	Subnet  byte // 0..15
	Node    byte // 0..255
}

Address is a 2-byte ComLynx node address. Network and subnet fit into the first byte (4 bits each); node occupies the second byte.

func Discover

func Discover(c *Client) (Address, error)

Discover sends a broadcast PingRequest and returns the address of the inverter on the bus. If exactly one inverter replies it is returned. If multiple inverters reply an error is returned instructing the caller to set the node address explicitly in the configuration (one meter entry per inverter).

func NewAddress

func NewAddress(network, subnet, node byte) Address

NewAddress constructs an Address. It panics on out-of-range inputs because those only come from hard-coded constants or config validation.

func (Address) Equal

func (a Address) Equal(b Address) bool

Equal reports whether two addresses are identical.

func (Address) String

func (a Address) String() string

type Client

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

Client is a single-inverter ComLynx client. All methods are safe for concurrent use.

func New

func New(log func(format string, a ...any), cfg Config) (*Client, error)

New opens a connection and returns a ready Client. log may be nil for silent operation. The caller may override the destination address via SetDestination before the first read.

func (*Client) Close

func (c *Client) Close() error

Close releases the underlying transport.

func (*Client) Destination

func (c *Client) Destination() Address

Destination returns the currently configured inverter address.

func (*Client) Ping

func (c *Client) Ping(multi bool) ([]Address, error)

Ping issues a broadcast PingRequest and returns the source address of the first replying node, or every replying node if multi is true. Used by address discovery.

func (*Client) Read

func (c *Client) Read(paramID uint16) (int32, error)

Read queries a single TLX parameter by its 16-bit ID and returns the raw 32-bit value (little-endian, as reported by the inverter). Scaling into engineering units happens at the meter wrapper layer.

func (*Client) SetDestination

func (c *Client) SetDestination(dst Address)

SetDestination updates the inverter address used by subsequent reads.

type Config

type Config struct {
	// Device is the local serial port (e.g. /dev/ttyUSB0). Mutually
	// exclusive with URI.
	Device string
	// URI is a TCP endpoint exposing a raw RS485 bridge (host:port). Mutually
	// exclusive with Device.
	URI string
	// Baudrate defaults to 19200 when unset.
	Baudrate int
	// Timeout is the per-request timeout. Zero uses DefaultTimeout.
	Timeout time.Duration
	// Retries is the number of attempts. Zero uses DefaultRetries.
	Retries int
	// Source is the address advertised on the bus. Zero-value uses
	// DefaultSource.
	Source Address
	// Destination is the address of the inverter to query. Zero-value is
	// interpreted as "unset"; the caller is expected to run address
	// discovery before the first read.
	Destination Address
}

Config holds the parameters needed to open a ComLynx client.

Jump to

Keyboard shortcuts

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