Touchwin C Language Manual
Touchwin C Language Manual
Users manual
Catalog
1.Introduction .......................................................................................... 3
2.C function ............................................................................................. 3
2.1 Functions .................................................................................. 3
2.1.1 Public functions ............................................................... 3
2.1.2 Performance functions ..................................................... 4
2.2 Predefined value type ................................................................. 4
2.3 Predefined value table............................................................... 4
2.3.1 Constant .......................................................................... 4
2.3.2 Serial port ........................................................................ 4
2.3.3 Internal registers .............................................................. 5
2.3.4 Value length ..................................................................... 5
2.4 Macro ......................................................................................... 5
2.5 Direct operation for HMI internal registers ................................. 6
2.5.1 PSW registers .................................................................. 6
2.5.2 PSB operation functions .................................................. 6
2.6 Notice ......................................................................................... 7
3.Application ........................................................................................... 8
3.1 Purpose ..................................................................................... 8
3.2 Device ...................................................................................... 8
3.3 Reference manual ..................................................................... 8
3.4 Steps ......................................................................................... 8
4. Make the project .................................................................................. 9
4.1 Make C function......................................................................... 9
4.2 Edit the C function .................................................................. 10
4.3 Make the HMI program .......................................................... 12
Appendix 1 System tips ......................................................................... 14
Appendix 2 API functions ...................................................................... 15
Appendix 3 The calling limit for C function........................................... 19
2/19
1. Introduction
C language function is added in the Touchwin software v2.C.6 and higher
version. With this new function; TH and TP series HMI can realize more
complicated operations.
We will explain the C function programming rules with simple examples.
2. C function
2.1 Functions
The C function writing mode is the same as C language. C functions
include public functions and performance functions.
return dwCrc;
}
Call performance functions in public functions:
void CallFunction()
{
Func1();
}
3/19
4/19
=0
=1
=2
=3
=4
=5
=6
//
// bit
// byte
// word
// double words
// register group
//
2.4 Macro
1. Max(a, b)
Example: Max(3, 4) == 4
2. Min(a, b)
Example: Min(3, 4) == 3
3. Combine two bytes in one word
MAKEWORD(lb, hb)
Example: MAKEWORD(0x01, 0x02)== 0x0201
4. combine two words in one double words
MAKELONG(lw, hw)
Example: MAKELONG(0x01, 0x02)== 0x00020001
5. Obtain the low byte of one word
LOBYTE(w)
Example: LOBYTE(0x0201) == 0x01
5/19
Example:
// PSB(301) = PSB(300)
if( GetPSBStatus(300) )
SetPSB( 301 );
else
6/19
ResetPSB( 301 );
2.6 Notice
1. When input API function, make sure the function name is together with
( and no space between them. Dialog and tip box will pop up by doing
this.
2. Capital and small letter is distinguished for the code.
3. It cannot assign initial value to the global variables defined in the
public function. The default value of global variables is 0.
4. When define the variables, the type must be the same to the data
source.
5. The performance function name must be English and cannot be the
same with others.
6. Press F7 to compile the program.
7. When declare the variables (global or local variables), dont declare the
array whose space larger than 128 bytes. It can use special space
allocation function.
8. Cannot call malloc/ free directly, but please use Malloc / Free (first
letter is capital).
9. The execution environment of performance function is multi-tasking.
The execution mode of performance function: parallel execution,
sequential execution.
Sequential execution: The task which calls the performance function
enables to do the next operation after the performance function execution
is finished. So performance function must have suitable exit condition.
Parallel execution: The task which calls the performance function will
build new task to execute the function. The task will do the next
operation.
10. Use carefully as the multi-task system has task lock.
7/19
3. Application
3.1 Purpose
Get 3 integral from the PLC, show the min and max ones on the screen.
3.2 Device
This project needs the following devices:
1. TH series HMI: TH465-MT 1pcs
2. XC series PLC: XC3-24R-E 1pcs
3. Software: Touchwin version 2.c.6 and higher
4. Cables: USB download cable 1pcs, PLC cable 1pcs, PC
3.4 Steps
1.
2.
3.
4.
8/19
9/19
10/19
11/19
12/19
5. Download the HMI program to the TH465. Connect PLC with the
TH465, power on. Input any value in a, b, c. Click Func button, the max
and min value will show on the screen.
13/19
14/19
15/19
/**********************************************************
*************************
Function: read and write register array
comID: serial port (HMI_LOCAL_MCH = -1
DOWNLOAD = 0,
PLC = 1)
staID: station number
objType: register address type
add1: register address
regs: register quantity
pRegs: value buffer (the length must be match to the read&write register
array)
return value: TRUE / FALSE (successful/failed)
***********************************************************
*************************/
BOOL Reads(int comID, int staID, int objType, int add1, int regs,
void* pRegs);
BOOL Writes(int comID, int staID, int objType, int add1, int regs,
void* pRegs);
Example:
WORD wArray[10];
Reads(PLC, 1, XINJE_XC_REGS_D, 0, 10, wArray);
Writes(PLC, 1, XINJE_XC_REGS_D, 0, 10, wArray);
/**********************************************************
*************************
Function: Enter, Leave: signal control, ensure the communication is
synchronization mode. Use together with send and receive.
***********************************************************
*************************/
void Enter( BYTE ComID );
void Leave( BYTE ComID );
/**********************************************************
*************************
Function: send data of serial port
comID: serial port (DOWNLOAD = 0, PLC = 1)
SndBuf: sending buffer, the type is byte
Len: sending data length, count as byte
Return value: TRUE / FALSE (successful/failed)
/**********************************************************
**************************/
BOOL Send( BYTE ComID, BYTE *SndBuf, WORD Len );
/**********************************************************
16/19
*************************/
Function: receive the data of serial port
comID: serial port (DOWNLOAD = 0, PLC = 1)
RcvBuf: receiving buffer, the type is byte
Len: receiving data length, count as byte
Timeout: receiving timeout time (0:always waiting). Unit: ms
TimeOutByte: receiving timeout time of bytes (set to 6)
Return value: received data length, count as byte
/**********************************************************
**************************/
WORD Receive( BYTE ComID, BYTE *RcvBuf, WORD Len, WORD
TimeOut, BYTE TimeOutBytes);
Exp:
BYTE byArray[10] = {0x00, .};
Enter(PLC);
// apply the serial port
Send(PLC, byArray, 10);
Receive(PLC, byArray, 10, 0, 6);
Leave(PLC);
// release the serial port
/**********************************************************
*************************
Function: Malloc, Free: instead of malloc, free.
Note: please release the applied space in time
***********************************************************
*************************/
/**********************************************************
*************************
Function: apply heap space
Size: apply the space size(bytes)
Return: the applied space address, NULL means the application is failed
***********************************************************
*************************/
void *Malloc( UINT size )
/**********************************************************
*************************
Function: release heap space
pBuffer: the space ready to release
***********************************************************
*************************/
void Free( void *pBuffer)
Example: BYTE* pBuffer = Malloc(10);
Free(pBuffer)
17/19
/**********************************************************
*************************
Function: Lock, UnLock: task lock, use them in pairs
Note: the locked area: access manage for global variables, make the
locked area as small as possible
Suitable case: the task lock is needed when there are many performance
functions need access for one global variable
***********************************************************
*************************/
void Unlock (void);
void Lock (void);
/**********************************************************
*************************
Function: Delay
ms: delay time (unit:ms), the max delay time = 0xFFFF * delay precision
delay precision: TP series(except TPA61-T) and OP series are 10ms;
TPA61-T and TH series are 5ms
***********************************************************
*************************/
void Delay( UINT ms);
Example:
Delay(1000);
// delay 1s
18/19
19/19