Pub Sub C Notification Example

NetAcquire Publish/Subscribe C Notification Example

Remote temperature data acquisition

The example below uses the event-driven variant of the NetAcquire Publish/Subscribe API. Instead of explicitly retrieving the subscription’s latest value as was done in the simpler example, here the flow of control is reversed and the Publish/Subscribe framework calls a user-defined callback function whenever the subscription’s value changes.

NetAcquire Pub Sub Example Code


#include
#include "naps.h"

/* callback function */
void temperature_changed(NAPS_Event event)
{
Time_stamp time;
Real_64 temperature;

naps_event_get_timestamp(event, &time);
naps_event_as_real64(event, &temperature);

printf("%I64d: %lf\n", time, temperature);
}

int main(void)
{
NAPS_Connection netacquire1;
NAPS_Subscription reactor_temperature;

netacquire1 = naps_open("na1");
reactor_temperature = naps_subscribe(netacquire1, "reactor_temperature");
naps_add_listener(reactor_temperature, temperature_changed);

getchar(); /* wait until user presses */

naps_close(reactor_temperature);
naps_close(netacquire1);
return 0;
}

The user defines a customized callback function, here called temperature_changed. Note, how the callback function accesses the event’s timestamp.

In the main function a connection to the data publisher named “na1” is opened and an object that represents a specific data subscription is created. this is identical to the simpler example. Then the callback function is registered with the subscription. From this point on the temperature_changed function will be called whenever the physical quantity that corresponds to the subscription changes. The application’s main thread can now continue with other chores, in this example it simply waits for the user to press the key.

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