Formulas are used for calculating values and statuses of input channels and calculating values of commands. Formulas processing is performed by the Server application.

Formulas are contained by the *Input channels* and *Output channels* tables of the configuration database. To enable the formula, tick the appropriate checkbox in the *Formula used* column. The *Formulas* table stores additional functions and data structures which can be used in formulas for input and output channels.

The general rules of using formulas:

- Formulas use the expressions syntax of C# language. Many of Microsoft .NET classes are accessible, for example, Math and DateTime classes.
- New constants, fields, properties and methods can be added and used in formulas.
- If at least one formula contains an error, Server operation is impossible. Information about errors in formulas is written in the Server application log.

The rules of using formulas for input channels:

- For channels of types
*Discrete*and*Real*calculation starts when the channels data are received by Server. Use these channel types if formula does not refer to data of other channels. - For channels of types
*Calculated **and*Switching counter*calculations are performed permanent and ordered by channel numbers. A formula of a calculated channel usually refers to data of other channels. - For channels of types
*Minute **and*Hourly **calculation starts periodically once per minute or once per hour. Use these channel types to create accumulated values, for example, energy consumption or operating time. - For channels of types
*Discrete*and*Real*statuses of the channels after calculating are equal to the statuses of these channels received by Server if status calculation does not specified explicitly. - For channels of the other types the statuses set to
*Defined*if status calculation does not specified explicitly. - An input channel formula that does not contain ";" (semicolon) symbol calculates the input channel value only.
- If an input channel formula contains ";", the formula calculates the input channel value and status. The first part before semicolon is a formula for value calculation and the second part after semicolon calculates status.

The rules of using formulas for output channels:

- Formulas are processed for output channels having
*Standard*and*Binary*command types. - A formula of an output channel defines command value calculation for a standard command and defines command data calculation for a binary command.

The variables accessible in formulas:

Variable | Value Type | Description |
---|---|---|

CnlVal, Cnl | double | The input channel value transmitted to Server before calculation |

CnlStat | int | The input channel status transmitted to Server before calculation |

CmdVal, Cmd | double | The command value transmitted to Server before calculation |

CmdData | byte[] | The command data transmitted to Server before calculation |

CnlNum | int | The channel number for which the formula is calculated |

E | double | The natural logarithmic base, specified by the constant, e |

PI | double | The ratio of the circumference of a circle to its diameter, specified by the constant, π |

The functions accessible in formulas:

Fucntion | Value Type | Description |
---|---|---|

N(n) | int | Returns the specified channel number for updating numbers on cloning |

Val() | double | Gets the current value of the formula channel |

Val(n) | double | Gets the current value of the channel n |

SetVal(n, val) | double | Sets the current value of the channel n |

Stat() | int | Gets the current status of the formula channel |

Stat(n) | int | Gets the current status of the channel n |

SetStat(n, stat) | int | Sets the current status of the channel n |

SetData(n, val, stat) | double | Sets the current value and status of the channel n |

Abs(x) | double | Calculates the absolute value of a number |

Sin(x) | double | Calculates the sine of the specified angle |

Cos(x) | double | Calculates the cosine of the specified angle |

Tan(x) | double | Calculates the tangent of the specified angle |

Exp(x) | double | Calculates e raised to the specified power |

Ln(x), Log(x) | double | Calculates the natural (base e) logarithm of a specified number |

Sqr(x) | double | Calculates the square of a specified number |

Sqrt(x) | double | Calculates the square root of a specified number |