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

This sample shows how to read and write datapoint values via the device API.

Usage:

04_BaosDatapoints <ip_address>
// e.g.:
04_BaosDatapoints 10.0.0.102

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

|---------|-----------------|-----------------------------|-------------|---------------|
|   Id    |  DPT            |  Name                       |  SizeInBit  |  Flags        |
|         |                 |                             |             | C R W T U I   |
|---------|-----------------|-----------------------------|-------------|---------------|
|    1    | DPT 01 - 1 bit  | Actuator A - Switch         | 1           | C - W - - -   |
|---------|-----------------|-----------------------------|-------------|---------------|
|    2    | DPT 01 - 1 bit  | Actuator A - State          | 1           | C R - T - -   |
|---------|-----------------|-----------------------------|-------------|---------------|
|    3    | DPT 01 - 1 bit  | Actuator B - Switch         | 1           | C - W - - -   |
|---------|-----------------|-----------------------------|-------------|---------------|
|    4    | DPT 01 - 1 bit  | Actuator B - State          | 1           | C R - T - -   |
|---------|-----------------|-----------------------------|-------------|---------------|
|   10    | DPT 07 - 2 bytes| Counter                     | 16          | C R W T I -   |
|---------|-----------------|-----------------------------|-------------|---------------|
Note
Another possibility is via the connection API.
See also: Sample GetDatapointValue SetDatapointValue.cpp
//
// 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 <iostream>
#include <memory>
using namespace wz::baos;
using namespace wz::baos::ip;
using namespace wz::baos::protocol;
/******************************************
** Anonymous namespace
*******************************************/
namespace
{
void traceDatapointValue(const DatapointValue& dpValue)
{
std::cout.fill('0');
std::cout << std::hex << std::uppercase;
for (auto& item : dpValue)
{
std::cout << std::setw(2) << static_cast<int>(item) << " ";
}
std::cout << std::nouppercase << std::dec;
}
} // end anonymous namespace
/******************************************
** Main
*******************************************/
int main(int argc, char* argv[])
{
try
{
std::cout << "******************************************" << std::endl;
std::cout << "04_BaosDatapoints sample" << 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 with the remote BAOS device
BaosIp4Device baosDevice(ipAddress);
// Get some datapoint values
// id = 1 (DPT 01 - 1 bit)
auto maybeValue = baosDevice.getDpValueRaw(1);
if (maybeValue.has_value())
{
std::cout << "Id: 1, value: ";
traceDatapointValue(*maybeValue);
std::cout << std::endl;
}
else
{
std::cerr << "No datapoint value for datapoint id 1" << std::endl;
}
// id = 4 (DPT 01 - 1 bit)
maybeValue = baosDevice.getDpValueRaw(4);
if (maybeValue.has_value())
{
std::cout << "Id: 4, value: ";
traceDatapointValue(*maybeValue);
std::cout << std::endl;
}
// id = 10 (DPT 07 - 2 bytes)
maybeValue = baosDevice.getDpValueRaw(10);
if (maybeValue.has_value())
{
std::cout << "Id: 10, value: ";
traceDatapointValue(*maybeValue);
std::cout << std::endl;
}
std::cout << std::endl << "all datapoints: " << std::endl;
// Get all valid datapoint values
// NOTE: datapoint values with invalid values are not returned
// reload it
const DatapointValues values = baosDevice.getAllDpValuesRaw(true);
for (const auto& item : values)
{
const DatapointValue& value = item.second;
std::cout << "Id: " << item.first;
std::cout << ", value: ";
traceDatapointValue(value);
std::cout << std::endl;
}
std::cout << "---------------" << std::endl;
// Set some datapoint values
// id = 1 (DPT 01 - 1 bit)
// NOTE: This value is NOT sent to the bus.
baosDevice.setDpValue(1, {0x00}, DatapointSetCommand::SetNewValue);
// id = 2 (DPT 01 - 1 bit)
baosDevice.setDpValue(2, {0x00}, DatapointSetCommand::SetNewValueAndSendOnBus);
// id = 10 (DPT 07 - 2 bytes)
baosDevice.setDpValue(10, {0x01, 0x23}, DatapointSetCommand::SetNewValueAndSendOnBus);
}
catch (const wzcpp::error::WzBaseException& e)
{
std::cout << "Wz Exception in main: " << e.msg() << std::endl;
}
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
std::map< std::uint16_t, DatapointValue > DatapointValues
A map with datapoint ids as keys and DatapointValue as values.
Definition: Defines.h:35
wz::baos::Buffer DatapointValue
An type for the DatapointValues.
Definition: Defines.h:33
Global BAOS sdk namespace.
Definition: config.h:62
void setBaosLogLevel(wzcpp::LogLevel level)