![]() |
libhomegear-base
0.7
Base library for Homegear and Homegear family modules.
|
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... | |
This class provides a Modbus client.
The class is thread safe.
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;
}
} | BaseLib::Modbus::Modbus | ( | BaseLib::SharedObjects * | baseLib, |
| Modbus::ModbusInfo & | serverInfo | ||
| ) |
|
virtual |
Destructor.
| void BaseLib::Modbus::connect | ( | ) |
Opens the connection to the Modbus server.
| SocketOperationException | When the connection cannot be established. |
| void BaseLib::Modbus::disconnect | ( | ) |
Closes the connection to the Modbus server.
|
inline |
Checks if the socket is connected.
| void BaseLib::Modbus::readCoils | ( | uint16_t | startingAddress, |
| std::vector< uint8_t > & | buffer, | ||
| uint16_t | coilCount | ||
| ) |
Executes modbus function 01 (0x01) "Read Coils".
| startingAddress | Valid values range from 0x0000 to 0xFFFF. | |
| [out] | buffer | The buffer to fill. Make sure the size is at least number of coils bits. |
| coilCount | The number of coils to read (from 1 to 2000 [= 0x7D0]). |
| SocketOperationException | On socket errors. |
| SocketTimeOutException | On socket timeout. |
| SocketClosedException | When the socket is closed during the request. |
| ModbusException | Thrown on all Modbus errors. |
| ModbusServerBusyException | Thrown when the server is currently busy. |
| Modbus::DeviceInfo BaseLib::Modbus::readDeviceIdentification | ( | ) |
Reads all device identification and returns it as a DeviceInfo struct.
| SocketOperationException | On socket errors. |
| SocketTimeOutException | On socket timeout. |
| SocketClosedException | When the socket is closed during the request. |
| ModbusServerBusyException | Thrown when the server is currently busy. |
| void BaseLib::Modbus::readDiscreteInputs | ( | uint16_t | startingAddress, |
| std::vector< uint8_t > & | buffer, | ||
| uint16_t | inputCount | ||
| ) |
Executes modbus function 02 (0x02) "Read Discrete Inputs".
| startingAddress | Valid values range from 0x0000 to 0xFFFF. | |
| [out] | buffer | The buffer to fill. Make sure the size is at least number of inputs bits. |
| inputCount | The number of inputs to read (from 1 to 2000 [= 0x7D0]). |
| SocketOperationException | On socket errors. |
| SocketTimeOutException | On socket timeout. |
| SocketClosedException | When the socket is closed during the request. |
| ModbusException | Thrown on all Modbus errors. |
| ModbusServerBusyException | Thrown when the server is currently busy. |
| void BaseLib::Modbus::readHoldingRegisters | ( | uint16_t | startingAddress, |
| std::vector< uint16_t > & | buffer, | ||
| uint16_t | registerCount | ||
| ) |
Executes modbus function 03 (0x03) "Read Holding Registers".
| startingAddress | Valid values range from 0x0000 to 0xFFFF. | |
| [out] | buffer | The buffer to fill. Make sure the size is at least number of number of registers. |
| registerCount | The number of registers to read (from 1 to 125 [= 0x7D]). |
| SocketOperationException | On socket errors. |
| SocketTimeOutException | On socket timeout. |
| SocketClosedException | When the socket is closed during the request. |
| ModbusException | Thrown on all Modbus errors. |
| ModbusServerBusyException | Thrown when the server is currently busy. |
| void BaseLib::Modbus::readInputRegisters | ( | uint16_t | startingAddress, |
| std::vector< uint16_t > & | buffer, | ||
| uint16_t | registerCount | ||
| ) |
Executes modbus function 04 (0x04) "Read Input Registers".
| startingAddress | Valid values range from 0x0000 to 0xFFFF. | |
| [out] | buffer | The buffer to fill. Make sure the size is at least number of number of registers. |
| registerCount | The number of registers to read (from 1 to 125 [= 0x7D]). |
| SocketOperationException | On socket errors. |
| SocketTimeOutException | On socket timeout. |
| SocketClosedException | When the socket is closed during the request. |
| ModbusException | Thrown on all Modbus errors. |
| ModbusServerBusyException | Thrown when the server is currently busy. |
| 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".
| readStartAddress | Valid values range from 0x0000 to 0xFFFF. | |
| [out] | readBuffer | The buffer to read values to. |
| readRegisterCount | The number of registers to read from 0x0001 to 0x007D. | |
| writeStartAddress | Valid values range from 0x0000 to 0xFFFF. | |
| writeValues | The values to write. | |
| writeRegisterCount | The number of registers to write from 0x0001 to 0x0079. |
| SocketOperationException | On socket errors. |
| SocketTimeOutException | On socket timeout. |
| SocketClosedException | When the socket is closed during the request. |
| ModbusException | Thrown on all Modbus errors. |
| ModbusServerBusyException | Thrown when the server is currently busy. |
|
inline |
Enables or disables debug mode (disabled by default).
In debug mode all packets are written to the standard output.
|
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.
| void BaseLib::Modbus::writeMultipleCoils | ( | uint16_t | startAddress, |
| const std::vector< uint8_t > & | values, | ||
| uint16_t | coilCount | ||
| ) |
Executes modbus function 15 (0x0F) "Write Multiple Coils".
| startAddress | Valid values range from 0x0000 to 0xFFFF. |
| values | The 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. |
| coilCount | The number of coils to write from 0x0001 to 0x07B0. |
| SocketOperationException | On socket errors. |
| SocketTimeOutException | On socket timeout. |
| SocketClosedException | When the socket is closed during the request. |
| ModbusException | Thrown on all Modbus errors. |
| ModbusServerBusyException | Thrown when the server is currently busy. |
| void BaseLib::Modbus::writeMultipleRegisters | ( | uint16_t | startAddress, |
| const std::vector< uint16_t > & | values, | ||
| uint16_t | registerCount | ||
| ) |
Executes modbus function 16 (0x10) "Write Multiple Registers".
| startAddress | Valid values range from 0x0000 to 0xFFFF. |
| values | The values to write. |
| registerCount | The number of registers to write from 0x0001 to 0x007B. |
| SocketOperationException | On socket errors. |
| SocketTimeOutException | On socket timeout. |
| SocketClosedException | When the socket is closed during the request. |
| ModbusException | Thrown on all Modbus errors. |
| ModbusServerBusyException | Thrown when the server is currently busy. |
| void BaseLib::Modbus::writeSingleCoil | ( | uint16_t | address, |
| bool | value | ||
| ) |
Executes modbus function 05 (0x05) "Write Single Coil".
| address | Valid values range from 0x0000 to 0xFFFF. |
| value | The value to set the coil to. |
| SocketOperationException | On socket errors. |
| SocketTimeOutException | On socket timeout. |
| SocketClosedException | When the socket is closed during the request. |
| ModbusException | Thrown on all Modbus errors. |
| ModbusServerBusyException | Thrown when the server is currently busy. |
| void BaseLib::Modbus::writeSingleRegister | ( | uint16_t | address, |
| uint16_t | value | ||
| ) |
Executes modbus function 06 (0x06) "Write Single Register".
| address | Valid values range from 0x0000 to 0xFFFF. |
| value | The value to set the register to (between 0x0000 and 0xFFFF). |
| SocketOperationException | On socket errors. |
| SocketTimeOutException | On socket timeout. |
| SocketClosedException | When the socket is closed during the request. |
| ModbusException | Thrown on all Modbus errors. |
| ModbusServerBusyException | Thrown when the server is currently busy. |