Lập trình C cho WINCC – Tổng hợp code C và VB - Phần 2
=========================================================================
#include “apdefap.h”
void OnClick(char* lpszPictureName,
char* lpszObjectName,
char* lpszPropertyName)
char* lpszObjectName,
char* lpszPropertyName)
{
SetVisible(lpszPictureName,“@ERROR”,FALSE);
SetOperation(lpszPictureName,“@LoginIn”,TRUE);
SetOperation(lpszPictureName,“@PasswordIn”,TRUE);
SetOperation(lpszPictureName,“@OK”,TRUE);
Set_Focus(lpszPictureName,“@LoginIn”);
SetVisible(lpszPictureName,lpszObjectName,FALSE);
}
================================================================
void OnClick(char* lpszPictureName,
char* lpszObjectName,
char* lpszPropertyName)
char* lpszObjectName,
char* lpszPropertyName)
{
// WINCC:TAGNAME_SECTION_START
// syntax: #define TagNameInAction DMTagName
// next TagID : 1
#define Apc_CurrentUser “@CurrentUser”
// WINCC:TAGNAME_SECTION_END
// WINCC:PICNAME_SECTION_START
// syntax: #define PicNameInAction PictureName
// next PicID : 1
#define Apcpictu_0_startpicture_00_PDL “pictu_0_startpicture_00.PDL”
// WINCC:PICNAME_SECTION_END
int i, j, n;
char pic_name[256];
char obj_name[60];
n = strlen( lpszPictureName );
for
( i = n; i >
0; i—
)
( i = n; i >
0; i—
)
if
( lpszPictureName[i]
==
‘.’
)
{
strncpy( pic_name, lpszPictureName, i );
strcpy( obj_name,
&lpszPictureName[i+1]
);
&lpszPictureName[i+1]
);
for
( j =
0; j <
(n–i); j++)
if
( obj_name[j]
==
‘:’
)
{
obj_name[j]
=
”;
=
”;
break;
}
SetVisible(pic_name,obj_name,FALSE);
break;
}
/*if ((strcmp(GetTagChar(Apc_CurrentUser),””))==0) {
SetVisible(Apcpictu_0_startpicture_00_PDL,”Picture Window6″,1);
}*/
}
==================================================================
// Di chuyen cheo
#include “apdefap.h”
long _main(char* lpszPictureName,
char* lpszObjectName,
char* lpszPropertyName)
{
static
int x1=10,y1=20;
int x1=10,y1=20;
if(GetTagBit(“start”)==1) x1+=20;
y1=(int)((0.391)*x1+16.09);
if(x1>=470)
{
x1=470; y1=200;
}
SetTagDWord(“oto”,y1);
//Return-Type: BOOL
//Return-Type: BOOL
return x1;
}
//////////////////////////////////
di chuyen cheo chuan
#include “apdefap.h”
long _main(char* lpszPictureName,
char* lpszObjectName,
char* lpszPropertyName)
{
static
int x1=640,y1=350;
int x1=640,y1=350;
static
int x2=770,y2=480;
int x2=770,y2=480;
static x=x1,y=y1;
//x=x1,y=y1;
if(GetTagBit(“start”)==1)
{
x+=10;
y=(int)((y2–y1)*(x–x1)/(x2–x1)+y1);
if(x>=x2)
{
x=x2; y=y2;
}
}
SetTagDWord(“oto”,y);
//Return-Type: BOOL
//Return-Type: BOOL
return x;
}
=====================================================================================
Option Explicit
Sub OnLButtonDown(Byval Item, Byval Flags, Byval x, Byval y)
Dim SMSSend
Dim SMS
Dim buffer
MsgBox “Xin chao”
‘ This is the name of the MSComm Control
Set SMSSend = ScreenItems(“Control 1”)
‘Comunicacion puerto COM
SMSSend.CommPort =
1
1
SMSSend.Settings =
“9600,E,8,1”
“9600,E,8,1”
SMSSend.InputLen =
0
0
SMSSend.Handshaking =
0
0
SMSSend.PortOpen = True
SMSSend.Output =
“atE0”
& Chr(13)
“atE0”
& Chr(13)
Do
buffer = buffer & SMSSend.Input
Loop Until InStr(buffer,“OK”)
buffer =
“”
“”
SMSSend.Output =
“AT+CMGF=1”
& Chr(13)
“AT+CMGF=1”
& Chr(13)
Do
buffer = buffer & SMSSend.Input
Loop Until InStr(buffer,“OK”)
buffer =
“”
“”
SMSSend.Output =
“AT+CMGS=”&“+34X X X X X X X X X “
& Chr(13)+“&”+ Len(SMS)/2+“&”+Chr(13)
“AT+CMGS=”&“+34X X X X X X X X X “
& Chr(13)+“&”+ Len(SMS)/2+“&”+Chr(13)
Do
buffer = buffer & SMSSend.Input
Loop Until InStr(buffer,
“>”)
“>”)
buffer =
“”
“”
SMSSend.Output =
“M ens aj e prueba”
+ Chr(26)
“M ens aj e prueba”
+ Chr(26)
Do
buffer = buffer & SMSSend.Input
Loop Until InStr(buffer,
“OK”)
“OK”)
‘ try to force s ends by cl os i ng the ports and reopen i n l oop
SMSSend.PortOpen = False
MsgBox “Tam Biet”
End Sub
============================================================
Open Port
Sub OnClick(Byval Item)
Dim objMSComm1, tagConnection
Set objMSComm1 = HMIRuntime.Screens(“Main”).ScreenItems(“MSComm1”)
Set tagConnection = HMIRuntime.Tags(“Connection”)
If objMSComm1.PortOpen = False Then
‘ Assign com port number
objMSComm1.Commport =
1
1
‘ Values: 9600 Baud, N – No Parity, 8 – Databit, 1 – Stopbit
objMSComm1.Settings =
“9600,N,8,1”
“9600,N,8,1”
objMSComm1.RThreshold =
1
1
objMSComm1.SThreshold =
1
1
objMSComm1.InputLen =
0
0
objMSComm1.PortOpen = True
tagConnection.Write(True)
HMIRuntime.Trace(“Port open.”
& vbCrLf)
& vbCrLf)
Else
HMIRuntime.Trace(“Port is already opened.”
& vbCrLf)
& vbCrLf)
End If
End Sub
=============================================================
Close Port
Sub OnClick(Byval Item)
Dim objMSComm1, tagConnection
Set objMSComm1 = HMIRuntime.Screens(“Main”).ScreenItems(“MSComm1”)
Set tagConnection = HMIRuntime.Tags(“Connection”)
If objMSComm1.PortOpen = True Then
objMSComm1.PortOpen = False
tagConnection.Write(False)
HMIRuntime.Trace(“Port close.”
& vbCrLf)
& vbCrLf)
End If
End Sub
==================================================================
Read Buffer
Option Explicit
Function action
Dim strBuffer, strTemp
Dim objMSComm1, tagBuffer
Set objMsComm1 = HMIRuntime.Screens(“Main”).ScreenItems(“MSComm1”)
Set tagBuffer = HMIRuntime.Tags(“Buffer”)
strTemp =
“”
“”
If objMSComm1.PortOpen = True Then
‘read the buffer
strTemp = CStr(objMSComm1.Input)
If strTemp <>
“” Then
“” Then
‘checking for the delimited character
If InStr(strTemp, Chr(6)) Then
strBuffer = Left(strTemp,Len(strTemp)-1)
Else
strBuffer = strTemp
End If
tagBuffer.Value = strBuffer
tagBuffer.Write
End If
Else
HMIRuntime.Trace(“No port is opened!”
& vbCrLf)
& vbCrLf)
End If
End Function
=====================================================================
Send Data
Sub OnClick(ByVal Item)
Dim tagOutput, objMSComm1
Set tagOutput = HMIRuntime.Tags(“Output”)
Set objMSComm1 = HMIRuntime.Screens(“Main”).ScreenItems(“MSComm1”)
If objMSComm1.PortOpen = True Then
tagOutput.Read
objMSComm1.Output = tagOutput.Value
tagOutput.Write(“”)
Else
HMIRuntime.Trace(“No port is opened!”
& vbCrLf)
& vbCrLf)
End If
End Sub
===================================================================
#define Apc_lower_limit “lower_limit”
#define Apc_upper_limit “upper_limit”
// next TagID : 4
// WINCC:TAGNAME_SECTION_END
// WINCC:PICNAME_SECTION_START
// syntax: #define PicNameInAction “PictureName”
// next PicID : 1
// WINCC:PICNAME_SECTION_END
double a;
double lolmt;
double uplmt;
SetOutputValueDouble(lpszPictureName,“Setpoint_I”,strtod(GetOutputValueChar(lpszPictureName,“DisplayNr”),NULL));
a=GetOutputValueDouble(lpszPictureName,“Setpoint_I”);
lolmt=GetTagFloat (Apc_lower_limit);
uplmt=GetTagFloat (Apc_upper_limit);
if
((a <= uplmt)
&&
(a >= lolmt ))
((a <= uplmt)
&&
(a >= lolmt ))
{
SetOperation(lpszPictureName,“-_Button”,1);
//Rückgabe-Typ :BOOL
//Rückgabe-Typ :BOOL
SetOperation(lpszPictureName,“._Button”,1);
//Rückgabe-Typ :BOOL
//Rückgabe-Typ :BOOL
SetOutputValueChar(lpszPictureName,“DisplayNr”,“”);
SetVisible(“PA2_bld01.PDL”,“Tastatur”,0);
//Rückgabe-Typ :BOOL
//Rückgabe-Typ :BOOL
SetTagDouble(Apc_Setpoint,a);
}
}
=======================================================
#include “apdefap.h”
void OnClick(char* lpszPictureName,
char* lpszObjectName,
char* lpszPropertyName)
char* lpszObjectName,
char* lpszPropertyName)
{
char* buffer;
char* s = GetText(lpszPictureName,lpszObjectName);
char help[255]=“”;
buffer=SysMalloc(strlen(GetOutputValueChar(lpszPictureName,“DisplayNr”))+5);
buffer = GetOutputValueChar(lpszPictureName,“DisplayNr”);
sprintf(help,“%s%s”,buffer,&s[0]);
SetOutputValueChar(lpszPictureName,“DisplayNr”,help);
SetOperation(lpszPictureName,“-_Button”,0);
//Rückgabe-Typ :BOOL
//Rückgabe-Typ :BOOL
SetOperation(lpszPictureName,“del_Button”,1);
//Rückgabe-Typ :BOOL
//Rückgabe-Typ :BOOL
}
====================================================================
#include “apdefap.h”
void OnClick(char* lpszPictureName,
char* lpszObjectName,
char* lpszPropertyName)
char* lpszObjectName,
char* lpszPropertyName)
{
// WINCC:TAGNAME_SECTION_START
// syntax: #define TagNameInAction DMTagName
// next TagID : 1
#define ApcVisibleTag03 “VisibleTag03”
// WINCC:TAGNAME_SECTION_END
// WINCC:PICNAME_SECTION_START
// syntax: #define PicNameInAction PictureName
// next PicID : 1
// WINCC:PICNAME_SECTION_END
SetVisible(lpszPictureName,“Tastatur”,0);
SetTagBit(ApcVisibleTag03,1–GetTagBit(ApcVisibleTag03));
}
Leave a Comment