Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

How to create a Network variable subscriber & OnDataUpdate for Variant data type?

Solved!
Go to solution

Looking at the example at C:\Documents and Settings\All Users\Documents\National Instruments\MStudioVS2008\DotNET\Examples\NetworkVariable\Basic\cs\Subcriber.2008, how can I create a network variable subscriber and OnDataUpdate for Variant data types? 

 

Any leads will be helpful. Thanks.

 

0 Kudos
Message 1 of 3
(3,851 Views)
Solution
Accepted by topic author learningnow

I solved it by using the correct datatype in .NET, for example sbyte for CVIInt8.

0 Kudos
Message 2 of 3
(3,834 Views)

Hi learningnow.   I'm glad you were able to solve the issue.  I thought I'd add to your solution in case anyone else is having a similar issue.

The Variant type for network variables is supported.  When writing values, no special considerations are needed; you can use any type parameter to the network variable object.  For example, the following code creates a writer that is able to write to a Variant type network variable and writes a string value. (I used "..." to represent the network variable's location for brevity.)

 

NetworkVariableWriter<string> writer = new NetworkVariableWriter<string>("...")
writer.Connect()

writer.WriteValue("Hello");

 

The safest way to read is to use object as the type parameter and verify that the value you read is the type you expect before using it.  For example, the following code creates a reader, reads some data, and verifies that the data is a double before using it:

 

NetworkVariableReader<object> reader = new NetworkVariableReader<object>("...");
reader.Connect();

NetworkVariableData<object> data = reader.ReadData();
if (data.HasValue)
{
object rawObject = data.GetValue();
try
{
double value = (double)rawObject;
PerformSomeCalculation(value);
}
catch (InvalidCastException ex)
{
// the value is not a double
}
}

 

The code is more complicated because a Variant type network variable will accept any value.  If the reader reads a string value, the cast to double will fail.  If you are certain of the type of the incoming value, you can probably use a type parameter other than object and avoid some of the code, but in that case I'd recommend against using a Variant variable.  If you only want one type, the variable should be that type.

 

I hope this helps.

 

 

Message Edited by OwenP on 11-06-2009 09:42 AM
Owen P.
Software Engineer
National Instruments
Message 3 of 3
(3,803 Views)