Publish/Subscribe C# .NET Example

Remote temperature data acquisition

NetAcquire Pub Sub Example Code

using System;
using netacquire.ClientToolkit.NAPS;
using netacquire.ClientToolkit;

namespace SubscribeSimpleExample
{
class Example
{
static void Main(string[] args)
{
NAServer server = NAServer.GetServer("netacquirehost");
Subscription subscription = server.GetSubscription("reactor_temperature");
Console.WriteLine(subscription.Value);
}
}
}

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 NetAcquire server. The second line retrieves an object that represents a specific data subscription. Subscriptions can be given convenient names on the NetAcquire server. 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 call to Console.WriteLine.

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 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 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