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

This demo shows how to to adjust the configuration of the BaosDeviceClass

Usage:

10_ClassConfig <ip_address>
// e.g.:
10_ClassConfig 10.0.0.102
//
// 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;
/******************************************
** Anonymous namespace
*******************************************/
namespace
{
void getDeviceInfo(ip::BaosIp4Device& baosDevice)
{
try
{
const DeviceStaticInfo staticInfo = baosDevice.getStaticInfo();
auto dpDescs = baosDevice.getAllDpConfigurations();
std::cout << dpDescs.size() << " datapoints configured" << std::endl;
auto dpValues = baosDevice.getAllDpValuesRaw();
std::cout << dpValues.size() << " valid datapoint values" << std::endl;
std::cout << "Read all parameter bytes: " << std::endl;
auto params = baosDevice.getAllParameterBytes();
std::cout << params.size() << " parameter bytes read" << std::endl;
}
catch (const wzcpp::error::WzBaseException& e)
{
std::cerr << "Wz Exception in main: " << e.msg() << std::endl;
}
}
} // namespace
int main(int argc, char* argv[])
{
try
{
std::cout << "******************************************" << std::endl;
std::cout << "08_Class config 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 << " Working with device on IP address " << ipAddress << std::endl;
// There are three ways to obtain the necessary information to communicate with the BAOS device
/* The first is using the ServerItemReadStrategy::METADATAREADWITHFALLBACK, which is the default.
* This method is also used in the other samples.
* It probes the device to obtain sufficient information to identify its device group, then checks the lookup
* table `DevicesMetaLookup` in baos/device/BaosDevicesMeta.h. If there is an entry use it otherwise asume that
* it is a "newer" device like the 777 or kTux stack and read all the serveritems at once. For this to work the
* BAOS device must either be in the DevicesMetaLookup table or must support reading serveritem 1-UINT16_MAX and
* should have the serveritem protocol::ServerItemId::MaxDatapoints and
* protocol::ServerItemId::MaxParameterBytes. If you are working with a device which is currently not in the
* lookup table, fell free to add it before compiling the library.
*/
{
std::cout << "------------------ ServerItemReadStrategy::METADATAREADWITHFALLBACK -----------------------"
<< std::endl;
// create a TPC/IP connection to the remote BAOS device
ip::BaosIp4Device baosDevice(ipAddress);
getDeviceInfo(baosDevice);
}
/* The second approach is setting the meta information manually. This produces the least traffic but you must
* know the details beforehand.*/
{
std::cout << "------------------ Set meta data manually -----------------------" << std::endl;
// create a TPC/IP connection to the remote BAOS device
ip::BaosIp4Device baosDevice(ipAddress);
/* This will set the max serveritem id to 18. All BAOS devices support the serveritems 1-18.
* The max parameter and datapoint count is set to 100 for this sample.
*/
baosDevice.setMetaInfo(DeviceMeta{"BaosGeneric", 18, 100, 100});
/* For adding a new device group to the meta data lookup table `DevicesMetaLookup` in
* baos/device/BaosDevicesMeta.h, a unique DeviceGroupIdentifier is required. The following snippet helps to
* obtain it:
*/
const DeviceConfigInfo configInfo = baosDevice.getConfigInfo();
const DeviceGroupIdentifier devicegroupid{configInfo.manufactureCodeApp,
configInfo.applicationId,
configInfo.applicationVersion};
auto hexString = wzcpp::rangeToHex<std::array<std::byte, 5>>(devicegroupid.deviceid);
std::cout << "The DeviceGroupIdentifier for this device is: " << hexString << std::endl;
getDeviceInfo(baosDevice); // Receive information from device as before.
}
// The third option will only work if the device supports reading all serveritems at once,
// has the serveritems protocol::ServerItemId::MaxDatapoints and protocol::ServerItemId::MaxParameterBytes.
// With this strategy no external meta information is required.
// The following code block checks that.
{
std::cout << "------------------ ServerItemReadStrategy::AllATONCE -----------------------" << std::endl;
ip::BaosIp4Device baosDevice(ipAddress);
baosDevice.setServerItemReadStrategy(ServerItemReadStrategy::ALLATONCE);
bool allAtOnceAvailable{false};
try
{
auto staticInfo = baosDevice.getStaticInfo(); // If reading of all serveritems at once is not available
// this line will fail.
auto connection = baosDevice.getConnection();
auto maxDatapointsItem = connection->getServerItem(protocol::ServerItemId::MaxDatapoints);
auto maxParameterBytesItem = connection->getServerItem(protocol::ServerItemId::MaxParameterBytes);
if (maxDatapointsItem.has_value())
{
std::cout << "Serveritem MaxDatapoints available" << std::endl;
}
if (maxParameterBytesItem.has_value())
{
std::cout << "Serveritem MaxParameterBytes available" << std::endl;
}
if (maxDatapointsItem.has_value() && maxParameterBytesItem.has_value())
{
allAtOnceAvailable = true;
}
}
catch (const wzcpp::error::WzBaseException& e)
{
std::cerr << "Wz Exception in main: " << e.msg() << std::endl;
}
std::cout << "The device with ip " << ipAddress << " can use the all at once strategy: " << std::boolalpha
<< allAtOnceAvailable << 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 SDK function and options.
Definition: Helper.h:23
@ MaxParameterBytes
(Read Only; 2 bytes; from V2.1)
@ MaxDatapoints
(Read Only; 2 bytes; from V2.1)
Global BAOS sdk namespace.
Definition: config.h:62
void setBaosLogLevel(wzcpp::LogLevel level)
@ ALLATONCE
Read from first to std::uint16_t Max serveritems.