LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Tips and best practices for translating C into LabVIEW? SERIOUS newbie...

You might be getting hung up on front panel controls and front panel indicators.  If you are creating a function where you would like to compute a value (Input 1 in your example) from 3 inputs

 

  double myFunc(double input1, double input2, double input3) { result = input2 * (input 1 + input3); }

 

then you would create a vi with input1, input2, and input3 controls on your front panel, and one indicator called something like myResult.  On the block diagram, the controls are sources that you can read out, and the indicators are sinks where you write values.  You can create something called a Local Variable (possibly an abomination in LabVIEW) where you can write to a front panel control and read from a front panel indicator too.

 

LabVIEW is a dataflow programming language and the concept of variables is analogous with wires on the block diagram, however, you cannot read and write to a wire.  It will take a while for you to get your head around this (trust me Smiley Happy) but it will come.  As soon as you start doing multi-threaded parallel applications by simply drawing another while loop on your block diagram you will start to smile.

0 Kudos
Message 11 of 34
(1,194 Views)

tbob,

 

In your example, it looks like the "Value of Input 1" variable is a separate entity from the "Input1" variable on the top-left, is that correct?

 

When I try making a reference to Input1, I can *read* the value from the reference, but I can't seem to write back to it.  Do I just need to create an indicator?

 

The reason I ask is that the C code makes heavy use of updating the values of variables by the form of A = A * (some other variables).

 

Thanks!

0 Kudos
Message 12 of 34
(1,180 Views)

Don't get confused by all our replys (You got bombarded with more than 10 answers in an hour!).

I disagree with tbob that the references work similar to C pointers. They work by-ref (instead of the LV native by-val). But they are not pointers. No pointers in LV at all (even for the 'big guys', we just get explained why there are no pointers, which won't help you learning LV). I'd even suggest to you to ignore references at all for now.

 

So your idea is very correct: create an indicator.

 

and the form A=A*B is simple wires in -> wires out. Maybe you get it if you use the 'light-bulb' (execution highlighting). The 'sequence' your commands are executed is enforced by the flow of the data along the wires. So you can't 'jump back'.

 

And please fellow helpers, let's stick on the basics and not go to intermediate level.  It takes time to get code running in any language.

 

Felix.

0 Kudos
Message 13 of 34
(1,172 Views)

If you need the Input 1 from before the calculation, then you branch the wire from there, if you need it from after the calculation, then you branch the wire there.

 

If you are doing things in a For Loop or while loop, such as counter = counter +1, then whatever wire represents counter, you feed to a shift register on your loop.

 

Before going crazy and asking what is the LabVIEW equivalent for every kind of C statement, first I would recommend looking at the online LabVIEW tutorials
LabVIEW Introduction Course - Three Hours
LabVIEW Introduction Course - Six Hours

0 Kudos
Message 14 of 34
(1,170 Views)

You are correct in that the Value for Input1 is a separate entity from Input1.   In this case, Input1 is a control.  Value for Input1 is an indicator.  However, the two values themselves are equivalent.  In that I mean the values are the same.  Input1 is a variable.  The reference to Input1 is the address of that variable.  By using the Value property node, I read the data at that address and transfer it into the indicator called Value for Input1.

 

To write to the control, right click on the property node and select Change to Write.  When first created, the property node comes up in a read mode, meaning you can read the data value at the referenced address.  By right clicking and selecting Change to Write, the property node changes direction and accepts an input.  This input will be written to the address space, effectively changing the actual value of Input1.

 

Example_VI_BD.png

 

- tbob

Inventor of the WORM Global
0 Kudos
Message 15 of 34
(1,167 Views)

F. Schubert wrote:

Don't get confused by all our replys (You got bombarded with more than 10 answers in an hour!).

I disagree with tbob that the references work similar to C pointers. They work by-ref (instead of the LV native by-val). But they are not pointers. No pointers in LV at all (even for the 'big guys', we just get explained why there are no pointers, which won't help you learning LV). I'd even suggest to you to ignore references at all for now.

Felix.


Well, I guess we will disagree.  Call it what you want, but the reference holds the address of the variable.  To me it is an equivalent to a pointer in C.  By-ref in either language indicates an address space where the data is held.  In C a pointer holds the address of the variable.  In LV, the reference holds the address of the variable.  I fail to see a difference.  In C you pass a pointer to a function and that function write to that address, changing the value of that variable.  In LV you pass a reference to a subvi, and that subvi can write to that address, changing its value.  Same thing.

 

I agree he should learn with the basics, but when a C guru wants to investigate LV and compare it to C, it is not unwise to point out the similarities between the two languages.  I think I have effectively shown that you can use references in LV in a similar manner to using pointers in C.

 

- tbob

Inventor of the WORM Global
0 Kudos
Message 16 of 34
(1,174 Views)

tbob, I don't mind disagreeing on this. The reference is holding a 'pointer' to the control not the wire. So you can change the input (control/terminal) value without changing the data on the wire.

The most important thingy about property nodes is: they are slow. And pointers (in C) are fast. So do not replace pointer operation with with refernences, use wires!

 

Felix

0 Kudos
Message 17 of 34
(1,154 Views)

F. Schubert wrote:

tbob, I don't mind disagreeing on this. The reference is holding a 'pointer' to the control not the wire. So you can change the input (control/terminal) value without changing the data on the wire.

The most important thingy about property nodes is: they are slow. And pointers (in C) are fast. So do not replace pointer operation with with refernences, use wires!

 

Felix


 I think we basically agree.  Just the difference in semantics.  A reference is truly holding a pointer to the control.  The wire is totally different.  So I guess you can say that a reference is not a pointer.  Yes property nodes are very slow, so I'm not equating timing here in my comparison, just saying that Labview references can do the things that C pointers can (maybe slower, but still can do).

 

I guess this is like comparing C structure to Labview Clusters.  They are fundamentally different but can perform the same end result.

Thanx for pointing out the subtleties between the two.  It should be noted by anyone coming from a C environment to LV.

 

 

- tbob

Inventor of the WORM Global
0 Kudos
Message 18 of 34
(1,151 Views)

promba wrote:

OK, here's another example:

 

 

Input1 = (Input2 * (Input1 + Input3));

 

Is there a way to assign input1 and reference input1 in LabVIEW?  Or do I need some other temporary variable?  When I try to wire the output of the "*" operator into Input1, I get an error.

 

Thanks!

 


This code construct MIGHT typically be inside a loop and in that case it would be best solved with a shift register inside the loop.

 

 

Another way to do this if it is not inside a loop is using locals (I don't advice to use locals much but they still are much better than control references). You basically take the control terminal as input and write the result back into a local of that control.

Message Edited by rolfk on 04-21-2010 07:51 AM
Rolf Kalbermatter
My Blog
0 Kudos
Message 19 of 34
(1,137 Views)

promba wrote:

OK, here's another example:

 

 

Input1 = (Input2 * (Input1 + Input3));

 

Is there a way to assign input1 and reference input1 in LabVIEW?  Or do I need some other temporary variable?  When I try to wire the output of the "*" operator into Input1, I get an error.

 


If you loop back to the control you'll get an error, yes. You'll need to use a local variable or just continue the result wire.

Basic.PNG

That's the easy to use and understand version, probably the ending local variable isn't needed. If it's used later on you can simply wire directly there instead.

If this is supposed to be a function you'll run into the problem of Inputs cannot be outputs, the solution is simply to create an indicator Output 1 instead.

 

/Y

G# - Award winning reference based OOP for LV, for free! - Qestit VIPM GitHub

Qestit Systems
Certified-LabVIEW-Developer
0 Kudos
Message 20 of 34
(1,131 Views)