libhomegear-base  0.7
Base library for Homegear and Homegear family modules.
BaseLib::Modbus Class Reference

This class provides a Modbus client. More...

#include <Modbus.h>

Classes

struct  DeviceInfo
 
struct  ModbusInfo
 

Public Member Functions

 Modbus (BaseLib::SharedObjects *baseLib, ModbusInfo &serverInfo)
 
virtual ~Modbus ()
 Destructor. More...
 
void setSlaveId (uint8_t value)
 Sets the slave ID. More...
 
void setDebug (bool value)
 Enables or disables debug mode (disabled by default). More...
 
void connect ()
 Opens the connection to the Modbus server. More...
 
void disconnect ()
 Closes the connection to the Modbus server. More...
 
bool isConnected ()
 Checks if the socket is connected. More...
 
void readCoils (uint16_t startingAddress, std::vector< uint8_t > &buffer, uint16_t coilCount)
 Executes modbus function 01 (0x01) "Read Coils". More...
 
void readDiscreteInputs (uint16_t startingAddress, std::vector< uint8_t > &buffer, uint16_t inputCount)
 Executes modbus function 02 (0x02) "Read Discrete Inputs". More...
 
void readHoldingRegisters (uint16_t startingAddress, std::vector< uint16_t > &buffer, uint16_t registerCount)
 Executes modbus function 03 (0x03) "Read Holding Registers". More...
 
void readInputRegisters (uint16_t startingAddress, std::vector< uint16_t > &buffer, uint16_t registerCount)
 Executes modbus function 04 (0x04) "Read Input Registers". More...
 
void writeSingleCoil (uint16_t address, bool value)
 Executes modbus function 05 (0x05) "Write Single Coil". More...
 
void writeSingleRegister (uint16_t address, uint16_t value)
 Executes modbus function 06 (0x06) "Write Single Register". More...
 
void writeMultipleCoils (uint16_t startAddress, const std::vector< uint8_t > &values, uint16_t coilCount)
 Executes modbus function 15 (0x0F) "Write Multiple Coils". More...
 
void writeMultipleRegisters (uint16_t startAddress, const std::vector< uint16_t > &values, uint16_t registerCount)
 Executes modbus function 16 (0x10) "Write Multiple Registers". More...
 
void readWriteMultipleRegisters (uint16_t readStartAddress, std::vector< uint16_t > &readBuffer, uint16_t readRegisterCount, uint16_t writeStartAddress, const std::vector< uint16_t > &writeValues, uint16_t writeRegisterCount)
 Executes modbus function 23 (0x17) "Read/Write Multiple Registers". More...
 
DeviceInfo readDeviceIdentification ()
 Reads all device identification and returns it as a DeviceInfo struct. More...
 

Detailed Description

This class provides a Modbus client.

The class is thread safe.

Modbus Client Example Code

Save the example below in main.cpp and compile with:

g++ -o main -std=c++11 main.cpp -lhomegear-base -lgcrypt -lgnutls

Start with:

./main

Example code:

// This program connects to a Modbus server, sets a register to "0xFFFF" and 2 seconds later to "0x0000".
#include <homegear-base/BaseLib.h>

std::shared_ptr<BaseLib::SharedObjects> _bl;

int main()
{
    _bl.reset(new BaseLib::SharedObjects(false));

    BaseLib::Modbus::ModbusInfo modbusInfo;
    modbusInfo.hostname = "192.168.0.206"; //Replace with the IP address or hostname of your Modbus server.

    BaseLib::Modbus modbus(_bl.get(), modbusInfo);

    try
    {
        modbus.connect();
        modbus.writeSingleRegister(0x2001, 0xFFFF); //Replace with a working register address and value
        std::this_thread::sleep_for(std::chrono::milliseconds(2000));
        modbus.writeSingleRegister(0x2001, 0x0000);  //Replace with a working register address and value
        modbus.disconnect();
    }
    catch(BaseLib::ModbusException& ex)
    {
        std::cerr << "Code: " << (int32_t)ex.getCode()
                  << ", response packet: " << BaseLib::HelperFunctions::getHexString(ex.getPacket())
                  << ", message: " << ex.what() << std::endl;
    }
    catch(BaseLib::Exception& ex)
    {
        std::cerr << ex.what() << std::endl;
    }
}

Constructor & Destructor Documentation

§ Modbus()

BaseLib::Modbus::Modbus ( BaseLib::SharedObjects baseLib,
Modbus::ModbusInfo serverInfo 
)

§ ~Modbus()

BaseLib::Modbus::~Modbus ( )
virtual

Destructor.

Member Function Documentation

§ connect()

void BaseLib::Modbus::connect ( )

Opens the connection to the Modbus server.

Exceptions
SocketOperationExceptionWhen the connection cannot be established.

§ disconnect()

void BaseLib::Modbus::disconnect ( )

Closes the connection to the Modbus server.

§ isConnected()

bool BaseLib::Modbus::isConnected ( )
inline

Checks if the socket is connected.

Returns
Returns true when the socket is connected and false otherwise.

§ readCoils()

void BaseLib::Modbus::readCoils ( uint16_t  startingAddress,
std::vector< uint8_t > &  buffer,
uint16_t  coilCount 
)

Executes modbus function 01 (0x01) "Read Coils".

Parameters
startingAddressValid values range from 0x0000 to 0xFFFF.
[out]bufferThe buffer to fill. Make sure the size is at least number of coils bits.
coilCountThe number of coils to read (from 1 to 2000 [= 0x7D0]).
Returns
Returns the coil states as a byte array. The least significant bit is to the left (in contrast to the Modbus packet). So the bits and bytes can be read from left to right.
Exceptions
SocketOperationExceptionOn socket errors.
SocketTimeOutExceptionOn socket timeout.
SocketClosedExceptionWhen the socket is closed during the request.
ModbusExceptionThrown on all Modbus errors.
ModbusServerBusyExceptionThrown when the server is currently busy.

§ readDeviceIdentification()

Modbus::DeviceInfo BaseLib::Modbus::readDeviceIdentification ( )

Reads all device identification and returns it as a DeviceInfo struct.

Returns
A DeviceInfo struct containing all identification objects (basic, regular and extended).
Exceptions
SocketOperationExceptionOn socket errors.
SocketTimeOutExceptionOn socket timeout.
SocketClosedExceptionWhen the socket is closed during the request.
ModbusServerBusyExceptionThrown when the server is currently busy.

§ readDiscreteInputs()

void BaseLib::Modbus::readDiscreteInputs ( uint16_t  startingAddress,
std::vector< uint8_t > &  buffer,
uint16_t  inputCount 
)

Executes modbus function 02 (0x02) "Read Discrete Inputs".

Parameters
startingAddressValid values range from 0x0000 to 0xFFFF.
[out]bufferThe buffer to fill. Make sure the size is at least number of inputs bits.
inputCountThe number of inputs to read (from 1 to 2000 [= 0x7D0]).
Returns
Returns the input states as a byte array. The least significant bit is to the left (in contrast to the Modbus packet). So the bits and bytes can be read from left to right.
Exceptions
SocketOperationExceptionOn socket errors.
SocketTimeOutExceptionOn socket timeout.
SocketClosedExceptionWhen the socket is closed during the request.
ModbusExceptionThrown on all Modbus errors.
ModbusServerBusyExceptionThrown when the server is currently busy.

§ readHoldingRegisters()

void BaseLib::Modbus::readHoldingRegisters ( uint16_t  startingAddress,
std::vector< uint16_t > &  buffer,
uint16_t  registerCount 
)

Executes modbus function 03 (0x03) "Read Holding Registers".

Parameters
startingAddressValid values range from 0x0000 to 0xFFFF.
[out]bufferThe buffer to fill. Make sure the size is at least number of number of registers.
registerCountThe number of registers to read (from 1 to 125 [= 0x7D]).
Returns
Returns the register values.
Exceptions
SocketOperationExceptionOn socket errors.
SocketTimeOutExceptionOn socket timeout.
SocketClosedExceptionWhen the socket is closed during the request.
ModbusExceptionThrown on all Modbus errors.
ModbusServerBusyExceptionThrown when the server is currently busy.

§ readInputRegisters()

void BaseLib::Modbus::readInputRegisters ( uint16_t  startingAddress,
std::vector< uint16_t > &  buffer,
uint16_t  registerCount 
)

Executes modbus function 04 (0x04) "Read Input Registers".

Parameters
startingAddressValid values range from 0x0000 to 0xFFFF.
[out]bufferThe buffer to fill. Make sure the size is at least number of number of registers.
registerCountThe number of registers to read (from 1 to 125 [= 0x7D]).
Returns
Returns the register values.
Exceptions
SocketOperationExceptionOn socket errors.
SocketTimeOutExceptionOn socket timeout.
SocketClosedExceptionWhen the socket is closed during the request.
ModbusExceptionThrown on all Modbus errors.
ModbusServerBusyExceptionThrown when the server is currently busy.

§ readWriteMultipleRegisters()

void BaseLib::Modbus::readWriteMultipleRegisters ( uint16_t  readStartAddress,
std::vector< uint16_t > &  readBuffer,
uint16_t  readRegisterCount,
uint16_t  writeStartAddress,
const std::vector< uint16_t > &  writeValues,
uint16_t  writeRegisterCount 
)

Executes modbus function 23 (0x17) "Read/Write Multiple Registers".

Parameters
readStartAddressValid values range from 0x0000 to 0xFFFF.
[out]readBufferThe buffer to read values to.
readRegisterCountThe number of registers to read from 0x0001 to 0x007D.
writeStartAddressValid values range from 0x0000 to 0xFFFF.
writeValuesThe values to write.
writeRegisterCountThe number of registers to write from 0x0001 to 0x0079.
Exceptions
SocketOperationExceptionOn socket errors.
SocketTimeOutExceptionOn socket timeout.
SocketClosedExceptionWhen the socket is closed during the request.
ModbusExceptionThrown on all Modbus errors.
ModbusServerBusyExceptionThrown when the server is currently busy.

§ setDebug()

void BaseLib::Modbus::setDebug ( bool  value)
inline

Enables or disables debug mode (disabled by default).

In debug mode all packets are written to the standard output.

§ setSlaveId()

void BaseLib::Modbus::setSlaveId ( uint8_t  value)
inline

Sets the slave ID.

For Modbus over TCP this is normally unnecessary. It is needed to reach devices on a serial network or for devices with broken firmware. The default value is "0xFF". Disconnecting doesn't change the ID. It can be set before "connect()" is executed.

§ writeMultipleCoils()

void BaseLib::Modbus::writeMultipleCoils ( uint16_t  startAddress,
const std::vector< uint8_t > &  values,
uint16_t  coilCount 
)

Executes modbus function 15 (0x0F) "Write Multiple Coils".

Parameters
startAddressValid values range from 0x0000 to 0xFFFF.
valuesThe values to write. The least significant bit is to the left (in contrast to the Modbus packet). So the bits and bytes can be read from left to right.
coilCountThe number of coils to write from 0x0001 to 0x07B0.
Exceptions
SocketOperationExceptionOn socket errors.
SocketTimeOutExceptionOn socket timeout.
SocketClosedExceptionWhen the socket is closed during the request.
ModbusExceptionThrown on all Modbus errors.
ModbusServerBusyExceptionThrown when the server is currently busy.

§ writeMultipleRegisters()

void BaseLib::Modbus::writeMultipleRegisters ( uint16_t  startAddress,
const std::vector< uint16_t > &  values,
uint16_t  registerCount 
)

Executes modbus function 16 (0x10) "Write Multiple Registers".

Parameters
startAddressValid values range from 0x0000 to 0xFFFF.
valuesThe values to write.
registerCountThe number of registers to write from 0x0001 to 0x007B.
Exceptions
SocketOperationExceptionOn socket errors.
SocketTimeOutExceptionOn socket timeout.
SocketClosedExceptionWhen the socket is closed during the request.
ModbusExceptionThrown on all Modbus errors.
ModbusServerBusyExceptionThrown when the server is currently busy.

§ writeSingleCoil()

void BaseLib::Modbus::writeSingleCoil ( uint16_t  address,
bool  value 
)

Executes modbus function 05 (0x05) "Write Single Coil".

Parameters
addressValid values range from 0x0000 to 0xFFFF.
valueThe value to set the coil to.
Exceptions
SocketOperationExceptionOn socket errors.
SocketTimeOutExceptionOn socket timeout.
SocketClosedExceptionWhen the socket is closed during the request.
ModbusExceptionThrown on all Modbus errors.
ModbusServerBusyExceptionThrown when the server is currently busy.

§ writeSingleRegister()

void BaseLib::Modbus::writeSingleRegister ( uint16_t  address,
uint16_t  value 
)

Executes modbus function 06 (0x06) "Write Single Register".

Parameters
addressValid values range from 0x0000 to 0xFFFF.
valueThe value to set the register to (between 0x0000 and 0xFFFF).
Exceptions
SocketOperationExceptionOn socket errors.
SocketTimeOutExceptionOn socket timeout.
SocketClosedExceptionWhen the socket is closed during the request.
ModbusExceptionThrown on all Modbus errors.
ModbusServerBusyExceptionThrown when the server is currently busy.

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