BAOS SDK v2  1.0.1
An SDK providing access to IP-BAOS devices through BAOS binary protocol version 2.x
11_TypeConversion.cpp

This sample demonstrates how the raw bytes in datapoint values can be converted to other types and vice versa.

Usage:

11_TypeConversion <ip_address>
// e.g.:
11_TypeConversion 10.0.0.102

Pre-condition: It assumes that the following datapoints are configured:

Datapoints:
|--------|-----------------|---------------------------------------------------|----------------------|----------|---|---|---|---|---|
|   Id   | Object Function | Data Type                                         | Name                 | Length   | C | R | W | T | U |
|--------|-----------------|---------------------------------------------------|----------------------|----------|---|---|---|---|---|
|     21 | DPT 1           | 1-bit, switch                                     | 21-DPT1              | 1 bit    | C | - | W | T | - |
|--------|-----------------|---------------------------------------------------|----------------------|----------|---|---|---|---|---|
|     22 | DPT 2           | 1-bit controlled, switch control                  | 22-DPT2              | 2 bit    | C | - | W | T | - |
|--------|-----------------|---------------------------------------------------|----------------------|----------|---|---|---|---|---|
|     23 | DPT 3           | 3-bit controlled, dimming control                 | 23-DPT3              | 4 bit    | C | - | W | T | - |
|--------|-----------------|---------------------------------------------------|----------------------|----------|---|---|---|---|---|
|     24 | DPT 4           | character, character (ASCII)                      | 24-DPT4              | 1 byte   | C | - | W | T | - |
|--------|-----------------|---------------------------------------------------|----------------------|----------|---|---|---|---|---|
|     25 | DPT 5           | 8-bit unsigned value, percentage (0..100%)        | 25-DPT5              | 1 byte   | C | - | W | T | - |
|--------|-----------------|---------------------------------------------------|----------------------|----------|---|---|---|---|---|
|     26 | DPT 6           | 8-bit signed value, percentage (-128..127%)       | 26-DPT6              | 1 byte   | C | - | W | T | - |
|--------|-----------------|---------------------------------------------------|----------------------|----------|---|---|---|---|---|
|     27 | DPT 7           | 2-byte unsigned value, pulses                     | 27-DPT7              | 2 bytes  | C | - | W | T | - |
|--------|-----------------|---------------------------------------------------|----------------------|----------|---|---|---|---|---|
|     28 | DPT 8           | 2-byte signed value, pulses difference            | 28-DPT8              | 2 bytes  | C | - | W | T | - |
|--------|-----------------|---------------------------------------------------|----------------------|----------|---|---|---|---|---|
|     29 | DPT 9           | 2-byte float value, temperature (°C)              | 29-DPT9              | 2 bytes  | C | - | W | T | - |
|--------|-----------------|---------------------------------------------------|----------------------|----------|---|---|---|---|---|
|     30 | DPT 10          | time, time of day                                 | 30-DPT10             | 3 bytes  | C | - | W | T | - |
|--------|-----------------|---------------------------------------------------|----------------------|----------|---|---|---|---|---|
|     31 | DPT 11          | date, date                                        | 31-DPT11             | 3 bytes  | C | - | W | T | - |
|--------|-----------------|---------------------------------------------------|----------------------|----------|---|---|---|---|---|
|     32 | DPT 12          | 4-byte unsigned value, counter pulses (unsigned)  | 32-DPT12             | 4 bytes  | C | - | W | T | - |
|--------|-----------------|---------------------------------------------------|----------------------|----------|---|---|---|---|---|
|     33 | DPT 13          | 4-byte signed value, counter pulses (signed)      | 33-DPT13             | 4 bytes  | C | - | W | T | - |
|--------|-----------------|---------------------------------------------------|----------------------|----------|---|---|---|---|---|
|     34 | DPT 14          | 4-byte float value, angular acceleration (rad/s²) | 34-DPT14             | 4 bytes  | C | - | W | T | - |
|--------|-----------------|---------------------------------------------------|----------------------|----------|---|---|---|---|---|
|     35 | DPT 16          | character string, Character String (ISO 8859-1)   | 35-DPT16             | 14 bytes | C | - | W | T | - |
|--------|-----------------|---------------------------------------------------|----------------------|----------|---|---|---|---|---|
|     36 | DPT 18          | scene control, scene control                      | 36-DPT18             | 1 byte   | C | - | W | T | - |
|--------|-----------------|---------------------------------------------------|----------------------|----------|---|---|---|---|---|
//
// Copyright (c) 2002-2023 WEINZIERL ENGINEERING GmbH
// All rights reserved.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
// SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY DAMAGES OR OTHER LIABILITY,
// WHETHER IN CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE
//
#include "Helper.h"
#include <baos/Sdk.h>
#include <wzcpp/features/knx/floatconverter.h>
#include <iostream>
#include <exception>
#include <memory>
#include <iomanip>
#include <chrono>
#include <thread>
using namespace wz::baos;
using namespace wz::baos::ip;
using namespace wz::baos::protocol;
/******************************************
** Anonymous namespace
*******************************************/
namespace
{
constexpr std::uint16_t DPT1_DatapointId = 21;
constexpr std::uint16_t DPT7_DatapointId = 27;
constexpr std::uint16_t DPT9_DatapointId = 29;
constexpr std::uint16_t DPT12_DatapointId = 32;
}
/******************************************
** Main
*******************************************/
int main(int argc, char* argv[])
{
try
{
std::cout << "******************************************" << std::endl;
std::cout << "11_TypeConversion" << std::endl;
std::cout << "******************************************" << std::endl;
std::cout << std::endl;
// Reduce log messages
setBaosLogLevel(wzcpp::LogLevel::warn);
// The CommandLineOptions::parse function
// validates the arguments and handles the help command
CommandLineOptions options;
options.parse(argc, argv);
if (options.wasHelpdisplayed())
{
return 0;
}
// Get the ip address from the parsed command line options
const std::string ipAddress = options.getIpAddress();
std::cout << " Connection requested for " << ipAddress << std::endl;
// create a TPC/IP connection to the remote BAOS device
BaosIp4Device baosDevice(ipAddress);
// read static server items
// NOTE: This does an auto connect
const DeviceStaticInfo staticInfo = baosDevice.getStaticInfo();
const auto dpConfigs = baosDevice.getAllDpConfigurations();
const auto dpValues = baosDevice.getAllDpValuesRaw();
std::cout << "Currently valid datapoint values " << dpValues.size() << std::endl;
// Set some datapoint values
auto knxFloat16 = wzcpp::Knx2ByteFloatConverter::fromFloat4(3.14f);
baosDevice.setDpValue(DPT9_DatapointId, {knxFloat16[0], knxFloat16[1]});
std::uint32_t uint32Value{42123765};
baosDevice.setDpValues({std::make_pair(DPT12_DatapointId, wzcpp::convertBytesFrom<Buffer>(uint32Value)),
std::make_pair(DPT1_DatapointId, wzcpp::convertBytesFrom<Buffer>(true)),
std::make_pair(DPT7_DatapointId, wzcpp::convertBytesFrom<Buffer>(33333))});
// Read back the datapoint values
// DPT9 the KNX 2 byte float
{
const auto rawValue = baosDevice.getDpValueRaw(DPT9_DatapointId);
if(rawValue.has_value())
{
std::cout << "The current raw value of dp " << DPT9_DatapointId << " is: " << wzcpp::rangeToHex(*rawValue) << std::endl;
wzcpp::Knx2ByteFloat knx2bytefloat{rawValue->at(0), rawValue->at(1)};
auto floatValue = wzcpp::Knx2ByteFloatConverter::toFloat4(knx2bytefloat);
std::cout << "The current value as float4 of dp " << DPT9_DatapointId << " is: " << std::fixed << std::setprecision(2) << floatValue << std::endl;
}
else
{
std::cerr << "No value for datapoint " << DPT9_DatapointId << std::endl;
}
}
// DPT12 4 bytes unsigned int as example for an integral type
{
const auto rawValue = baosDevice.getDpValueRaw(DPT12_DatapointId);
if(rawValue.has_value())
{
std::cout << "The current raw value of dp " << DPT12_DatapointId << " is: " << wzcpp::rangeToHex(*rawValue) << std::endl;
std::uint32_t dpt12Value = wzcpp::convertBytesTo<Buffer::const_iterator, std::uint32_t>(std::cbegin(*rawValue));
std::cout << "The current value as std::uint32_t of dp " << DPT12_DatapointId << " is: " << dpt12Value << std::endl;
}
else
{
std::cerr << "No value for datapoint " << DPT12_DatapointId << std::endl;
}
}
// DPT 1 1bit as example for a boolean
{
const auto rawValue = baosDevice.getDpValueRaw(DPT1_DatapointId);
if(rawValue.has_value())
{
std::cout << "The current raw value of dp " << DPT1_DatapointId << " is: " << wzcpp::rangeToHex(*rawValue) << std::endl;
std::uint32_t dpt1Value = wzcpp::convertBytesTo<Buffer::const_iterator, bool>(std::cbegin(*rawValue));
std::cout << "The current value as bool of dp " << DPT1_DatapointId << " is: " << dpt1Value << std::endl;
}
else
{
std::cerr << "No value for datapoint " << DPT1_DatapointId << std::endl;
}
}
// What happens if a wrong DPT is assumed
std::uint32_t uint32Value2{42123765};
std::uint16_t uint16Value{64123};
// Writing with to many bytes
std::cout << "Writing 4 bytes into 2 byte type DPT7-> error response, even if the actual error codes may differ from device to device" << std::endl;
try
{
baosDevice.setDpValue(DPT7_DatapointId, wzcpp::convertBytesFrom<Buffer>(uint32Value2));
}
catch (const wzcpp::error::WzBaseException& e)
{
std::cerr << "Wz Exception in main: " << e.what() << ":" << e.msg() << std::endl;
}
// Writing with to little bytes
std::cout << "Writing 2 bytes into 4 byte type DPT12-> error response, even if the actual error codes may differ from device to device" << std::endl;
try
{
baosDevice.setDpValue(DPT12_DatapointId, wzcpp::convertBytesFrom<Buffer>(uint16Value));
}
catch (const wzcpp::error::WzBaseException& e)
{
std::cerr << "Wz Exception in main: " << e.what() << ":" << e.msg() << std::endl;
}
// Reading with to many bytes
std::cout << "Reading 2 bytes into 4 byte type-> the rest gets filled with zeros" << std::endl;
try
{
const auto rawValue = baosDevice.getDpValueRaw(DPT7_DatapointId);
if(rawValue.has_value())
{
std::cout << "The current raw value of dp " << DPT7_DatapointId << " is: " << wzcpp::rangeToHex(*rawValue) << std::endl;
std::uint32_t dpt12Value = wzcpp::convertBytesTo<Buffer::const_iterator, std::uint32_t>(std::cbegin(*rawValue));
std::cout << "The current value as std::uint32_t of dp " << DPT12_DatapointId << " is: " << dpt12Value << std::endl;
}
else
{
std::cerr << "No value for datapoint " << DPT7_DatapointId << std::endl;
}
}
catch (const wzcpp::error::WzBaseException& e)
{
std::cerr << "Wz Exception in main: " << e.what() << ":" << e.msg() << std::endl;
}
// Reading with to little bytes
std::cout << "Reading 4 bytes into 2 byte type-> only the first 2 bytes are converted" << std::endl;
try
{
const auto rawValue = baosDevice.getDpValueRaw(DPT12_DatapointId);
if(rawValue.has_value())
{
std::cout << "The current raw value of dp " << DPT12_DatapointId << " is: " << wzcpp::rangeToHex(*rawValue) << std::endl;
std::uint16_t dpt12Value = wzcpp::convertBytesTo<Buffer::const_iterator, std::uint16_t>(std::cbegin(*rawValue));
std::cout << "The current value as std::uint16_t of dp " << DPT12_DatapointId << " is: " << dpt12Value << std::hex << "(0x" << dpt12Value << ")"<< std::endl;
}
else
{
std::cerr << "No value for datapoint " << DPT12_DatapointId << std::endl;
}
}
catch (const wzcpp::error::WzBaseException& e)
{
std::cerr << "Wz Exception in main: " << e.what() << ":" << e.msg() << std::endl;
}
// wait for an Enter, all indications will be shown
// in the datapoint event handler
std::cout << "Press [Enter] to exit the application ..." << std::endl;
std::getchar();
}
catch (const std::exception& e)
{
std::cerr << "Failed: " << e.what() << std::endl;
return -1;
}
return 0;
}
Specialization for a IP v4 BAOSDevice.
Specialization for a TCP IP v4 BAOSConnection.
Global BAOS protocol defines and types.
Global BAOS SDK function and options.
Definition: Helper.h:23
Groups BAOS binary protocol specific types , defines and classes for Indications, Responses etc.
Definition: Defines.h:32
Global BAOS sdk namespace.
Definition: config.h:62
void setBaosLogLevel(wzcpp::LogLevel level)