Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

NetworkVariableSubscriber.DataUpdated fires without anything,and doesn't when it should

Hey all,

 

first i'll post my code:

 

NetworkVariableSubscriber<bool> nvs = new NetworkVariableSubscriber<bool>(netzwerkadresse);
nvs.ConnectionBehavior = SubscriberConnectionBehavior.DoNothing;
nvs.DataUpdated += new EventHandler<DataUpdatedEventArgs<bool>>(nvs_DataUpdated);
nvs.Connect();

 

void nvs_DataUpdated(object sender, DataUpdatedEventArgs<bool> e)
{
_window.textBox1.Text = (int.Parse(_window.textBox1.Text) + 1).ToString();
PreviousStatus = e.Data.GetValue();
}

 

(full code on http://pastebin.com/eJAAdMEr )

 

What i basically want to achieve is reading the current status of a boolean variable that is controlled by an NI9148 and part of a NI9472 (right now i'm testing it with the LEDs in that module)

so as the NetworkVariableReader tends to timeout on some reading operation, i thought that i could subscribe the event and get the latest value this way. well i was absolutely wrong.

 

On start, the event fires 18 times, but it doesn't at all when i write a new value to the variable.

writing works fine as i see the LED light up.

 

my question would be: am i doing anything absolutely wrong? the event is subscribed once in the objects constructor, so multiple subscriptions to it can't be the reason.

i already changed the ConnectionBehaviour hoping for a change,but this didn't change a thing.

 

Of course,i could just make a variable that i save inside the class and update it whenever i use a write-operation, but it should be doable in this way too,especially when i can confirm that the writing worked using a Test-Class with this way,what i can't with the other.

 

 

Thank you very much for your help

 

streppel

 

PS: i know that i shouldn't be passing a Window to the class,and i know that this is absolutely not threadsafe etc, but it was the fastest way to get some debug output,and it was like this(aka multiple calls of the method without any real change) before with a debugger attached too.

 

0 Kudos
Message 1 of 8
(6,000 Views)

Are you looking at anything other than the value in the updates? Getting multiple updates is not unheard of. You can use the Timestamp property to determine if you're getting duplicates, and it might be worth seeing if other properties like Quality have changed.

 

I'm concerned that you mentioned thread safety as well; I don't see anything in your example code that suggests multiple threads are in play. The DataUpdated event will be raised on a safe thread if it was created in a single-threaded environment; the behavior is less predictable if you create it from a worker thread. There's no "quick and easy" way to ignore cross-thread calls to controls. In the best case it rarely works and in the worst case it crashes. Have you verified the event is not being raised by placing a breakpoint in the event handler?

 

If threading is involved, I'd like to see more code to try and reproduce the situation you have described.

Owen P.
Software Engineer
National Instruments
0 Kudos
Message 2 of 8
(5,984 Views)

Hello Owen,

 

the mainproblem with the subscriber isn't that it gets multiple updates in the beginning, but that it gives none afterwards. i tested it with a debugger,it simply doesn't execute the code anymore.

 

My 2nd approach was this:

 

CONSTRUCTOR:

_reader = new NetworkVariableReader<bool>(netzwerkadresse);
_reader.Connect();

 

outside of the constructor:

public bool Status
{
get
{
return _reader.ReadData().GetValue();
}
set
{
_writer.WriteValue(value);
}
}

 

so i could in general write a toggle in just one line of code:

            _licht1.Status = (!_licht1.Status);

where _licht1 is an object of the class above

 

now the problem is,that as soon as i read data using the NetworkVariableReader, reading the status automatically resets it to it's default value(in this case means the LED turns off as soon as i read its status), and not only the one which status i'm trying to read,but also the other one that should not be affected by it at all

 

 

Thanks for your help

 

streppel

0 Kudos
Message 3 of 8
(5,974 Views)

I've double-checked in a test application, and a ReadValue() shouldn't affect the status of the variable or trigger a subscriber update. I have a feeling the problem lies in some code that hasn't been revealed. When I write applications with code similar to what you have posted, they behave as expected.

 

Do simple applications using network variables (like the examples) work as expected? What about the parts of your application that do not interact with the hardware? If that works fine, can you watch the variable from Distributed System Manager while the problem is happening? Is the quality reported as anything but "Good"? You mentioned reads time out periodically, are you sure the subscriber's connection isn't being lost?

Owen P.
Software Engineer
National Instruments
0 Kudos
Message 4 of 8
(5,956 Views)

@OwenP wrote:

I've double-checked in a test application, and a ReadValue() shouldn't affect the status of the variable or trigger a subscriber update. I have a feeling the problem lies in some code that hasn't been revealed. When I write applications with code similar to what you have posted, they behave as expected.

 

Do simple applications using network variables (like the examples) work as expected? What about the parts of your application that do not interact with the hardware? If that works fine, can you watch the variable from Distributed System Manager while the problem is happening? Is the quality reported as anything but "Good"? You mentioned reads time out periodically, are you sure the subscriber's connection isn't being lost?


Actually, this is a simple application. It only consists of a WPF form that is loaded and creates 2 objects of the class on startup. Then upon button presses, i toggle the LEDs with the line i posted previously.

As for the quality i can't check right now because i'm not at work and don't know if i can do so tomorrow, but will try to.

 

I can upload the full solution too if you want to have a look at it,but it is as simple as i said.

0 Kudos
Message 5 of 8
(5,951 Views)

I'd like to know the results of running the Network Variable example application. That's been verified to work on many, many machines. If there's a problem with it, there's something wrong with your machine and that would be the most likely reason for failure. If the example works, then we can be more certain there's something in your code causing the problems.

 

I would like to see the full solution; I haven't tried using Network Variable from WPF much and I'd like to make sure I'm trying the same things you are trying. What version of Measurement Studio are you using? I'd like to make sure I use the right one the next time I attempt to reproduce this problem.

Owen P.
Software Engineer
National Instruments
0 Kudos
Message 6 of 8
(5,945 Views)

@OwenP wrote:

I'd like to know the results of running the Network Variable example application. That's been verified to work on many, many machines. If there's a problem with it, there's something wrong with your machine and that would be the most likely reason for failure. If the example works, then we can be more certain there's something in your code causing the problems.

 

I would like to see the full solution; I haven't tried using Network Variable from WPF much and I'd like to make sure I'm trying the same things you are trying. What version of Measurement Studio are you using? I'd like to make sure I use the right one the next time I attempt to reproduce this problem.


i uploaded the solution for you at  this location

 

i'll try to run the network variable example and post the results afterwards

 

EDIT: the network variable example is working as expected, good quality etc, but this is not really representable for the chassis, not?

could it be that the chassis itself if not working correctly? 

0 Kudos
Message 7 of 8
(5,922 Views)

...Push? anything?

Problem still exists with measurement studio 2013 and ethernet rio ni9148.

I did the same thing as before, tryed to read with a subscriber...fires 4 times in the beginning,quality is good,everything seems fine but nothing after that.

So i thought,ok it is ugly,but lets try it with a normal reader that polls...not a good idea either, cause it will reset all other values...

 

0 Kudos
Message 8 of 8
(5,770 Views)