Common Vision Blox 15.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Friends Modules Pages
Image Manager/Cvb++/CppMultiStream

This example program is located in your CVB installation under %CVB%Tutorial/Image Manager/Cvb++/CppMultiStream.

main.cpp:

// Demonstrates an image acquisition application that uses multiple streams to acquire from. Requires:
// A connected and configured GenICam device, or a mock device that is provided by CVMockTL. If you
// prefer to execute this tutorial with the mock device, please pass the "--mock" switch to the
// executable.
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
#include <iostream>
#include <chrono>
#include <cvb/device_factory.hpp>
#include <cvb/image.hpp>
#include <cvb/global.hpp>
// Constants:
static const constexpr auto TIMEOUT = std::chrono::milliseconds(3000);
static const constexpr int NUM_ELEMENTS_TO_ACQUIRE = 10;
static std::map<Cvb::WaitStatus, const char*> WAIT_ERROR_STATES{ {Cvb::WaitStatus::Timeout, "Timeout"},
{Cvb::WaitStatus::Abort, "Abort"} };
int main(int argc, char* argv[])
{
try
{
// Check if it is requested to run with a mock GenTL Producer.
bool demonstrateWithMock =
((argc > 1) && (std::string(argv[1]).find("--mock") != std::string::npos)) ? true : false;
// Configure the discovery flags.
auto discoverFlags = Cvb::DiscoverFlags::IgnoreVins; // Ignores all of the CVB VINs.
// If it is requested to run with a mock, then additionally set an appropriate flag.
if (demonstrateWithMock)
discoverFlags |= Cvb::DiscoverFlags::IncludeMockTL; // Include CVMockTL.
// Retrieve a device.
auto infoList = Cvb::DeviceFactory::Discover(discoverFlags);
// Can't continue the demo if there's no device:
if (infoList.empty())
throw std::runtime_error("No devices found.");
// Determine the target device.
Cvb::String accessToken;
if (demonstrateWithMock)
{
for (auto n = 0; (n < infoList.size()) && accessToken.empty(); ++n)
if (infoList.at(n).AccessToken().find("MockTL") != std::string::npos)
accessToken = infoList.at(n).AccessToken();
// Make sure if CVMockTL is on the candidate list.
if (accessToken.empty())
throw std::runtime_error("Could not find CVMockTL.");
}
else
accessToken = infoList.front().AccessToken();
// Open the selected device.
auto device =
Cvb::DeviceFactory::Open<Cvb::GenICamDevice>(infoList.front().AccessToken(), Cvb::AcquisitionStack::GenTL);
// Get all streams that belong to the device.
std::vector<Cvb::ImageStreamPtr> streams;
std::generate_n(std::back_inserter(streams), device->StreamCount(),
[&device, i = 0]() mutable { return device->Stream<Cvb::ImageStream>(i++); });
std::cout << "Stream count: " << streams.size() << "\n";
// Let all streams start data streaming.
for (const auto stream : streams)
stream->Start();
// Acquire data.
// Note: Getting the data is sequential here for simplicity;
// concurrent wait on different streams is the more likely use case.
for (auto imageIndex = 0; imageIndex < NUM_ELEMENTS_TO_ACQUIRE; ++imageIndex)
{
for (auto streamIndex = 0u; streamIndex < streams.size(); ++streamIndex)
{
const auto stream = streams[streamIndex];
auto [image, waitStatus, nodeMaps] = stream->WaitFor(TIMEOUT);
switch (waitStatus)
{
default:
std::cout << "Unknown wait status.\n";
case Cvb::WaitStatus::Abort:
case Cvb::WaitStatus::Timeout:
{
std::cout << "Wait status not OK: " << WAIT_ERROR_STATES[waitStatus] << "\n";
continue;
}
case Cvb::WaitStatus::Ok:
break;
}
std::cout << "Stream #" << streamIndex << ", Image #" << imageIndex;
std::cout << std::hex << ", Width: " << image->Width() <<
", Height: " << image->Height() << "\n";
}
}
// Terminate the streaming.
for (const auto stream : streams)
stream->TryAbort();
}
catch (const std::exception& error)
{
std::cout << error.what() << std::endl;
}
return 0;
}
static std::vector< DiscoveryInformation > Discover()
static std::shared_ptr< T > Open(const String &provider, AcquisitionStack acquisitionStack=AcquisitionStack::PreferVin)
std::string String