(****************************************************************************************)
FUNCTION_BLOCK logger
VAR_INPUT
END_VAR
VAR_OUTPUT
END_VAR
VAR
systemzeit:FB_LocalSystemTime;
mav_speed:FILTER_MAV_W;
mav_strom:FILTER_MAV_W;
mav_feuchte_rel:FILTER_MAV_W;
mav_feuchte_abs:FILTER_MAV_W;
mav_temp:FILTER_MAV_W;
bastelstring:FB_FormatString;
novram:FB_NovRamReadWriteEx;
novram_to_buffer:FB_NovRamReadWriteEx;
fbfileopen:FB_FileOpen;
fbfileclose:FB_FileClose;
fbfilewrite:FB_FileWrite;
fbfileread:FB_FileRead;
fbfileputs:FB_FilePuts;
i_step:INT:=0;
mav_n:UINT:=27;
wsecold: WORD;
dummy: BOOL;
mav_rst: BOOL;
wminold: WORD;
zeitstempel:STRING(255);
bwritenovram: BOOL;
sverify: STRING(255);
ncbread: UDINT;
ncbwrite: UDINT;
bbusy: BOOL;
berror: BOOL;
nerrid: UDINT;
btest: BOOL;
breadnovram: BOOL;
cblen: UDINT:=37;
w_divrest: INT;
offcnt:UINT;
crlf: STRING(5):='$R$N';
aBuffer: ARRAY [1..5000] OF BYTE; (*bytepuffer für fileoperation*)
b_readtobuffer: BOOL;
bbusy1: BOOL;
berror1: BOOL;
nerrid1: UDINT;
ncbread1: UDINT;
sDateiname: STRING(150);
s_kopf: STRING(255);
openhandle: UINT;
s_Datensatz_a: STRING(100);
s_Datensatz_b: STRING(100);
s_datensatz: STRING(255);
cbwrite_vrfy: UDINT;
END_VAR
VAR_EXTERNAL
END_VAR
(* windows systemzeit auslesen und mit rtc synchronisieren *)
systemzeit(
sNetID:= , (*default local*)
bEnable:=TRUE ,
dwCycle:=5 ,
dwOpt:=1 ,
tTimeout:= , (*default 5s*)
bValid=> ,
systemTime=> ,
tzID=> );
(****************************************************************************************)
(*sekundenflanke *)
IF systemzeit.systemTime.wSecond <> wsecold THEN
(* hier alles rein, was zyklisch (3s) ausgeführt werden soll*)
w_divrest:= systemzeit.systemTime.wSecond MOD 2;
IF w_divrest = 0 THEN
mav_speed(X:=DINT_TO_INT(speedsbm_stair) , N:=mav_n , RST:=mav_rst , Y=>);
mav_strom(X:=Strom_presse , N:=mav_n , RST:=mav_rst , Y=> );
mav_feuchte_rel(X:=rh_hmi , N:=mav_n , RST:=mav_rst , Y=> );
mav_feuchte_abs(X:=dew_hmi , N:=mav_n , RST:=mav_rst , Y=> );
mav_temp(X:=temp_hmi, N:=mav_n , RST:=mav_rst , Y=> );
END_IF;
END_IF;
wsecold:=systemzeit.systemTime.wSecond;
(****************************************************************************************)
mav_rst:=FALSE;
(*minutenflanke*)
IF wminold <> systemzeit.systemTime.wMinute THEN
(* hier alles rein, was minütlich ausgeführt werden soll*)
mav_rst:=TRUE;
(*datensatz zusammenbauen*)
bastelstring(
sFormat:='%2u.%2u.%4u;%2u:%2u;%5u;%5u;%5u;%5u;%5u;',
arg1:=F_WORD(systemzeit.systemTime.wDay) ,
arg2:=F_WORD(systemzeit.systemTime.wMonth) ,
arg3:=F_WORD(systemzeit.systemTime.wYear) ,
arg4:= F_WORD(systemzeit.systemTime.wHour),
arg5:= F_WORD(systemzeit.systemTime.wMinute),
arg6:=F_WORD(mav_speed.Y) ,
arg7:=F_WORD(mav_strom.Y) ,
arg8:=F_WORD(mav_feuchte_rel.Y),
arg9:=F_WORD(mav_feuchte_abs.Y) ,
arg10:=F_WORD(mav_temp.Y) ,
bError=> ,
nErrId=> ,
sOut=>s_Datensatz_a);
zeitstempel:= CONCAT(s_datensatz_a,crlf); (*datensatz mit CRLF abschließen*)
cblen:=LEN(zeitstempel); (*datensatzlänge ermitteln*)
bwritenovram:=TRUE;
s_kopf:='Datum;Zeit;speed;strom;feuchte_rel;feuchte_abs;Temperatur$N';
END_IF;
wminold:=systemzeit.systemTime.wMinute;
(****************************************************************************************)
(* NOVRAM speichern*)
novram(
nDevId:= 1,
bRead:=bwritenovram ,
bWrite:=bwritenovram ,
cbSrcLen:=cblen ,
cbDestLen:=cblen ,
pSrcAddr:=ADR(zeitstempel) ,
pDestAddr:=ADR(sverify) ,
nReadOffs:=noffset ,
nWriteOffs:=noffset ,
tTimeOut:=t#1s ,
bBusy=>bbusy ,
bError=>berror,
nErrId=> nerrid,
cbRead=>ncbread ,
cbWrite=>ncbwrite );
IF bwritenovram AND NOT bbusy AND NOT berror AND NOT btest THEN
IF zeitstempel = sverify THEN
noffset:=noffset+cblen;
END_IF;
bwritenovram:=FALSE;
END_IF;
(***********************************************************************************************)
(***********************************)
IF noffset > 4900 THEN noffset:=128;END_IF;
IF noffset > 4500 THEN
b_readtobuffer:=TRUE;
END_IF;
(************************************************************************************************)
novram_to_buffer(
nDevId:= 1,
bRead:=b_readtobuffer ,
bWrite:= ,
cbSrcLen:= ,
cbDestLen:=(noffset-128) ,
pSrcAddr:= ,
pDestAddr:=ADR(aBuffer) ,
nReadOffs:=128 ,
nWriteOffs:= ,
tTimeOut:=t#1s ,
bBusy=>bbusy1 ,
bError=>berror1 ,
nErrId=>nerrid1 ,
cbRead=>ncbread1 ,
cbWrite=> );
IF b_readtobuffer AND NOT bbusy1 AND NOT berror1 AND ncbread1=noffset-128 AND i_step=0 THEN
noffset:=128;
b_readtobuffer:=FALSE;
i_step:=10;
(*ELSIF b_readtobuffer AND NOT bbusy1 AND ( berror1 OR ncbread1<>noffset-128) THEN
b_readtobuffer:=FALSE;*)
END_IF;
(***********************************************************************************************)
(* state machine fileop *)
(***********************************)
CASE i_step OF
10: (************************************************************************************************************************)
(*dateiname zusammensetzen*)
bastelstring(
sFormat:='Hard Disk\%2u%2u%4u.csv',
arg1:=F_WORD(systemzeit.systemTime.wDay) ,
arg2:=F_WORD(systemzeit.systemTime.wMonth) ,
arg3:=F_WORD(systemzeit.systemTime.wYear) ,
sOut=>sDateiname );
(*vorhandensein prüfen*)
fbfileopen(
sNetId:= ,
sPathName:=sDateiname ,
nMode:=FOPEN_MODEREAD OR FOPEN_MODETEXT ,
ePath:= ,
bExecute:=TRUE ,
tTimeout:=t#1s ,
bBusy=> ,
bError=> ,
nErrId=> ,
hFile=> );
IF fbfileopen.bExecute AND NOT fbfileopen.bBusy THEN
fbfileopen(
sNetId:= ,
sPathName:=sDateiname ,
nMode:=FOPEN_MODEREAD OR FOPEN_MODETEXT ,
ePath:= ,
bExecute:=FALSE ,
tTimeout:=t#1s );
IF fbfileopen.bError AND fbfileopen.nErrId=1804 THEN (*file nicht vorhanden*)
i_step:=20; (*sprung nach datei erstellen*)
ELSIF NOT fbfileopen.bError THEN (*vorhanden und öffnen erfolgreich*)
fbfileclose(
sNetId:= ,
hFile:=fbfileopen.hFile ,
bExecute:=TRUE ,
tTimeout:=t#500ms ,
bBusy=> ,
bError=> ,
nErrId=> );
IF fbfileclose.bExecute AND NOT fbfileclose.bBusy AND NOT fbfileclose.bError THEN
fbfileclose(bExecute:=FALSE);
i_step:=30; (*sprung nach datei zum schreiben öffnen*)
END_IF;
END_IF;
END_IF;
20: (********************************************************************************************************************)
(*file neu erstellen*)
fbfileopen(
sNetId:= ,
sPathName:=sDateiname ,
nMode:= FOPEN_MODEWRITE OR FOPEN_MODETEXT ,
ePath:= ,
bExecute:=TRUE ,
tTimeout:=t#1s ,
bBusy=> ,
bError=> ,
nErrId=> ,
hFile=>openhandle );
IF fbfileopen.bExecute AND NOT fbfileopen.bBusy THEN
i_step:=21;
END_IF;
21:(*kopf schreiben*)
fbfileopen(bExecute:= FALSE );
fbfileputs(
sNetId:= ,
hFile:=openhandle ,
sLine:=s_kopf ,
bExecute:=TRUE ,
tTimeout:= t#1s,
bBusy=> ,
bError=> ,
nErrId=> );
IF fbfileputs.bExecute AND NOT fbfileputs.bBusy THEN
i_step:=22;
END_IF;
22:(*datei schließen*)
fbfileputs(bExecute:=FALSE);
fbfileclose(
sNetId:= ,
hFile:=openhandle ,
bExecute:= TRUE,
tTimeout:=t#500ms ,
bBusy=> ,
bError=> ,
nErrId=> );
IF fbfileclose.bExecute AND NOT fbfileclose.bBusy (*AND NOT fbfileclose.bError *)THEN
i_step:=23;
END_IF;
23:
fbfileclose(bExecute:=FALSE);
i_step:=30; (*sprung nach daten schreiben*)
30:(*datei zum schreiben (anhängen) öffnen*)
fbfileopen(
sNetId:= ,
sPathName:=sDateiname ,
nMode:=FOPEN_MODEAPPEND OR FOPEN_MODEBINARY ,
ePath:= ,
bExecute:=TRUE ,
tTimeout:=t#500ms ,
bBusy=> ,
bError=> ,
nErrId=> ,
hFile=>openhandle );
IF fbfileopen.bExecute AND NOT fbfileopen.bBusy THEN
i_step:=31;
END_IF;
31:(*datei geöffnet; bytebuffer schreiben*)
fbfileopen(bExecute:= FALSE );
fbfilewrite(
sNetId:= ,
hFile:=openhandle ,
pWriteBuff:=ADR(abuffer),
cbWriteLen:=ncbread1 ,
bExecute:= TRUE,
tTimeout:=t#3s ,
bBusy=> ,
bError=> ,
nErrId=> ,
cbWrite=>cbwrite_vrfy );
IF fbfilewrite.bExecute AND NOT fbfilewrite.bBusy THEN
i_step:=32;
END_IF;
32:
fbfilewrite(bExecute:=FALSE );
fbfileclose(
sNetId:= ,
hFile:=openhandle ,
bExecute:=TRUE ,
tTimeout:=t#500ms ,
bBusy=> ,
bError=> ,
nErrId=> );
IF fbfileclose.bExecute AND NOT fbfileclose.bBusy THEN
i_step:=33;
END_IF;
33:
fbfileclose(bExecute:=FALSE);
i_step:=0;
END_CASE;