This sample shows how to read a parameter and write datapoint values via the device API.
Pre-condition: It assumes that the following datapoints and parameters are configured:
#include "Helper.h"
#include <iostream>
#include <exception>
#include <memory>
using namespace wz::baos::ip;
namespace
{
enum Datapoints : std::uint16_t
{
DoAdd = 9,
Counter = 11,
};
enum Parameters : std::uint16_t
{
Step = 2
};
void doAdd(BaosIp4Device& baosDevice)
{
ParameterBytes parameterBytes = baosDevice.getParameterBytes(Parameters::Step, 1);
std::uint8_t step = parameterBytes.at(0);
std::uint16_t counter = 0;
auto maybeValue = baosDevice.getDpValueRaw(Datapoints::Counter);
if (maybeValue.has_value())
{
counter = (maybeValue->at(0) << 8) | maybeValue->at(1);
}
counter += step;
std::uint8_t highByte = (counter & 0xFF00) >> 8;
std::uint8_t lowByte = counter & 0xFF;
baosDevice.setDpValue(1, {highByte, lowByte}, DatapointSetCommand::SetNewValueAndSendOnBus);
}
}
int main(int argc, char* argv[])
{
try
{
std::cout << "******************************************" << std::endl;
std::cout << "08_Counter sample" << std::endl;
std::cout << "******************************************" << std::endl;
std::cout << std::endl;
CommandLineOptions options;
options.parse(argc, argv);
if (options.wasHelpdisplayed())
{
return 0;
}
const std::string ipAddress = options.getIpAddress();
std::cout << " Connection requested for " << ipAddress << std::endl;
BaosIp4Device baosDevice(ipAddress);
const DeviceStaticInfo staticInfo = baosDevice.getStaticInfo();
baosDevice.registerDPValueCallback(
{
for (const auto& item : values)
{
std::uint16_t id = item.first;
std::cout << "Received datapoint value indication for id " << id << std::endl;
switch (id)
{
case Datapoints::DoAdd:
std::cout << "Add step value to counter " << std::endl;
doAdd(baosDevice);
break;
case Datapoints::Counter:
std::cout << "Counter was changed from other (bus or other baos client)" << std::endl;
break;
default:
std::cout << "Nothing to do" << std::endl;
}
}
});
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.
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
std::vector< std::uint8_t > ParameterBytes
Strong type for a list of parameter bytes.
Definition: Defines.h:616
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)