Lập trình C cho WINCC – Tổng hợp code C VB Trong trình làm học project, có nghiên cứu chút phần lập trình C, VB WinCC Dưới số code tham khảo tự code Mong giúp phần cho bạn sử dụng WinCC code an/hien window picture #include “apdefap.h” void OnLButtonDown(char* lpszPictureName,char* lpszObjectName,char*lpszPropertyName, UINT Flags, int x,int y) { SetVisible(lpszPictureName,“set_oto”,1–GetVisible(lpszPictureName,“set_oto”)); } ======================================== void move_xy(char* lpszPictureName, char* lpszObjectName,int x1,int y1,int x2, int y2) { static int x0,y0; // Chuyen dong ve phia tang x x0=x1,y0=y1; if(x1=x2) { x0=x2; y0=y2; } } // Chuyen dong ve phia giam x else { x0-=10; y0=(int)((y2–y1)*(x0–x1)/(x2–x1)+y1); if(x0=500) a=500; if(a=500) b=500; if(b Authorization -> chọn quyền tương ứng mà bạn gán : gán quyền quản trị ko thể lên cửa sổ đăng nhập được, đăng nhập dùng cách ấn nút cho thêm C–Ation PASSLoginDialog(“1”); ==================================================================================== ====== #include “apdefap.h” void OnClick(char* lpszPictureName, char* lpszObjectName, char* lpszPropertyName) { #pragma code (“UseAdmin.DLL”) #include “pwrt_api.h” #pragma code () int i, j, n; char pic_name[256]; char obj_name[60]; PWRTLogout(); n = strlen( lpszPictureName ); for ( i = n; i > 0; i— ) if ( lpszPictureName[i] == ‘.’ ) { strncpy( pic_name, lpszPictureName, i ); strcpy( obj_name, &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; } } ========================================================================= #include “apdefap.h” void OnClick(char* lpszPictureName, 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); } ================================================================ #include “apdefap.h” void OnClick(char* lpszPictureName, char* lpszObjectName, char* lpszPropertyName) { // WINCC:TAGNAME_SECTION_START // syntax: #define TagNameInAction DMTagName // next TagID : #define Apc_CurrentUser “@CurrentUser” // WINCC:TAGNAME_SECTION_END // WINCC:PICNAME_SECTION_START // syntax: #define PicNameInAction PictureName // next PicID : #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— ) if ( lpszPictureName[i] == ‘.’ ) { strncpy( pic_name, lpszPictureName, i ); strcpy( obj_name, &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; 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 x1; } ////////////////////////////////// di chuyen cheo chuan #include “apdefap.h” long _main(char* lpszPictureName, char* lpszObjectName, char* lpszPropertyName) { static int x1=640,y1=350; static 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 x; } ==================================================================================== = Option Explicit 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 ‘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) 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) End If End Sub =================================================================== #define Apc_lower_limit “lower_limit” #define Apc_upper_limit “upper_limit” // next TagID : // WINCC:TAGNAME_SECTION_END // WINCC:PICNAME_SECTION_START // syntax: #define PicNameInAction “PictureName” // next PicID : // WINCC:PICNAME_SECTION_END double a; double lolmt; double uplmt; SetOutputValueDouble(lpszPictureName,“Setpoint_I”,strtod(GetOutputValueChar(lpszPict ureName,“DisplayNr”),NULL)); a=GetOutputValueDouble(lpszPictureName,“Setpoint_I”); lolmt=GetTagFloat (Apc_lower_limit); uplmt=GetTagFloat (Apc_upper_limit); if ((a = lolmt )) { SetOperation(lpszPictureName,“-_Button”,1); //Rückgabe-Typ :BOOL SetOperation(lpszPictureName,“._Button”,1); //Rückgabe-Typ :BOOL SetOutputValueChar(lpszPictureName,“DisplayNr”,“”); SetVisible(“PA2_bld01.PDL”,“Tastatur”,0); //Rückgabe-Typ :BOOL SetTagDouble(Apc_Setpoint,a); } } ======================================================= #include “apdefap.h” void OnClick(char* lpszPictureName, 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 SetOperation(lpszPictureName,“del_Button”,1); //Rückgabe-Typ :BOOL } ==================================================================== #include “apdefap.h” void OnClick(char* lpszPictureName, char* lpszObjectName, char* lpszPropertyName) { // WINCC:TAGNAME_SECTION_START // syntax: #define TagNameInAction DMTagName // next TagID : #define ApcVisibleTag03 “VisibleTag03” // WINCC:TAGNAME_SECTION_END // WINCC:PICNAME_SECTION_START // syntax: #define PicNameInAction PictureName // next PicID : // WINCC:PICNAME_SECTION_END SetVisible(lpszPictureName,“Tastatur”,0); SetTagBit(ApcVisibleTag03,1–GetTagBit(ApcVisibleTag03)); } ================================================================= #include “apdefap.h” void OnClick(char* lpszPictureName, char* lpszObjectName, char* lpszPropertyName) { //Decrements the assigned tag by int value,min; LINKINFO plink; PDLRTGetPropEx(0,lpszPictureName,lpszObjectName,“Process”,VT_I4,&value,NULL,NULL,0,N ULL,NULL); PDLRTGetPropEx(0,lpszPictureName,lpszObjectName,“Min”,VT_I4,&min,NULL,NULL,0,NULL,NU LL); if(value>=(min+1)) value-=1; else value = min; PDLRTGetLink(0,lpszPictureName,lpszObjectName,“Process”,&plink,NULL,NULL,NULL); //Rückgabe-Typ :BOOL if (plink.LinkType >2) return; else SetTagDouble (plink.szLinkName, value); } ==================================================================================== ============== #include “apdefap.h” void OnLButtonDown(char* lpszPictureName, char* lpszObjectName, char* lpszPropertyName, UINT nFlags, int x, int y) { extern lok_fct(LPCTSTR lpszPictureName, LPCTSTR lpszObjectName); // WINCC:TAGNAME_SECTION_START // syntax: #define TagNameInAction “DMTagName” #define Tag_1 “stop” // next TagID : // WINCC:TAGNAME_SECTION_END // WINCC:PICNAME_SECTION_START // syntax: #define PicNameInAction “PictureName” // next PicID : // WINCC:PICNAME_SECTION_END SetTagBit(Tag_1,1); //Triggerbit für globale Aktion lok_stop lok_fct(“PA3_bld02_lok.pdl”,“Zustandsanzeige1”); //Lokanimationsfunktion } =================================================================================== #include “apdefap.h” int gscAction( void ) { // WINCC:TAGNAME_SECTION_START // syntax: #define TagNameInAction “DMTagName” #define Tag_1 “loko_S36” // next TagID : // WINCC:TAGNAME_SECTION_END // WINCC:PICNAME_SECTION_START // syntax: #define PicNameInAction “PictureName” #define Pic_2 “PA10_bld06.pdl” // next PicID : // WINCC:PICNAME_SECTION_END #include “dmclient.h” extern Delay(ms); static long int a=-1; while (GetTagBit(Tag_1)) //Rückgabe – Typ :short int { SetIndex(Pic_2,“Zustandsanzeige1”,a); printf(“loko_s36.pas return value = %d \r\n”,a); Delay(50); a++; if(a==8) a=0; } return a; } ======================================================================= #include “apdefap.h” int gscAction( void ) { // WINCC:TAGNAME_SECTION_START // syntax: #define TagNameInAction “DMTagName” // next TagID : // WINCC:TAGNAME_SECTION_END // WINCC:PICNAME_SECTION_START // syntax: #define PicNameInAction “PictureName” // next PicID : // WINCC:PICNAME_SECTION_END PrintHtml (); //Rückgabe – Typ :void return 0; } ======================================================================== Creat trend #include “apdefap.h” int gscAction( void ) { // WINCC:TAGNAME_SECTION_START // syntax: #define TagNameInAction DMTagName #define Tag_1 “stop” #define Tag_2 “T01” #define Tag_3 “T02” #define Tag_4 “T03” #define Tag_5 “T04” #define Tag_6 “T05” #define Tag_7 “T06” #define Tag_8 “T07” #define Tag_9 “Trend_1” #define Tag_10 “Trend_2” #define Tag_11 “Trend_3” #define Tag_12 “Trend_4” #define Tag_13 “Trend_5” #define Tag_14 “Trend_6” #define Tag_15 “Trend_7” #define ApcMel_Mischer “Mel_Mischer” #define Apc_X_Trend “X_Trend” #define Apc_Y_Trend “Y_Trend” // next TagID : 17 // WINCC:TAGNAME_SECTION_END // WINCC:PICNAME_SECTION_START // syntax: #define PicNameInAction PictureName // next PicID : // WINCC:PICNAME_SECTION_END // create_trends.pas by Bob Meads modified by Hans-Norbert Schwoerer // this script creates values of internal variables // for trending and alarming static long int alarm=0; long int trend_1c=0; long int trend_2c=0; long int trend_3c=0; long int trend_4c=0; long int trend_5c=0; long int trend_6c=0; long int trend_7c=0; long int trend_1=0; long int trend_2=0; long int trend_3=0; long int trend_4=0; long int trend_5=0; long int trend_6=0; long int trend_7=0; static long int trend_X=0; static long int trend_Y; if(!GetTagBit(Tag_1)) { alarm=((alarm+1)%8); while((trend_1128)) trend_1=rand (); trend_1c=trend_1 / 4; while((trend_2111)) trend_2=rand (); trend_2c=trend_2 / 3; while((trend_3130)) trend_3=rand (); trend_3c=trend_3 / 4; while((trend_4168)) trend_4=rand (); trend_4c=trend_4 / 6; while((trend_5173)) trend_5=rand (); trend_5c=trend_5 / 5; trend_6c=trend_5 / 25; trend_7c=trend_5*7; while((trend_6183)) trend_6=rand (); while((trend_7193)) trend_7=rand (); trend_X=((trend_X+1)%11); trend_Y=(trend_X*trend_X); } SetTagMultiWait (“%d%d%d%d %d%d%d%d %d%d%d%d %d%d%d%d %d”, ApcMel_Mischer,alarm, Tag_2, trend_1, Tag_3, trend_2, Tag_4, trend_3, Tag_5, trend_4, Tag_6, trend_5, Tag_7, trend_6, Tag_8, trend_7, Tag_9, trend_1c , Tag_10, trend_2c, Tag_11, trend_3c, Tag_12,trend_4c , Tag_13,trend_5c, Tag_14,trend_6c, Tag_15,trend_7c, Apc_X_Trend,trend_X, Apc_Y_Trend,trend_Y ); return 1; } ======================================================================= poly curve #include “apdefap.h” int gscAction( void ) { // WINCC:TAGNAME_SECTION_START // syntax: #define TagNameInAction “DMTagName” #define APC_raw1 “raw1” // next TagID : // WINCC:TAGNAME_SECTION_END // WINCC:PICNAME_SECTION_START // syntax: #define PicNameInAction “PictureName”1 #define Pic_1 “PAX.pic_win_5:PA5_bld09_1” #define object “Polygon_trend” // next PicID :2 // WINCC:PICNAME_SECTION_END long i=-1; BYTE byte[121]; GetTagRaw(APC_raw1,byte,121); while(++i Kurve um 170 Pixel nach unten verschieben // +170 –> move trend 170 pixel down return i; } ======================================================== #include “apdefap.h” int gscAction( void ) { // WINCC:TAGNAME_SECTION_START // syntax: #define TagNameInAction DMTagName #define ApcTest1 “Test1” #define ApcTest3 “Test3” #define ApcTest2 “Test2” #define ApcTest4 “Test4” #define ApcMSEXCELPath “MSEXCELPath” #define ApcProjectPath “ProjectPath” #define ApcRT_Stat”RT_Stat” // next TagID : // WINCC:TAGNAME_SECTION_END // WINCC:PICNAME_SECTION_START // syntax: #define PicNameInAction PictureName // next PicID : // WINCC:PICNAME_SECTION_END FILE *datei; WORD a ; BYTE b; char *c, *d, *e; char t[30]; char tmsexcel[75]; char* file; char* pp; BOOL merk; DWORD dwLaenge; c=SysMalloc(25); d=SysMalloc(25); e=SysMalloc(80); file = SysMalloc(200); pp = SysMalloc(120); pp=GetTagChar(ApcProjectPath); //Projektpfad einlesen – get Project Path sprintf(file,“%svariablen.txt”,pp); printf(“%s\r\n”,file); /*————————————————————————————————————————————————————————SCHREIBEN IN DAS TXT-FILE – Writing Tags to Text-File ————————————————————————————————————————————————————————*/ if(GetTagBitWait(ApcRT_Stat)==1) //Wenn RT_Anlaufmerker (RT_Stat) gesetzt – if RT_Stat Bit Flag is set { printf(“Anlaufmerker gesetzt – Starting-Bit-Flag set\r\n”); pp=GetTagChar(ApcProjectPath); //Projektpfad einlesen – get Project Path datei = fopen( file, “w” ); // TXT-File mit Schreibkennung öffnen if( datei != NULL ) { a=GetTagWord(ApcTest1); //Variablen auslesen und in C-Variablen schreiben b=GetTagByte(ApcTest3); c=GetTagChar(ApcTest2); d=GetTagChar(ApcTest4); e=GetTagChar(ApcMSEXCELPath); fprintf( datei,“%d\r\n%d\r\n%s\r\n%s\r\n%s\r\n”, a, b, c, d, e); // Werte in die Datei schreiben printf(“Variablen in Datei geschrieben – finished writing tags to file \r\n%d\r\n\ %d\r\n%s\r\n%s\r\n%s\r\n “, a, b, c, d, e); //Kontrolle in GSC-Runtimefenster fclose( datei ); //close file return 1; // Returnwert zur Kontrolle im GSC-Runtimefenster } else printf( “file open error\r\n” ); // Ausgabe Text, bei Fehler beim öffnen der Datei return 0; } else // wenn Anlaufmerker nicht gesetzt { /*———————————————————————————————————————————————————————— Lesen aus dem TXT-File – Read Tags from Text File ————————————————————————————————————————————————————————*/ datei = fopen(file, “r” ); // TXT_File mit Lesekennung öffnen if (datei !=NULL) { printf(“\r\nVariablen aus der Datei gelesen – finished reading tags from the file\r\n”); fgets(t,30,datei); //Wert aus der TXT lesen und in C-Variable “t” schreiben printf(“%s\r\n”,t); //Kontrolle in GSC-Runtimefenster SetTagWord(ApcTest1,(WORD)atoi(t)); // Variable beschreiben fgets(t,30,datei); //Wert aus der TXT lesen und in C-Variable “t” schreiben printf(“%s\r\n”,t); //Kontrolle in GSC-Runtimefenster SetTagByte(ApcTest3,(BYTE)atoi(t)); // Variable beschreiben fgets(t,30,datei); // Wert aus der TXT lesen und in die C-Variable “t” schreiben dwLaenge=strlen(t); // Länge des gespeicherten Textes ermitteln strcpy(&t[dwLaenge–2],“”); //mit wird um Stellen nach links geschoben um den String ohne \r\n zu terminieren printf(“%s, laenge=%d\r\n”,t,dwLaenge); //Kontrolle in GSC-Runtimefenster SetTagChar(ApcTest2,t); // Variable beschreiben fgets(t,30,datei); // Wert aus der TXT lesen und in die C-Variable “t” schreiben dwLaenge=strlen(t); // Länge des gespeicherten Textes ermitteln strcpy(&t[dwLaenge–2],“”); //mit wird um Stellen nach links geschoben um den String ohne \r\n zu terminieren printf(“%s, laenge=%d\r\n”,t,dwLaenge); //Kontrolle in GSC-Runtimefenster SetTagChar(ApcTest4,t); // Variable beschreiben fgets(tmsexcel,75,datei); // Wert aus der TXT lesen und in die C-Variable “tmsexcel” schreiben dwLaenge=strlen(tmsexcel); // Länge des gespeicherten Textes ermitteln strcpy(&tmsexcel[dwLaenge–2],“”); //mit wird um Stellen nach links geschoben um den String ohne \r\n zu terminieren printf(“%s, laenge=%d\r\n”,tmsexcel,dwLaenge); //Kontrolle in GSC-Runtimefenster SetTagChar(ApcMSEXCELPath,tmsexcel); fclose( datei); //Datei schließen return 1; // Returnwert zur Kontrolle im GSC-Runtimefenster } else { printf(“\r\nfile open error\r\n” ); // Ausgabe Text, bei Fehler beim öffnen der Datei return 0; } } } =============================================================================== log out #include “apdefap.h” void OnClick(char* lpszPictureName, char* lpszObjectName, char* lpszPropertyName) { #pragma code (“UseAdmin.DLL”) #include “pwrt_api.h” #pragma code () int i, j, n; char pic_name[256]; char obj_name[60]; PWRTLogout(); n = strlen( lpszPictureName ); for ( i = n; i > 0; i— ) if ( lpszPictureName[i] == ‘.’ ) { strncpy( pic_name, lpszPictureName, i ); strcpy( obj_name, &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; } } ================ #include “apdefap.h” void OnClick(char* lpszPictureName, 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); } =================================================================== #include “apdefap.h” void OnClick(char* lpszPictureName, char* lpszObjectName, char* lpszPropertyName) { // WINCC:TAGNAME_SECTION_START // syntax: #define TagNameInAction DMTagName // next TagID : #define Apc_CurrentUser “@CurrentUser” // WINCC:TAGNAME_SECTION_END // WINCC:PICNAME_SECTION_START // syntax: #define PicNameInAction PictureName // next PicID : #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— ) if ( lpszPictureName[i] == ‘.’ ) { strncpy( pic_name, lpszPictureName, i ); strcpy( obj_name, &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); }*/ } ============================================================= ... TagNameInAction “DMTagName” // next TagID : // WINCC: TAGNAME_SECTION_END // WINCC: PICNAME_SECTION_START // syntax: #define PicNameInAction “PictureName” // next PicID : // WINCC: PICNAME_SECTION_END... “VisibleTag03” // WINCC: TAGNAME_SECTION_END // WINCC: PICNAME_SECTION_START // syntax: #define PicNameInAction PictureName // next PicID : // WINCC: PICNAME_SECTION_END SetVisible(lpszPictureName,“Tastatur”,0);... #define PicNameInAction PictureName // next PicID : #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];