WinCC如何读写txt文件
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
WinCC如何读写txt文件
说明:
HelpStr 用于存储文本内容,由于使用字符串指针,文本长度理论上可以达到无限长。
tmp 用于存放每行读取的内容。
txt 用于存放文本文件的地址。
ChrNumLine 设定每行读取的长度。
char tmp[255];
char *HelpStr;
char txt[255];
int ChrNumLine=38;
FILE *fp;
BOOL ret;
sprintf(txt,"%s273\\HelpFile\\%s.txt",PrjPath
(),lpszObjectName);
fp=fopen(txt,"r");
if (fp==NULL)
{
printf("\r\nOpen File : %s Failed!",txt);
sprintf(HelpStr,"\r\nError!\r\nThe Help file - %s does not exit!",txt);
}
else
{
rewind(fp);
while(!feof(fp))
{
fgets(tmp,ChrNumLine,fp);
strcat(HelpStr,tmp);
strcat(HelpStr,"\r\n");
}
}
ret=fclose(fp);
if (ret!=0) printf("\r\nError In fclose file %s",txt);
SetText("Help_Content.pdl","Main",HelpStr);
vb脚本:
Function ReadAllT extFile
Const ForReading = 1, ForWriting = 2
Dim fso, f
Set fso = CreateObject("s cripting.FileSystemObject")
Set f = fso.OpenTextFile("c:\testfile.txt", ForWriting, True)
f.Write "Hello world!"
Set f = fso.OpenTextFile("c:\testfile.txt", ForReading) ReadAllTextFile = f.ReadAll
End Function
====================================== =============================== 一、将WinCC变量导出到TEXT文件
Sub OnLButtonUp(ByVal Item, ByVal Flags, ByVal x, ByVal y) Dim fso,File
Dim a
a=HMIRuntime.Tags("FileName").Read
Const ForWriting = 2
Set fso = CreateObject("Scripting.FileSystemObject")
Set File = fso.OpenTextFile("D:\Export&Import\"&CStr(a)&".txt", ForWriting, True)
File.WriteLine(HMIRuntime.Tags("Var_1").read)
File.WriteLine(HMIRuntime.Tags("Var_2").read)
File.WriteLine(HMIRuntime.Tags("Var_3").read)
File.WriteLine(HMIRuntime.Tags("Var_4").read)
File.WriteLine(HMIRuntime.Tags("Var_5").read)
File.Close
MsgBox "文件已经成功导出/Export Successful"
End Sub
二、从TXT文件中读取数据到WinCC变量
Sub OnLButtonUp(ByVal Item, ByVal Flags, ByVal x, ByVal y) Dim fso
Dim txtfile
Dim a
a=HMIRuntime.Tags("FileName").Read
Set fso = CreateObject("scripting.filesystemobject")
If fso.FileExists("D:\\Export&Import\\"&CStr(a)&".txt") Then Set txtfile = fso.OpenTextFile("D:\\Export&Import\\"&CStr(a)&".txt") HMIRuntime.Tags("Var_1").Write txtfile.ReadLine
HMIRuntime.Tags("Var_2").Write txtfile.ReadLine
HMIRuntime.Tags("Var_3").Write txtfile.ReadLine
HMIRuntime.Tags("Var_4").Write txtfile.ReadLine
HMIRuntime.Tags("Var_5").Write txtfile.ReadLine
MsgBox "导入数据成功/Import Successful"
txtfile.Close
Else
MsgBox "文件不存在/File is not existing"
End if
End Sub
====================================== =======================
用c script有两种方法,一种简单的就是使用ansic函数,代码如下:
FILE* lpFile;
char* lpszStr = "hello\r\n";
lpFile = fopen("c:\\test.txt", "a");
if(lpFile == NULL)
{ printf("can not open file\r\n"); return;}
fprintf(lpFile, lpszStr);
fclose(lpFile);
但是WinCC中使用printf,fprintf等函数最多只能处理360个字符,如果你写的字符串过长,可以使用windows api,代码如下:#pragma code("kernel32.dll")
#include "windows.h" // 这里的window.h必须要装上windows sdk
// 或者装个vc6.0
// 然后把vc98\include里的头文件拷贝到
// siemens\wincc\aplib中
#pragma code()
HANDLE hFile;
DWORD dwPos, dwBytes;
char* lpszText = "hello\r\n";
int nLen = strlen(lpszText);
hFile = CreateFile("c:\\test.txt", GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if(hFile == INVALID_HANDLE_VALUE)
{printf("can not open file\r\n"); return;}
dwPos = SetFilePointer(hFile, 0, NULL, FILE_END);
LockFile(hFile, dwPos, 0, dwPos + nLen, 0);
WriteFile(hFile, lpszText, nLen, &dwBytes, NULL);
UnLockFile(hFile, dwPos, 0, dwPos + nLen, 0);
CloseHandle(hFile);
ps:如果编译时出现常量没有定义的错误,自行去winbase.h中找
到定义并在脚本中自己预定义一下。
Upload By Yelky QQ1216534370。