LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Relay timer, Program works but need advice on making the code more efficient

I would like have a VI to control relays via the serial port. I have attached a part of the whole program.
The attached VI is to control three relays, but in future there would be more, something like 7 or 8.

This VI does the job, but as you can see I have used three separate case structures with one Elapsed time subvi for each of the relay. 

Is there a more professional/efficient way to do this? i.e. to not have individual case structure for each relay? The operation does not need to be super fast.

 

More comments in the VI.

0 Kudos
Message 1 of 4
(2,233 Views)

Note that you are setting Relay n twice, ones by it's terminal, once with a property node. That is a race condition, the exact behavior will be unclear. Try to set it by just it's terminal.

 

Next thing is to get that code in a sub VI. That way you don't have to copy the code 8 times, and later change the code 8 times. That get's old very fast. It's a terrible practice.

 

Ideally, abstract the relay behavior in a class. That would enable you to make an array of relay objects, and you can then execute them in a for loop. If you ever get more types of relays, it's easy to adapt (by making a child).

Message 2 of 4
(2,208 Views)

Thank you for the pointers. I would start with the SubVI route.

 

Could you give an example of the "Ideally, abstract the relay behavior in a class. That would enable you to make an array of relay objects, and you can then execute them in a for loop. If you ever get more types of relays, it's easy to adapt (by making a child)." with respect to my problem?

 

Thank you for your time.

 

 

0 Kudos
Message 3 of 4
(2,205 Views)

@zezendapuss wrote:

Thank you for the pointers. I would start with the SubVI route.


Sure, but keep in mind that OO is not more difficult then "normal" programming. It's just a different mindset. Read one or two good books on OO, and programming will be a lot easier for the rest of your life (IMHO). 

 


@zezendapuss wrote:

 Could you give an example of the "Ideally, abstract the relay behavior in a class. That would enable you to make an array of relay objects, and you can then execute them in a for loop. If you ever get more types of relays, it's easy to adapt (by making a child)." with respect to my problem?


Well, once you have a class, you can instantiate objects. Each object will represent a relay. The relay class could have a method "Check" (or Execute, Run, or whatever makes sense). In a for loop you can then execute the "Check" on that array.

 

But here comes the nice part of OO...

 

Your application expects an array or relay objects. Those all have a "Check" behavior. But if you inherit from relay, you get a child, let's say "Timed Relay". This child can extend or modify it's parent (relay) behavior. So "Timed Relay" could change how "Check" works. It might automatically terminate the wait after 2 seconds for instance.

 

The best part about this, is that this child can be added. The for loop of relays can now contain Relay objects, and Timed Relay objects (as a Timed Relay is a Relay)!

 

This allows you to have, and add, different relay behavior, without changing any of the code that uses them.

 

A typical use for this is a "Simulated Relay". so you'll have a base class "IRelay" (the interface), a child "Relay" ( the real physical relay), and a child "Simulated Relay". While at the office testing, use an array of simulated relays, while in the field use an array of real relays. And again, the rest of the code does not need any modification.

 

 

 

 

Message 4 of 4
(2,199 Views)