Pub Sub C++ Example

NetAcquire Publish/Subscribe C++ Example

Remote temperature data acquisition

NetAcquire Pub Sub Example Code

#include
#include "naps.hpp"

int main()
{
NA_Connection netacquire1("na1");
NA_Subscription reactor_temperature(netacquire1, "reactor_temperature");

cout << "Reactor temperature: " << reactor_temperature << endl; }

This fully functional example illustrates a complete application requiring only three lines of publish/subscribe executable code.

The first line of the main program creates an object that represents a data publisher. The second line creates an object that represents a specific data subscription. Subscriptions can be given convenient names on the NetAcquire; in this case the subscription represents an analog voltage from a reactor temperature sensor. Once a subscription is created, the current value of the subscription can be displayed at any time, as illustrated by the final cout line.

When inserting a subscription object directly into an output stream as done here the implementation formats the value using the most suitable type for the published quantity. For more application control over the data type one can retrieve the subscription's value using an explicit accessor. Integers of width 8, 16, 32, and 64 bits as well as 32 and 64 bit floating point types are supported.

Notice that there is no "clean-up" code at the end of this example; NetAcquire Publish/Subscribe automatically handles all memory and buffer deallocation and network shutdown.

The above example illustrates that NetAcquire Publish/Subscribe automatically keeps the value of the variable reactor_temperature up-to-date without any coding on the application's part. In fact, NetAcquire subscriptions can be used just like regular C++ variables and the "value" of these variables always reflects the remote measurement on the NetAcquire. In the following example two subscription variables are used as if they were variables of type double.

#include
#include "naps.hpp"

int main()
{
NA_Connection netacquire1("na1");
NA_DoubleSubscription reactor_temp1(netacquire1, "reactor_temperature1");
NA_DoubleSubscription reactor_temp2(netacquire1, "reactor_temperature2");

// can perform other activities here...

// compute the average temperature based on the most recent acquired data
double average_temp = (reactor_temp1+reactor_temp2)/2.0;
}

In addition to keeping subscriptions automatically up-to-date, NetAcquire Publish/Subscribe can also notify applications when the value of a subscription changes (see the C++ notification example).

Is NetAcquire a good fit for your project?

Our applications engineers will discuss your needs and offer advice and pricing for the solutions we can provide.
NetAcquire provides quick responses to phone and email queries during Pacific Time business hours.

Call us toll free: 888-675-1122 or email info@netacquire.com

For Employment, Business Affairs and other NetAcquire Contacts, CONTACT US