OptoBlog

OptoNews Tech tip: Eval() in OptoScript?

Posted by Jean Femia on Apr 29, 2015 5:08:23 PM


PAC Control automation softwareHere's a tech tip for the more adventurous among you, who'd like to learn a faster, shorter way to code repetitious actions on similarly named variables or I/O.

A recent post on the OptoForum asked for an OptoScript equivalent to Eval(), a function used in a variety of programming languages, typically to act on expressions or operators passed in a string.

We won't get into the question of whether Eval() is "evil" or not (apparently it has a reputation for trouble in more than one language). Instead, we'll move right into how you can do good things by using a similar approach in your PAC Control strategy—and clean up a lot of spaghetti code in the process. 

In this Forum case, a large number of tags in the poster's strategy had similar names (Digi0, Digi1, and so on). The same value was applied to each tag individually—which meant a lot of code.

Fortunately, when you have several similar tags that need similar logic, PAC Control gives you options for grouping them to automate your automation logic: 
  • If you have several related integers or floats, you can use a Numeric Table.
  • For a set of names, labels, or the like, you can use a String Table.
  • Or if you need to loop through different types of tags (like all the I/O units in your strategy), you can use a Pointer Table. (A good example is this handy Product Support-recommended chart, which regularly loops through all your I/O units to maintain communication with them.)
But what if, instead of being grouped in a table, your tags are "grouped" by name?

You could load all those points into a pointer table, like the I/O Enabler chart mentioned above does. (Learn more about tables here; learn more about pointers here.)

But as this OptoForum user pointed out, some programming languages use Eval() to loop through regularly named variables. The solution used the command Get Pointer From Name to do something similar.

Sample code for PAC Control automation software

Check out the reply in the OptoForum post to see or join the discussion. 

One caveat: If someone changes the names (for example, if Digi0, Digi1, and so on became Pump0, Pump1, etc.), this logic will break. That’s why you’ll notice a check for null in the example.

So remember: You always have multiple options for writing your control logic, and it’s always a good idea to check on the OptoForums for some you might not have known!

Read the OptoForum post


OptoNews April 29, 2015 - Read more in this issue:

Topics: Tips, optonews, OptoNews 2015-04-29

Written by Jean Femia

Jean Femia writes about technical subjects and has focused on automation and control systems for more than 15 years. She likes learning about technology and taking corners in her Honda S2000.
Find me on:

    Subscribe to Email Updates

    Recent Posts

    Posts by Topic

    see all