当前位置:文档之家› wincc中的C例题

wincc中的C例题

wincc中的C语言典型题Wincc用动态向导传送S5/S7数据变量Text library设制多语种文本间接寻址:间接变量是一个文本变量16位字符集类型的变量。

使用时必需选中间接寻址。

项目文件的确定:#include"apdefap.h"void onClick(char *lpszpicturename,char *lpszobjectname,char*lpszproper){Bool bRet;char szprojectFile[_MAX_PATH+1];CMN_ERROR ERROR;bRet=DMGetRuntimeProject(szProjectFile,_MAX_PATH+1,&ERROR);IF(bRet==False){printf("\r\nError inDMGetRuntimeProject()\r\n","\t%s\r\n",Error.szErrortext);return;}printf("\r\nProjectFile:\r\n%s\r\n",szpProjectFile);}确定项目路径:#include"apdefap.h"void onClick(char *lpszpicturename,char *lpszobjectname,char*lpszproper){Bool bRet=False;char szprojectFile[_MAX_PATH+1];CMN_ERROR ERROR;char* psz=NULL;IF(bRet==False){printf("\r\nError inDMGetRuntimeProject()\r\n","\t%s\r\n",Error.szErrortext);return;}psz=strrchr(szProjectFile,'\\');if(psz!=NULL){*(psz+1)=0;}printf("\r\nProjectPath:\r\n%s\r\n",szpProjectFile);}确定计算机名称:#include "apdefap.h"void onClick(char *lpszpicturename,char *lpszobjectname,char*lpszproper){#pragma code("Kernel32.dll");Bool GetComputerNameA(LPSTR ComputerName,LPDWORD pdwsize);#pragma code();Bool bRet=FALSE;char szComputerNameA[MAX_COMPUTERNAME_LENGTH+1];DWORD dwSize=MAX_COMPUTERNAME_LENGTH+1;bRet=GetComputerNameA(szComputerName,&dwSize);if(bRet==FALSE){printf("\r\nComputerName:\r\nUnknow computer\r\n");return;}printf("\r\nComputerName:\r\n",szComputerName);}确定用户名:#include "apdefap.h"void onClick(char *lpszpicturename,char *lpszobjectname,char*lpszproper){#pragma code("advapi32.dll");Bool GetUserNameA(LPSTR UserName,LPDWORD pdwSize);#define UNLEN 256#pragma code();Bool bRet=FALSE;char szUserName[UNLEN+1];DWORD dwSize=UNLEN+1;bRet=GetUserNameA(szUserName,&dwSize);if(bRet==FALSE){printf("\r\nUserName:\r\nUnknow User\r\n");return;}printf("\r\nUserName:\r\n%s\r\n",szUserName);}windows API的函数也位于同的DLL中,就如Wincc API的函数一样。

在各种不同的头文件中对这些函数进行了声明。

DLL的集成遵循集成WINCCDLL所使用的同一原理。

下列实例程序代码对这种集成进行了说明。

#pragma code("comdlg32.dll")#include "commdlg.h"#pragma code()设置windows属性#include "apdefap.h"void onOpenPicture(char *lpszpicturename,char *lpszobjectname,char*lpszproper){HWND hWnd=NULL;hWnd=findWindow(NULL,"Wincc-Runtime-");SetWindowText(hWnd,"Wincc C-course");SetWindowPos(hWnd,HWND_TOP,0,0,1024,768,0);SetTagByte("U08i_org_bar_1",0);createExternalTags();}读取系统时间#include "apdefap.h"char*_main(char *lpszpicturename,char *lpszobjectname,char *lpszproper) {#pragma code("kernel32.dll")VOID GetLocalTime(LPSYSTEMTIME lpSystemtime);#pragma code()SYSTEMTIME sysTime;char szTime[6]="";GetLocalTime(&sysTime);sprintf(szTime,"%02d:%02d",sysTime.wHour,sysTime.wMinute);return szTime;}播放声音文件#include "apdefap.h"{#pragma code("winmm.dll")Bool PlaySound(LPCTSTR lpszSound,HMODULE hModule,DWORD dwSound);#define SND_FILENAME 0x00020000L#define SND_ASYNC 0x0001#pragma code()Bool bRet=FALSE;char szProjectPath(_MAX_PATH);char szSoundPath(_MAX_PATH);GetProjectPath(szProjectPath);sprintf(szSoundPath,"%sSound\\%s",szProjectPath,lpszSoundFile);bRet=PlaySound(szSoundPath,NULL,SND_FILENAME|SND_ASYNC);if(bRet==false){MessageBeep((WORD)-1);}}语言切换:#include"apdefap.h"void onClick(char *lpszpicturename,char *lpszobjectname,char*lpszproper){HWND hwndParent=NULL;DWORD dwFlags=0;DWORD dwSetLocalIDs[3]={0x0409,0x0407,0x040C};UINT uSetIDArraySize=3;DWORD dwGetLocaleID;BOOL bRet;CMN_ERROR error;hwndParent=FindWindow(NULL,"Wincc C-Course");bRet=DMShowLanguageDialog(hwndParent,dwFlags,dwSetLocaleIDs,uSetIDArr aySize,&dwGetLocaleID,&Error);if(bRet==FAlSE){printf("\r\nError inDMShowLanguageDialog()\r\n","\t%s\r\n",Error.szErrorText);return;}bRet=SetLanguage(dwGetLocaleID);if(bRet==FALSE){printf("\r\nError in SetLanguage()\r\n");return;}}变量选择#include "apdefap.h"void onClick(char *lpszpicturename,char *lpszobjectname,char*lpszproper){BOOL bRet;char szProjectFile[_MAX_PATH+1];CMN_ERROR Error;HWND hwndParent=NULL;DM_VARKEY dmVarKey;LINKINFO link;if(DMGetRuntimeProject(szProjectFile,_MAX_PATH+1,&Error)==FALSE) {printf("\r\nError inDMGetRuntimeProject()\r\n","\t%s\r\n",Error.szErrorText);return;}hwndParent=FindWindow(NULL,"Wincc C-Course");if(DMShowVarDatabase(szProjectFile,hwndParent,NULL,NULL,&dmVarKey,&Er ror)==FALSE){printf("\r\nError inDMShowVarDatabase()\r\n","\t%s\r\n",Error.szErrorText);return;}SetText(lpszPictureName,"Tagname",dmVarKey.szName);link.LinkType=BUBRT_LTVARIABLE_DIRECT;link.dwCycle=0;strcpy(link.szLinkName,dmVarKey.szName);PDLRTSetLink(0,lpszPictureName,"TagValue","OutputValue",&link,NULL,NU LL,&Error);}出错框#include "apdefap.h"void onClick(char *lpszpicturename,char *lpszobjectname,char*lpszproper){HWND hWnd=NULL;hWnd=Findwindow(NULL,"Wincc C-Course");MessageBox(hWnd,"Wincc C-Course raised unknownExeption!!!","Error",MB_OK|MB_ICONSTOP|MB_APPLMODAL);}询问框#include "apdefap.h"void onClick(char *lpszpicturename,char *lpszobjectname,char*lpszproper){HWND hWnd=NULL;int iRet;HWnd=Findwindow(NULL,"Wincc C-Course");iRet=MessageBox(hWnd,"Do you want to do someting?","Question",MB_YESNO|MB_ICONQUESTION|MB_APPLMODAL);printf("\r\nExample 3\r\n");if(iRet==IDYES){printf("User selected Yes Button\r\n");}else{printf("User selected No Button\r\n");}}打开标准对话框#include "apdefap.h"void onClick(char *lpszpicturename,char *lpszobjectname,char*lpszproper){#pragma code("comdlg32.dll")#include "commdlg.h"#pragma code()BOOL bRet;OPENFILENAME ofn;char szFilter[]="Textfiles|*.txt|all Files|*.*|";char szFile[_MAX_PATH+1];char szInitialDir[_MAX_PATH+1]="C:\\";ofn.lStructSize=sizeof(OPENFILENAME);ofn.hwndOwner=Findwindow(NULL,"wincc c-course");for(psz=szFilter;*psz;psz++){if(*psz=='|'){*psz=0;}}ofn.lpstrFilter=szFilter;ofn.lpstrFile=szFile;ofn.nMaxFile=_MAX_PATH+1;GetProjectPath(szInitiaDir)ofn.lpstrInitialDir=szInitialDir;bRet=GetOpenFileName(&ofn);if(bRet==FALSE){printf("\r\nError in GetOpenFileName()\r\n");return;}printf("\r\nselected File (Path+name)\r\n%s\r\n",ofn.lpstrFile); }备份数据#include "apdefap.h"void onClick(char *lpszpicturename,char *lpszobjectname,char*lpszproper){FILE* pFile=NULL;char szFile[_MAX_PATH+10];int iData;float fDate;if(GetProjectPath(szFile)==FALSE){printf("\r\nError in GetProjectPath()\r\n");return;}strcat(szFile,"Data.txt");pFile=fopen(szFile,"w+");if(pFile==NULL){printf("\r\nError in fopen()\r\n");return;}iData=GetTagSDWord("S32_couse_file_1");fData=GetTagFloat("F32i_course_file_1");fprintf(pFile,"%d\r\n%f\r\n",iData,fData);fclose(pFile);printf("\r\nData written in file:\r\n\t%d\r\n\t%f\r\n",iData,fData); }读取数据#include "apdefap.h"void onClick(char *lpszpicturename,char *lpszobjectname,char*lpszproper){FILE* pFile=NULL;char szFile[_MAX_PATH+10];int iData;float fData;if(GetProjectPath(szFile)==FALSE){printf("\r\nError in GetProjectPath()\r\n");return;}strcat(szFile,"Data.txt");pFile=fopen(szFile,"r+");if(pFile==NULL){printf("\r\nError in fopen()\r\n");return;}fscanf(pFile,"%d\r\n%f\r\n",&iData,&fData);fclose(pFile);SetTagSDWord("S32i_course_file_1",iData);SetTagFloat("F32i_course_file_1",fData);printf("\r\nData read form file.\r\n\t%d\r\n\t%f\r\n",iData,fData); }报表项目函数LogText()#include"apdefap.h"BOOL LogText(char* lpszLogText){File* pFile=NULL;char szFile[_MAX_PATH+10];if(GetProjectPath(szFile)==FALSE){printf("\r\nError in GetProjectPath()\r\n");return FALSE;}strcat(szFile,"Log.txt");pFile=fopen(szFile,"a+");if(pFile==NULL){printf("\r\nError in fopen()\r\n");return FALSE;}fprintf(pFile,"%s-%s\r\n",GetLocalTimeString(),lpszLogText);fclose(pFile);return TRUE;}Excel中读取WINCC变量值:read:Rem Read Tag Values in Wincc-projectPrivate Sub GetValue_click()Dim mcp as ObjectDim var as stringDim value as VariantDim i as IntegerSet mcp=CreateObject("Wincc-Runtime-Project")Cell="c3"i=1Do while Not Range(cell)=""var=range(cell)value=mcp.GetValue(var)Range("D"&2+i).value=valuecell="c"&3+ii=i+1loopend subwrite:Rem set Tag Values in Wincc-ProjectPrivate Sub SetValue_click()Dim mcp as ObjectDim var as stringDim value as VariantDim i as IntegerDim bRet as Integerset mcp =CreateObject("wincc-Runtime-Project") Cell="c3"i=1Do while Not Range(Cell)=""Var=Range(cell)Value=Range("e"&2+i).valuebRet=mcp.SetValue(var,value)cell="c"&3+ii=i+1Loopend sub。

相关主题