08-13-2008 07:55 PM
I'm still getting the hang of this LabView programming style and have found a repeating (and annoying) design pattern that I'm sure others have encountered. I have a long series of steps to perform. Each step is a prerequisite of the next step and each could fail. So I've found myself creating these massive nested case structures so that step 2 only gets called if step 1 succeeds, step 3 only if step 2 is OK, etc. YUCK!
What is the Right Way to handle a chain like this?
Thanks,
Keith.
08-13-2008 09:30 PM
Vi's only begin executing when all their inputs are satisfied, right? Well, make use of that by paying attention to all the data your VIs can generate - or might need.
Say you have three VIs (A, B and C) that need to execute in the order listed. What information does A output that B would need? Regardless of exactly what A and B do there is one piece of information that will be there 90% of the time. I am speaking of status. A needs to let what comes after know whether an error occurred during its processing. Likewise B needs to know if an error occurred before it started.
The mechanism that LV provide for this status communications is the Error Cluster. It contains a boolean that serves as a basic error flag (T = error, F = no error); a numeric that can hold a general error code to specify in general what the problem is, and a string that you can put as much information as you want to identify the issue.
Stringing VIs together using the error clusters is an excellent way of structuring linear code - and can make your applications more robust. At least 90% of LV programs that appear to "lock-up" or "freeze" are due to errors that don't get reported.
Mike...
08-13-2008 10:04 PM
OK, I get that -- use the error cluster to sequence things. But how does that prevent the VIs further down the chain from getting called if there was an error upstream?
Keith.
08-13-2008 10:18 PM
08-13-2008 10:28 PM
Right, and this leads to the nested case statements if I have a bunch of trivial (non-VI) things I want to do ...
Are you sure that standard VIs all check the status before executing?
08-13-2008 10:35 PM
There should not be that much nesting if the subVI is designed logically with dataflow in the first place.
Yes, I am sure. There are some exceptions. These are usually functions that do a 'close'. For example, you would want to execute a VISA Close no matter whether one up the upstream functions caused an error or not.
08-14-2008 12:35 PM
Hi Knicewar,
If your code is modularized into sub-VIs and you can divide it up into sequences which depend on each other you can use a mixture of structures such as sequences and loops. It all depends on how you develop your VI.
Ipshita C.
08-14-2008 06:58 PM
OK, I think I'm getting the hang of this. So for a loop that calls something that uses error clusters, I should use a shift register, right? And if I have two or more things that I don't care if they are in a particular sequence, I should split the error cluster then merge it?
Thanks,
Keith.
08-14-2008 09:24 PM
Yes, if you have things that happen in parallel you can split the error cluster and then merge the two branches back together. One thing to be look out for when merging separate error chains is to not duplicate error messages if both branches contain the same error.
Mike...