Introduction
The purpose of this article is to provide information about how to program C language (ANSI C89) Operators and how do they work within a Macro Function in a NB HMI.
Background Information
Macro is an advanced HMI control method, which makes the function of the HMI more powerful. The HMI can have the same logic and arithmetic functions with the PLC through the Macro programming. Using Macro flexibly can realize more powerful functions that can not be realized by the common component, and make the HMI more perfect.
NB-Series PT provides the new Macros different from the Macro scripting language mode in the other HMIs, and the Macro is compatible with the standard C language (ANSI C89) completely.
Content
- What can a Macro Function do
- How to create a Macro
- Macro Variables
- Data types used in Macro Variable
- How to trigger a Macro
- C Language (ANSI C89) Syntax
- Example syntax 'NB HMI Operators'
1. What can a Macro Function do
A Macro Function can do the following (supported) items:
- Read / Write values to and / or from Local memory and Host memory that can be accessed via com. driver
- Using Mathematic Functions such as +, +=, ++, & and more
- Control the flow with Do..While.., Switch and more
- Create graphics on the NB screen
2. How to create a Macro
Press the (Add Macrocode) icon in the database toolbar as shown below, or click [Macrocode…] in the [Option] menu.
3. Macro Variables
Macro variables are classified into two types: internal variables and external variables.
Internal variables: Refers to the HMI’s own register units. Internal variables can be defined in [Macrocode Variable Window] and also can be used in [Macrocode Editing Window].
External variables: Refers to the register units read or written by the HMI from external controllers. External variables must be defined in [Macrocode Variable Window] beforehand, and then can be used in [Macrocode Editing Window].
4. Data types used in Macro Variable
The data types used in Macro Variable are shown in the table below.
5. C Language (ANSI C89) Syntax
After creation a Macro Function contains the following C Language (ANSI C89) code:
#include "macrotypedef.h" #include "math.h"/*
Read,Write Local address function:
int ReadLocal( const char *type, int addr, int nRegs, void *buf, int flag );
int WriteLocal( const char *type, int addr, int nRegs, void *buf , int flag );Parameter: type is the string of "LW","LB" etc;
address is the Operation address ;
nRegs is the length of read or write ;
buf is the buffer which store the reading or writing data;
flag is 0,then codetype is BIN,is 1 then codetype is BCD;
return value : 1 ,Operation success
0, Operation fail.eg: read the value of local lw200 and write it to the lw202,with the codetype BIN,
The code is :short buf[2] = {0};
ReadLocal("LW", 200, 2, (void*)buf, 0);
WriteLocal("LW", 202, 2, (void*)buf, 0);*/
int MacroEntry()
{
return 0;
}
The Macro Function adds two pre-defined libraries to its code; "Macrotypedef.h" and "Math.h". "Macrotypedef.h" embedds functions and structured data such as memory access and graphic drawing. While "Math.h" embedds fixed parameters and functions for mathematic calculation. The following button shows the embedded functions that are supported in the NB HMI.
Furthermore, the syntax is an important aspect while writing commands in the Macro Function. NB-Designer only has a built-in compiler; this means that NB-Designer does not check the syntax on the fly. However during compilation NB-Designer will check for errors and / or warnings. The following code represents several syntax examples such as the usage of ; at the end of a code line, usage of the 'if' statement and how to create comments.
int MacroEntry() { c = a + b; if ( c == 0 ) { c = 1; } return; // Put your comment here /* Put your comment here */ }
6. How to trigger a Macro
A Macro can be triggered in different ways. A Macro can be used upon start-up of the NB, it can be used with a trigger such as a Timer, Function Key and many more functions within the NB.
The figure below displays how to trigger a Macro, a Multiple State Setting Component is used as an example.
7. Example syntax 'NB HMI Operators'
The attachment: 'NB HMI Operators' is a sample program to show the syntax of Operators including a description for each operator.
Note: This program does not describe C language programming instead it only concentrates on the syntax of C programming of Operators and several 'nice to know' functions.
Note: Two warnings occur during compilation. These warnings are related with the pointer function, please refer Macro code for more information.
Important Notes
- Delete a Macro with caution because any function that would otherwise trigger the deleted Macro will trigger another Macro instead.
Reference
NB Series NB-Designer Operation Manual - chapter 3-9 Macro Function
Tutorial about C-programming