05.05.2014 Views

csmstr - Omega Engineering

csmstr - Omega Engineering

csmstr - Omega Engineering

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

RETURNING VALUES<br />

CRIMSON USER MANUAL - MODULAR CONTROLLER<br />

RETURNING VALUES<br />

As mentioned above, programs can return values. Such programs can be invoked by other<br />

programs or by expressions anywhere in the database. For example, if you want to perform a<br />

particularly complex decode on a number of conditions relating to a motor and return a value<br />

to indicate the current state, you could create a program that returns an integer like this…<br />

if( MotorRunning )<br />

return 1;<br />

else {<br />

if( MotorTooHot )<br />

return 2;<br />

if( MotorTooCold )<br />

return 3;<br />

return 0;<br />

}<br />

You could then configure a multi-state formula to invoke this program, and use that tag’s<br />

format tab to define the names of the various states. The invocation would be performed by<br />

setting the tag’s Value property to Name(), where Name is the name of the program in<br />

question. The parentheses are used to indicate a function call, and cannot be omitted.<br />

HERE BE DRAGONS!<br />

Note that you have to exercise a degree of caution when using programs to return values. In<br />

particular, you should avoid looping for long periods of time, or performing actions that make<br />

no sense in the context in which the function will be invoked. For example, if the code<br />

fragment above called the GotoPage function to change the page, the display would change<br />

every time the program was invoked. Imagine what would happen if you, say, tried to log<br />

data from the associated tag, and you’ll realize that this would not be a good thing! Therefore,<br />

keep programs that return values simple, and always consider the context in which they will<br />

be run. If in doubt, avoid doing anything other than simple math and if statements.<br />

PASSING ARGUMENTS<br />

As also mentioned above, program can accept arguments. As an example, suppose you want<br />

to write a program called FindMean to take the average of two values. The program could be<br />

configured to accept two integer arguments, a and b, as shown in the example given when<br />

defining the purpose of the Arguments property. The program would also be configured so as<br />

to return a integer value. The code within the program would then be defined as…<br />

return (a+b)/2;<br />

Once this program has been created and translated, you will be able to enter an expression<br />

such as FindMean(Tag1, Tag2) to invoke it with the appropriate arguments. In this case, the<br />

expression will be equal to the average of Tag1 and Tag2.<br />

PAGE 228<br />

http://www.redlion.net/controller

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!