Macro arithmetic for beginners

Introduction:

Within this post, you will learn how to read and manipulate numeric data via simple arithmetic operations using Macros.

Software version:

EasyBuilder Pro 6.03.02.463+
Note: Certain options within objects or windows may be placed in other locations in different versions. The example below was designed with 6.09.01.524.

Instructions:

  1. Locate the “Macro” icon within the “Project” tab.

  2. Within the “Macro Manager”, select “New” to create a new macro within the program.

  3. Within the macro workspace, define an input and result variable using a data-type that corresponds with the data you intend to read as in the following example:

Declaration Type Equivalent Range
bool bit BOOLEAN 0 ~ 1
char byte (signed) SINT -128 ~ 127 Note 2
short 16-bit (signed) INT/WORD -32768 ~ 32767 Note 2
int 32-bit (signed) DINT/DWORD -2147483648 ~ 2147483647
float 32-bit (float) REAL
unsigned char byte (unsigned) USINT 0 ~ 255
unsigned short 16-bit (unsigned) UINT 0 ~ 65535
unsigned int UDINT 0 ~ 4,294,967,295
long LINT -248 ~ 248 Note 1
unsigned long ULINT 0 ~ 248 Note 1
double LREAL -248 ~ 248 Note 1

Note: In this example, I will declare a floating-point variable to hold device input called “Input” and one to output the result called “result”.

  1. To read device data, move your cursor to a new line and select the “GET/SET FN…” button to open the API:

  2. Within the “Function name” drop-down list select “GetData” to read device data:

  3. In this demonstration, I will set the “Variable” to target my “Input” variable and configure an appropriate “Read” address:
    Note: The “Data count” entry box may be used when reading multiple elements into an array variable. When reading one data point, we can leave the ‘count’ set to 1.

  4. Click “OK” when finished and the function will be added to the same line as your cursor:

  5. After reading device data, we can manipulate the input using the following arithmetic operators using parenthesis to determine the intended order of operations:

Operator sign
Assignment =
Addition +
Subtraction -
Multiplication *
Division /
Modulo division
(remainder)
% or mod
  1. Use these operators freely to carry out the scaling or manipulation of the input variable and assign the result to the ‘result’ operator:
    Note: The equation below will scale an input within the range of 4 - 20 to a number between 0 - 100. By default, integer operation will truncate decimal data so a ‘.0’ may be appended to the number to ensure that the resulting number contains decimal data. This means that 100.0 / 16.0 will return 6.25, but 100 / 16 will return 6 because the fractional portion of this result is truncated.

  2. To output the result, select the “GET/SET FN…” button to open the API. Within the API, select the “SetData” function to write this data to a device register or tag.
    Tip: Set the “Category” to “PLC” to select the “SetData” or “GetData” function quickly.

  3. The Macro can be made to execute cyclically or “periodically” by enabling “Periodical execution”:

  4. When finished, select “Save & compile” to ensure that the macro is error free:

  5. When not using “Periodical execution” a “Combo Button” or “Function Key” may be used to execute a Macro each time the object is clicked:

Notes:

  • The “Periodical execution” time specifies the minimum amount of time that the HMI will wait before re-executing this Macro. As an example, if the HMI executes the Macro within 20ms and the periodical execution time is set to 100ms, the HMI will wait an additional 80ms before executing this Macro again. If the Macro requires more time than allowed by the “Periodical execution” to complete, the HMI will begin a new instance of this Macro as soon as the current instance is complete.

  • When using a GetData or SetData statement, it is possible that the HMI will not complete execution if communication is lost. If the HMI cannot retrieve data from the target device, it will wait the length of the “timeout” and then halt execution entirely. To force the macro to complete even when communication is lost, please use a GetDataEx statement instead as described within the “Macro reference guide”.