LIBSUNSPEC  v0.1.6
A C++ library for the SunSpec protocol
SunSpec Class Reference

Base class for SunSpec compatible inverters, meters and batteries. More...

#include <SunSpec.h>

Inheritance diagram for SunSpec:
CommonModel ExtendedModel FroniusRegister ImmediateModel InverterModel MeterModel MultiMpptModel NameplateModel

Public Member Functions

 SunSpec (void)
 
virtual ~SunSpec (void)
 
std::string GetErrorMessage (void) const
 
bool ConnectModbusTcp (const std::string &node, const std::string &service="502")
 
bool ConnectModbusRtu (const std::string &device, const int &baud_rate=9600)
 
void SetModbusDebug (const bool &debug)
 
bool SetModbusAddress (const int &slave_id=1)
 
bool GetModbusAddress (int &slave_id)
 
bool SetResponseTimeout (const int &millis=500)
 
bool GetResponseTimeout (int &millis)
 
bool SetByteTimeout (const int &millis=500)
 
bool GetByteTimeout (int &millis)
 
bool SetIndicationTimeout (const int &millis=0)
 
bool GetIndicationTimeout (int &millis)
 
bool SetErrorRecovery (const bool recovery)
 
template<>
std::string ConvertRegister (const uint16_t *tab_reg, const uint16_t &size)
 
template<>
float ConvertRegister (const uint16_t *tab_reg, const uint16_t &size)
 

Protected Member Functions

template<typename T >
bool GetRegister (T &res, const uint16_t &reg_addr, const uint16_t &size)
 
bool SetRegister (const uint16_t &value, const uint16_t &reg_addr)
 

Protected Attributes

std::string ErrorMessage
 

Private Member Functions

uint16_t * ReadRegister (const uint16_t &address, const uint16_t &size)
 
template<typename T >
ConvertRegister (const uint16_t *tab_reg, const uint16_t &size)
 

Private Attributes

modbus_t * Ctx
 

Detailed Description

Base class for SunSpec compatible inverters, meters and batteries.

Supports both Modbus TCP and Modbus RTU protocols.

Author
Alexander Pohl alex@.nosp@m.ahpo.nosp@m.hl.co.nosp@m.m

Constructor & Destructor Documentation

SunSpec::SunSpec ( void  )

SunSpec class constructor

SunSpec::~SunSpec ( void  )
virtual

SunSpec class destructor

Member Function Documentation

bool SunSpec::ConnectModbusRtu ( const std::string &  device,
const int &  baud_rate = 9600 
)

Create a libmodbus context for RTU serial

The ConnectModbusRtu() function shall allocate and initialize a modbus_t structure to communicate in RTU mode on a serial line.

Parameters
devicedevice specifies the name of the serial port
baud_ratebaud rate (9600 or 19200)
bool SunSpec::ConnectModbusTcp ( const std::string &  node,
const std::string &  service = "502" 
)

Create a libmodbus context for TCP/IPv4

The ConnectModbusTcp() function shall allocate and initialize a modbus_t structure to communicate with a Modbus TCP IPv4/IPv6 server.

Parameters
nodehostname or address of the server to which the client wants to establish a connection
serviceservice name/port number to connect to
template<typename T >
T SunSpec::ConvertRegister ( const uint16_t *  tab_reg,
const uint16_t &  size 
)
private

Convert raw Modbus registers to usable values

Returns
number or string (template T)
template<>
std::string SunSpec::ConvertRegister ( const uint16_t *  tab_reg,
const uint16_t &  size 
)

Convert raw Modbus registers to string

template<>
float SunSpec::ConvertRegister ( const uint16_t *  tab_reg,
const uint16_t &  size 
)

Convert raw Modbus registers to float

bool SunSpec::GetByteTimeout ( int &  millis)

Get timeout between bytes [ms]

std::string SunSpec::GetErrorMessage ( void  ) const

Return the error message (if any)

bool SunSpec::GetIndicationTimeout ( int &  millis)

Get timeout for indication [ms]

bool SunSpec::GetModbusAddress ( int &  slave_id)

Reads Modbus device address (slave ID)

Returns
: 1-247
template<typename T >
template bool SunSpec::GetRegister ( T &  res,
const uint16_t &  reg_addr,
const uint16_t &  size 
)
protected

Get a Modbus register and convert to number or string

Method automatically converts the raw register value(s) to the destination format specified in the res parameter. Supported register types are:

T: int16_t, uint16_t, uint32_t, uint64_t, std::string

Returns
number or string (template T)
bool SunSpec::GetResponseTimeout ( int &  millis)

Get timeout for response [ms]

uint16_t * SunSpec::ReadRegister ( const uint16_t &  address,
const uint16_t &  size 
)
private

Read raw Modbus registers from the device

Returns
pointer to an array with the register values
bool SunSpec::SetByteTimeout ( const int &  millis = 500)

Set timeout between bytes

The SetByteTimeout() function shall set the timeout interval between two consecutive bytes of the same message. The timeout is an upper bound on the amount of time elapsed before select() returns, if the time elapsed is longer than the defined timeout, an ETIMEDOUT error will be raised by the function waiting for a response.

If millis is zero, this timeout will not be used at all. In this case, SetResponseTimeout() governs the entire handling of the response, the full confirmation response must be received before expiration of the response timeout. When a byte timeout is set, the response timeout is only used to wait for until the first byte of the response.

Parameters
millis[ms], default 500
bool SunSpec::SetErrorRecovery ( const bool  recovery)

Set error recovery

The SetErrorRecovery() function shall set link and protocol error recovery mode to apply when the connection fails, a response timeout occurred or the byte received is not expected.

Parameters
recoveryflag to enable or disable error recovery mode
bool SunSpec::SetIndicationTimeout ( const int &  millis = 0)

Set timeout for indication

The SetIndicationTimeout() function shall set the timeout interval used by a server to wait for a request from a client. The value of to_usec argument must be in the range 0 to 999999. If both to_sec and to_usec are zero, this timeout will not be used at all. In this case, the server will wait forever.

Parameters
millis[ms], default 0
bool SunSpec::SetModbusAddress ( const int &  slave_id = 1)

Sets the Modbus device address (slave ID)

The SetModbusAddress() function sets the slave number in the libmodbus context. It is usually only required to set the slave ID in RTU. The meaning of this ID will be different if your program acts as client (master) or server (slave).

As RTU client, SetModbusAddress() sets the ID of the remote device you want to communicate. Be sure to set the slave ID before issuing any Modbus requests on the serial bus. If you communicate with several servers (slaves), you can set the slave ID of the remote device before each request.

In TCP, the slave number is only required if the message must reach a device on a serial network.

Parameters
slave_idModbus device address (1-247), default (1)
void SunSpec::SetModbusDebug ( const bool &  debug)

Set/clear debug flag of the Modbus context (very verbose)

bool SunSpec::SetRegister ( const uint16_t &  value,
const uint16_t &  reg_addr 
)
protected

Set a single Modbus register (function 0x06)

bool SunSpec::SetResponseTimeout ( const int &  millis = 500)

Set timeout for response

The SetResponseTimeout() function shall set the timeout interval used to wait for a response. When a byte timeout is set, if elapsed time for the first byte of response is longer than the given timeout, an ETIMEDOUT error will be raised by the function waiting for a response. When byte timeout is disabled, the full confirmation response must be received before expiration of the response timeout.

Parameters
millis[ms], default 500

Field Documentation

modbus_t* SunSpec::Ctx
private

Structure that holds the Modbus connection

std::string SunSpec::ErrorMessage
protected

String to hold the error message


The documentation for this class was generated from the following files: