Touchwin C Language Manual
Touchwin C Language Manual
User’s manual
2/24
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
Example:
DWORD Crc(BYTE* pBytes, int Length)
{
DWORD dwCrc = 0;
…
return dwCrc;
}
3/24
2.1.2 Performance functions
Example:
BYTE byArray[10];
DWORD dwCrc = 0;
dwCrc = Crc(byArray, 10); // call public function
//
enum ECom
{
HMI_LOCAL_MCH = -1,
DOWNLOAD,
PLC,
EComMax
};
enum EInnerReg
{
TYPE_PSB = 0,
TYPE_PSW,
TYPE_PFW,
TYPE_PRW,
4/24
TYPE_PHW,
TYPE_PUW,
TYPE_PCW,
EInnerRegMax
};
enum EDataType
{
TYPE_NONE,
TYPE_BIT,
TYPE_BYTE,
TYPE_WORD,
TYPE_DWORD,
TYPE_REGS,
TYPE_BYTE_3
};
2.4 Macro
5/24
6. Obtain the high byte of one word
HIBYTE(w)
Example: HIBYTE(0x0201)== 0x02
Explanation:
comID: serial port (HMI_LOCAL_MCH=-1, DOWNLOAD=0, PLC=1)
staID: station no.
objType: register address type
add1, add2: register address
dataType: TYPE_BIT =1 occupy 1 byte
TYPE_BYTE =2 occupy 1 byte
TYPE_WORD =3 occupy 2 bytes
TYPE_DWORD =4 occupy 4 bytes
pValue: data buffer (the length must match dataType)
return value: TRUE/FALSE (succeed/fail)
Example:
BOOL bValue; //define a boolean variable
WORD wValue; //define a integer variable
Read(PLC,1,XINJE_XC_BIT_M,0,0,TYPE_BIT,&bValue); //read M0
Read(PLC,1,XINJE_XC_REG_D,0,0,TYPE_WORD,&wValue);//read
D[0]
6/24
Explanation:
comID: serial port (HMI_LOCAL_MCH=-1, DOWNLOAD=0, PLC=1)
staID: station no.
objType: register address type
add1: register address
regs: register quantity
pRegs: value buffer (length must match read&write register group)
Example:
WORDwValue[10]; //define a integer variable
Reads(PLC,1,XINJE_XC_REGS_D,0,1,wValue); //read D[0] group
Example:
Enter(PLC);
Leave(PLC);
Example:
BYTERcvArray[8]={0x01,0x01,0x00,0x10,0x00,0x10,0x3C,0x03}
Send(PLC,RcvArray,8)
7/24
Len: receive data length, count by byte
Timeout: receiving timeout time (0: always wait). Unit: ms
TimeOutBytes: byte receiving timeout time (set to 6)
Return value: receiving data length, count by byte
Example:
BYTEbyArray[10];
Receive(PLC,byArray,10,0,6);
Example:
Unit* p;
P=(Unit*) malloc (sizeof(Unit));
15. use with malloc() in pair, free the space applied by malloc
Void Free(void*pBuffer)
Example:
BYTE*pBuffer=Malloc(10);
Free(pBuffer)
Note: the locked area must be access management for global variables;
the lock area must be as small as possible.
Suitable case: if there are many performance functions need to access for
the same global variable, the task lock is needed.
8/24
17. Delay
Void Delay(UNIT ms);
Example:
Delay(10); //delay 10ms
Delay(1000); //delay 1s
Example:
ScreenJump(2); // jump to screen No.2
Example:
OpenWindow(2,10,10); //display window no.2 at (10,10)
Example:
CloseWindow(2); //close window no.2
21. A buzzer
Void Beep(void);
Example:
Beep();
9/24
22. Light the screen (from the screen save state)
Void LightScreen(void);
Example:
LightScreen();
Example:
BYTEbuffer[3]={0x01,0x02,0x03};
UNIT nCrc=Crc(buffer,3); // put the checksum of array buffer Crc to
variable nCrc
10/24
DWORD dwValue=MAKEDWORD(PSW[300],PSW[301]);
/ / assign the value of PSW[300] and PSW[301] to one double words
SetPSB(PSB_No); // PSB=1
Example: SetPSB(300); //PSB300=1
ResetPSB(PSB_No); // PSB=0
Example: ResetPSB(300); //PSB300=0
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), don’t declare the
array whose space larger than 128 bytes. It can use special space
allocation function.
11/24
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.
3. Application
3.1 Purpose
Get 3 integral from the PLC, show the min and max ones on the screen.
3.2 Device
12/24
3.4 Steps
13/24
3. Click Func1 to edit the C function.
15/24
Write(HMI_LOCAL_MCH,0,TYPE_PSW,300,0,TYPE_WORD,max);
//write max value to PSW300
Write(HMI_LOCAL_MCH,0,TYPE_PSW,301,0,TYPE_WORD,min);
//write min value to PSW301
1. Put 3 digital input buttons on the screen. Set the address to D0, D2 and
D4.
2. Put 2 digital display buttons on the screen. Make the address to
PSW300 and PSW301.
3. Put 1 function button on the screen to call the Func1.
16/24
4. The final screen:
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.
17/24
Appendix 1 System tips
WORD a,b,c,g[4];
Read(PLC, 1, XINJE_XC_BIT_M, 0, 0, TYPE_BIT, &b);
//Read the value of M0 to b
Read(PLC,1,XINJE_XC_REG_D,2,0,TYPE_WORD,&b);
//Read the value of D2 to b
Reads(PLC, 1, XINJE_XC_REGS_D, 10, 4, g);
//Read the value of D10~D13 to g
Read(HMI_LOCAL_MCH, 0, TYPE_PSB, 300, 0, TYPE_BIT, &a);
//Read the value of PSB300 to a
Read(HMI_LOCAL_MCH, 0, TYPE_PSW, 300, 0, TYPE_WORD, &b);
//Read the value of PSW300 to b
Reads(HMI_LOCAL_MCH, 0, TYPE_PFW, 300, 4, g);
//Read the value of PFW300~PFW303 to array g
Write(PLC, 1, XINJE_XC_BIT_M, 0, 0, TYPE_BIT, 0);
//Set OFF M0
Write(PLC, 1, XINJE_XC_REG_D, 50, 0, TYPE_WORD, a);
//Write the value of a to D50
Writes(PLC, 1, XINJE_XC_REGS_D, 20, 4, g);
//write the value of array g to D20~D23
Write(HMI_LOCAL_MCH, 0, TYPE_PSB, 300, 0, TYPE_BIT, 1);
//Set ON PSB300
Write(HMI_LOCAL_MCH,0,TYPE_PSW,300,0,TYPE_WORD,max);
//write the value of max to PSW300
Writes(HMI_LOCAL_MCH, 0, TYPE_PFW, 300, 4, g);
//write the value of array g to PFW300~PFW303
18/24
Appendix 2 API functions
Take XC as an example:
/**********************************************************
*************************
Function: read and write register (for bit and register)
comID: serial port (HMI_LOCAL_MCH = -1
DOWNLOAD = 0,
PLC = 1)
staID: station number
objType: register address type
add1,add2: register address
dataType: TYPE_BIT = 1 occupy 1 byte
TYPE_BYTE = 2 occupy 1 byte
TYPE_WORD = 3 occupy 2 bytes
TYPE_DWORD = 4 occupy 4 bytes
pValue: value buffer (the length must match to dataTyp)
return value: TRUE / FALSE (successful/ failed)
***********************************************************
*************************/
BOOL Read (int comID, int staID, int objType, int add1, int add2,
int dataType, void* pValue);
BOOL Write(int comID, int staID, int objType, int add1, int add2,
int dataType, DWORD dwValue);
Example:
BOOL bValue = FALSE;
WORD wValue = 0;
Read(PLC, 1, XINJE_XC_BIT_M, 0, 0, TYPE_BIT, &bValue);
// read the bit
Read(PLC, 1, XINJE_XC_REG_D, 0, 0, TYPE_WORD, &wValue);
//read D[0]
Read(HMI_LOCAL_MCH, 0, TYPE_PFW, 300, 0, TYPE_WORD,
&wValue); //read PFW[300]
Write(HMI_LOCAL_MCH, 0, TYPE_PFW, 300, 0, TYPE_WORD,
wValue); //write PFW[300]
19/24
/**********************************************************
*************************
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 );
/**********************************************************
20/24
*************************/
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, ….};
/**********************************************************
*************************
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)
21/24
/**********************************************************
*************************
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
22/24
Appendix 3 The calling limit for C function
This chapter will introduce the restricted library functions.
Most C functions can be used normally (except heap functions). However,
the following functions are limited when using.
1. The functions in alloca.h cannot be called, they are related to heap.
2. The assert functions in assert.h cannot be called.
3. The stream functions in stdio.h cannot be called, only the functions
for string (sscanf, sprint) can be used normally.
4. The heap functions in stdlib.h cannot be called, but they can be
instead by API functions.
23/24
WUXI XINJE ELECTRIC CO., LTD.
4th Floor, Building 7th, No.100 Dicui Rd,
Wuxi, China
Tel: 86-0510-85134139
Fax: 86-0510-85111290
www.xinje.com
Email: [email protected]
24/24