如何通过 C 语言脚本将数据从 WinCC 数据库导入到 ASCII 文件?

问题:
如何通过 C 语言脚本将数据从 WinCC 数据库导入到 ASCII 文件?n

解答:
将下列脚本作为项目函数集成到 WinCC中。脚本在指定的时间段内将数据从过程值归档导入到 ASCII 文件(例如,通过一个内部变量)。需要的话,比方说可以通过按钮启动该项目函数。n

重要信息:

  • 基本上可以通过该方式实现数据导出,然而,不能担保百分百实现。用户负责通过 SQL 任务访问 WinCC 项目数据库。如果使用的是 SP2 版本后的 WinCC V5,请考虑条目号7792504中的注意事项。n
  • WinCC V5.1版本及以前版本具备该功能。n
  • 从 WinCC V6.0版本起,使用另一个数据库(Microsoft SQL Server),这个数据库无法使用该脚本。有关详情,请参见条目号19300688

项目函数:n

void SQL_Zugriff()
{
// The path data for isql and parameter file have to be adapted
// to the real circumstances.
// A time selection with start and end of time has to be added
// with "and T<..." (after the "T>-task").
// Tag declaration:
// Filepointern

#include <apdefap.h>
FILE *stream;n

// internal tags of Type WORD
WORD tag=0,monat=0,Jahr=0,stunde=0,minute=0;n

// for data string
char datum[256];n

// for change WORD to string
char wandlung[256];n

// Task of isql-call with parameter file for ProgramExecute
// //While porting a project to another PC it has to be considered that constant path data
might cause problems if
// the directories on the target device
// are not identical.n

char*a="C:SIEMENSCommonSQLANYISQL -q -b -c UID=DBA;PWD=SQL;DBF=D:WCCPRJ30ProjectnameProjectname.db;DBN=<DatabaseName> read D:WCCPRJ30Projextnamearchiv.sql";n

<DatabaseName> 从 WinCC V3.1版本起,该名称结构如下:
Project name_Date_time
示例:MyProjct_97-08-01_09:00:00n

可通过 REGEDIT 确定名称:n

HKEY_CURRENT_USER
|_ Software
|_ ODBC
|_ ODBC.INI

纵向列出所有数据库名称;当点击需要的名称时,可以在右侧看到相应的设置,例如,"DatabaseName"。这是在上述示例中必须声明的顺序。

// Reading the tag values:
// The tags "tag", "monat", "jahr", "stunde" and "minute"
// either have to established as internal or as process tags of type
// "16 Bit unsigned" in the Control Center.
// Now, a time period can be determined with EA-fields;
//it is also possible to start the export via a button.

tag=GetTagWord("tag");
monat=GetTagWord("monat");
jahr=GetTagWord("jahr");
stunde=GetTagWord("stunde");
minute=GetTagWord("minute");

// Compilation of the select-String:
// The indication of the name of the Process value archive
// depends on the corresponding configuration.

strcpy(datum,"select * from pde#hd#Prozesswertarchiv#DB12DW3 where T>");
//strcpy(datum,"select * from pde#hd#Prozesswertarchiv#DB12DW3");

// For monat less than 10 set leading 0
sprintf( wandlung, "'%d", jahr );
strcat(datum,wandlung);
strcat(datum,"-");
if (monat<10) strcat(datum,"0");
sprintf( wandlung, "%d", monat );
strcat(datum,wandlung);
strcat(datum,"-");

// For datum less than 10 set leading 0
if (tag<10) strcat(datum,"0");
sprintf( wandlung, "%d", tag );
strcat(datum,wandlung);
strcat(datum," ");

// For stunde less than 10 set leading 0
if (stunde<10) strcat(datum,"0");
sprintf( wandlung, "%d", stunde );
strcat(datum,wandlung);
strcat(datum,":");

// For minute less than10 set leading 0
if (minute<10) strcat(datum,"0");
sprintf( wandlung, "%d", minute );
strcat(datum,wandlung);
strcat(datum,":00.000'");

// for output with correct time sequence
strcat(datum," order by t;");

// Creation of the parameter data:
// While porting a project to another PC it has to be considered
// that constant path data might cause problems if
// the directories on the target device
// are not identical.
stream = fopen( "D:WCCPRJ30Projectnamearchiv.sql", "w" );

// Writing in Parameter-file

if( stream != NULL )

{

fprintf( stream, "%srn",datum);
fprintf( stream, "output to D:WCCPRJ30kag_hotlarchiv.txt format asciirn" );
//printf("selectstring=%srn",datum);

// Datei schliessen
fclose( stream );

}

// Programmaufruf
ProgramExecute(a);

}

关键字:
备份,数据传送,数据导出,导出数据

原创文章,作者:ximenziask,如若转载,请注明出处:https://www.zhaoplc.com/plc338406.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2020年11月5日
下一篇 2021年4月12日

相关推荐

发表回复

登录后才能评论