ST ENUM
TYPE ENUM_log :
(
nInitial:=0,
nOpen,
nWhetherOpen,
nWrite,
nWhetherWrite,
nClose,
nWhetherClose
);
END_TYPE
TYPE ST_log :
STRUCT
control:ST_log_control;
parameter:ST_log_parameter;
status:ST_log_status;
FB:FB_log;
END_STRUCT
END_TYPE
TYPE ST_log_control :
STRUCT
bWrite:BOOL;
END_STRUCT
END_TYPE
TYPE ST_log_parameter :
STRUCT
sPathName:STRING:='C:\PlcData\Log\';
sDescription:WSTRING:="无信息";
END_STRUCT
END_TYPE
TYPE ST_log_status :
STRUCT
bWriteDone:BOOL;
bError:BOOL;
sComment:WSTRING;
END_STRUCT
END_TYPE
FB_log
FUNCTION_BLOCK FB_log
VAR_INPUT
LogControl:ST_log_control;
LogParameter:ST_log_parameter;
LogTime:STRING;
END_VAR
VAR_OUTPUT
LogStatus:ST_log_status;
END_VAR
VAR
sFileName:STRING;
sDataUTF8:STRING(255);
sDescriptionUTF8:STRING(255);
nWriteStep:INT;
FileOpen:FB_FileOpen;
FileWrite:FB_FileWrite;
FileClose:FB_FileClose;
END_VAR
//文件名
sFileName:=CONCAT(MID(STR:=LogTime , LEN:=10 , POS:=1 ),'.TXT');
//写入数据处理
sDescriptionUTF8:='';
WSTRING_TO_UTF8(ADR(sDescriptionUTF8),ADR(LogParameter.sDescription),SIZEOF(sDescriptionUTF8));
STRING_TO_UTF8(ADR(sDataUTF8),ADR(LogTime),SIZEOF(sDataUTF8));
sDataUTF8:=CONCAT(sDataUTF8,' ');
sDataUTF8:=CONCAT(sDataUTF8,sDescriptionUTF8);
sDataUTF8:=CONCAT(sDataUTF8,'$n');
//文件写入步
CASE nWriteStep OF
ENUM_log.nInitial://复位动作
FileOpen.bExecute:=FALSE;
FileWrite.bExecute:=FALSE;
FileClose.bExecute:=FALSE;
IF LogControl.bWrite THEN
LogStatus.bError:=FALSE;
LogStatus.sComment:="";
LogStatus.bWriteDone:=FALSE;
nWriteStep:=ENUM_log.nOpen;
END_IF
ENUM_log.nOpen://打开文件
FileOpen.bExecute:=TRUE;
IF FileOpen.bBusy THEN
nWriteStep:=ENUM_log.nWhetherOpen;
END_IF
ENUM_log.nWhetherOpen://判断文件是否打开
IF FileOpen.bError THEN
LogStatus.bError:=TRUE;
LogStatus.sComment:="打开文件失败";
nWriteStep:=ENUM_log.nInitial;
END_IF
IF NOT FileOpen.bBusy AND NOT FileOpen.bError THEN
nWriteStep:=ENUM_log.nWrite;
END_IF
ENUM_log.nWrite://写入数据
FileWrite.bExecute:=TRUE;
IF FileWrite.bBusy THEN
nWriteStep:=ENUM_log.nWhetherWrite;
END_IF
ENUM_log.nWhetherWrite://判断数据是否写入
IF FileWrite.bError THEN
LogStatus.bError:=TRUE;
LogStatus.sComment:="写入数据失败";
nWriteStep:=ENUM_log.nInitial;
END_IF
IF NOT FileWrite.bBusy AND NOT FileWrite.bError THEN
nWriteStep:=ENUM_log.nClose;
END_IF
ENUM_log.nClose://关闭文件
FileClose.bExecute:=TRUE;
IF FileClose.bBusy THEN
nWriteStep:=ENUM_log.nWhetherClose;
END_IF
ENUM_log.nWhetherClose://判断文件是否关闭
IF FileClose.bError THEN
LogStatus.bError:=TRUE;
LogStatus.sComment:="关闭文件失败";
nWriteStep:=ENUM_log.nInitial;
END_IF
IF NOT FileClose.bBusy AND NOT FileClose.bError THEN
LogStatus.bWriteDone:=TRUE;
LogStatus.sComment:="写记录完成";
nWriteStep:=ENUM_log.nInitial;
END_IF
END_CASE
//文件功能块
FileOpen(
sNetId:='' ,
sPathName:=CONCAT(LogParameter.sPathName,sFileName) ,
nMode:=FOPEN_MODEAPPEND OR FOPEN_MODETEXT ,
ePath:=PATH_GENERIC ,
bExecute:= ,
tTimeout:=T#2S ,
bBusy=> ,
bError=> ,
nErrId=> ,
hFile=> );
FileWrite(
sNetId:='' ,
hFile:=FileOpen.hFile ,
pWriteBuff:=ADR(sDataUTF8) ,
cbWriteLen:=INT_TO_UDINT(LEN(sDataUTF8)) ,
bExecute:= ,
tTimeout:=T#2S ,
bBusy=> ,
bError=> ,
nErrId=> ,
cbWrite=> );
FileClose(
sNetId:='' ,
hFile:=FileOpen.hFile ,
bExecute:= ,
tTimeout:=T#2S ,
bBusy=> ,
bError=> ,
nErrId=> );
GVL_log
VAR_GLOBAL
//报警记录日志
alarmLog:ARRAY[0..nAlarmMaxNum] OF ST_log;
END_VAR
P_log
//******报警记录******//
FOR i:=1 TO nAlarmMaxNum BY 1 DO
//复位写log
IF alarmLog[i].control.bWrite THEN
alarmLog[i].control.bWrite:=FALSE;
END_IF
//报警上升沿触发信息
IF alarm[i].rTrig.Q THEN
alarmLog[i].control.bWrite:=TRUE;
alarmLog[i].parameter.sDescription:=WCONCAT("报警码:",STRING_TO_WSTRING(DINT_TO_DECSTR(INT_TO_DINT(alarm[i].nCode),4)));
alarmLog[i].parameter.sDescription:=WCONCAT(alarmLog[i].parameter.sDescription," 报警信息:");
alarmLog[i].parameter.sDescription:=WCONCAT(alarmLog[i].parameter.sDescription,alarm[i].sComment);
END_IF
//报警下降沿触发信息
IF alarm[i].fTrig.Q THEN
alarmLog[i].control.bWrite:=TRUE;
alarmLog[i].parameter.sDescription:=WCONCAT("报警码:",STRING_TO_WSTRING(DINT_TO_DECSTR(INT_TO_DINT(alarm[i].nCode),4)));
alarmLog[i].parameter.sDescription:=WCONCAT(alarmLog[i].parameter.sDescription," 报警信息:");
alarmLog[i].parameter.sDescription:=WCONCAT(alarmLog[i].parameter.sDescription,alarm[i].sComment);
alarmLog[i].parameter.sDescription:=WCONCAT(alarmLog[i].parameter.sDescription,"--报警消除");
END_IF
//报警LOG功能块
alarmLog[i].FB(
LogControl:=alarmLog[i].control ,
LogParameter:=alarmLog[i].parameter ,
LogTime:=sSystemTime ,
LogStatus=>alarmLog[i].status );
END_FOR
P_log_delete
PROGRAM P_log_delete
VAR
bDelete:BOOL:=TRUE;
sPathName:STRING:='C:\PlcData\Log\';
sFileName:STRING;
bDeleteDone:BOOL;
bError:BOOL;
sComment:WSTRING;
nDeleteStep:INT;
nYear:WORD;
nMonth:WORD;
nDay:WORD;
sYear:STRING;
sMonth:STRING;
sDay:STRING;
FindFileEntry:ARRAY[1..200] OF ST_FindFileEntry;
EnumFindFileList:FB_EnumFindFileList;
FileDelete:FB_FileDelete;
END_VAR
//零点删除
IF stSystemTime.wHour=0 AND stSystemTime.wMinute=0 AND stSystemTime.wSecond=0 THEN
bDelete:=TRUE;
END_IF
//计算日期
ACT_date();
//文件删除步
CASE nDeleteStep OF
0://复位动作
EnumFindFileList.bExecute:=FALSE;
FileDelete.bExecute:=FALSE;
IF bDelete THEN
bError:=FALSE;
sComment:="";
bDeleteDone:=FALSE;
bDelete:=FALSE;
nDeleteStep:=100;
END_IF
100://查找文件列表
EnumFindFileList.bExecute:=TRUE;
IF EnumFindFileList.bBusy THEN
nDeleteStep:=110;
END_IF
110://判断是否查找到文件
IF EnumFindFileList.bError THEN
bError:=TRUE;
sComment:="查找文件列表失败";
nDeleteStep:=0;
END_IF
IF NOT EnumFindFileList.bBusy AND NOT EnumFindFileList.bError THEN
nDeleteStep:=200;
END_IF
200://删除文件
FileDelete.bExecute:=TRUE;
IF FileDelete.bBusy THEN
nDeleteStep:=210;
END_IF
210://判断文件是否删除
IF FileDelete.bError THEN
bError:=TRUE;
sComment:="删除文件失败";
nDeleteStep:=0;
END_IF
IF NOT FileDelete.bBusy AND NOT FileDelete.bError THEN
bDeleteDone:=TRUE;
sComment:="删记录完成";
nDeleteStep:=0;
END_IF
END_CASE
//文件功能块
EnumFindFileList(
sNetId:='' ,
sPathName:=CONCAT(sPathName,'*.TXT') ,
eCmd:=eEnumCmd_First ,
pFindList:= ADR(FindFileEntry) ,
cbFindList:=SIZEOF(FindFileEntry) ,
bExecute:= ,
tTimeout:=T#2S ,
bBusy=> ,
bError=> ,
nErrID=> ,
bEOE=> ,
nFindFiles=> );
FileDelete(
sNetId:='' ,
sPathName:=CONCAT(sPathName,sFileName) ,
ePath:=PATH_GENERIC ,
bExecute:= ,
tTimeout:=T#2S ,
bBusy=> ,
bError=> ,
nErrId=> );
//计算日期
IF stSystemTime.wDay>30 THEN
nYear:=stSystemTime.wYear;
nMonth:=stSystemTime.wMonth;
nDay:=stSystemTime.wDay-30;
ELSIF stSystemTime.wMonth>1 THEN
IF ((stSystemTime.wMonth MOD 2)=0 AND stSystemTime.wMonth<8 AND stSystemTime.wMonth<>3)
OR ((stSystemTime.wMonth MOD 2)=1 AND stSystemTime.wMonth>7) THEN
nYear:=stSystemTime.wYear;
nMonth:=stSystemTime.wMonth-1;
nDay:=stSystemTime.wDay+31-30;
ELSIF stSystemTime.wMonth<>3 THEN
nYear:=stSystemTime.wYear;
nMonth:=stSystemTime.wMonth-1;
nDay:=stSystemTime.wDay+30-30;
ELSIF (stSystemTime.wYear MOD 4)=0 AND stSystemTime.wDay>1 THEN
nYear:=stSystemTime.wYear;
nMonth:=stSystemTime.wMonth-1;
nDay:=stSystemTime.wDay+29-30;
ELSIF (stSystemTime.wYear MOD 4)=0 AND stSystemTime.wDay=1 THEN
nYear:=stSystemTime.wYear;
nMonth:=stSystemTime.wMonth-2;
nDay:=stSystemTime.wDay+29+31-30;
ELSIF (stSystemTime.wYear MOD 4)<>0 AND stSystemTime.wDay>2 THEN
nYear:=stSystemTime.wYear;
nMonth:=stSystemTime.wMonth-1;
nDay:=stSystemTime.wDay+28-30;
ELSIF (stSystemTime.wYear MOD 4)<>0 AND stSystemTime.wDay<=2 THEN
nYear:=stSystemTime.wYear;
nMonth:=stSystemTime.wMonth-2;
nDay:=stSystemTime.wDay+28+31-30;
END_IF
ELSE
nYear:=stSystemTime.wYear-1;
nMonth:=stSystemTime.wMonth+12-1;
nDay:=stSystemTime.wDay+31-30;
END_IF
//日、月显示两位
sDay:=WORD_TO_DECSTR(nDay,2);
sMonth:=WORD_TO_DECSTR(nMonth,2);
sYear:=WORD_TO_STRING(nYear);
//删除的日期
sFileName:=CONCAT(sYear,'-');
sFileName:=CONCAT(sFileName,sMonth);
sFileName:=CONCAT(sFileName,'-');
sFileName:=CONCAT(sFileName,sDay);
sFileName:=CONCAT(sFileName,'.TXT');