PRG_DatFileCreat.TcPOU 9.69 KB
<?xml version="1.0" encoding="utf-8"?>
<TcPlcObject Version="1.1.0.1" ProductVersion="3.1.4024.11">
  <POU Name="PRG_DatFileCreat" Id="{176c317a-1861-4b3c-96b5-e3f639fff030}" SpecialFunc="None">
    <Declaration><![CDATA[PROGRAM PRG_DatFileCreat
VAR
	iSeq 			: INT;
	iExceptionSeq	: INT;	
	fbGetSystemTime	: NT_GetTime;
	
	i 				: INT;
	
	DatHeader		: stDatHeader;		(* Beware memory alignment *)
	DatAcqBlock		: stAcquisitionBlock;
	pRequestData	: POINTER TO BOOL;
	
	nCurRecodeNo	: UDINT;	
	
	sFileName		: T_MaxString;
	
	
	fbFileOpen		: FB_FileOpen;
	fbFileSeek		: FB_FileSeek;
	fbFileWrite		: FB_FileWrite;
	fbFileClose		: FB_FileClose;
	
	nTxtParserLate	: UDINT;		(* Lag counter *)
	pPathPassThrough	: 	POINTER TO T_MaxString;
	pStartParser	: POINTER TO BOOL;
	
END_VAR
]]></Declaration>
    <Implementation>
      <ST><![CDATA[pRequestData := ADR(MAIN.fbBufferAccess.bGetData);

CASE iSeq OF
0:	(* Check for bSamplingStart command start *)
	IF NOT(MAIN.fbBufferAccess.bFifoEmpty) THEN
		nCurRecodeNo := 0;
		iSeq := iSeq + 1;
	END_IF
1:	(* Creat time serial file name *)
	fbGetSystemTime(NETID:= , 
		START:= TRUE, 
		TMOUT:= DEFAULT_ADS_TIMEOUT, 
		BUSY=> , 
		ERR=> , 
		ERRID=> , 
		TIMESTR=> );
	
	IF 	NOT(fbGetSystemTime.BUSY) AND NOT(fbGetSystemTime.ERR) THEN
(*
		iTemp := iTemp + 1;
		sFileName := USINT_TO_STRING(iTemp);
		sFileName := CONCAT (sFileName,'.dat');	
*)

		sFileName := SYSTEMTIME_TO_STRING(fbGetSystemTime.TIMESTR);

		fbGetSystemTime(START:= FALSE); 
		iSeq := iSeq + 1;
	ELSIF fbGetSystemTime.ERR THEN			
		fbGetSystemTime(START:= FALSE); 
		iExceptionSeq := iSeq;
		iSeq := 999;
	END_IF
2:	(*  fliter file name & creat further info  *)
	FOR i := 1 TO len(sFileName) BY 1 DO
		IF  F_ReplaceByString(sSource:= sFileName, sFindContent:= ':', sReplaceContent:='_' ) <> '' THEN	(* Replace content to avoid involid file name *)
			sFileName := F_ReplaceByString(sSource:= sFileName, sFindContent:= ':', sReplaceContent:='_' );
		ELSE
			EXIT;
		END_IF
	END_FOR			
		
	sFileName := CONCAT (sFileName,'Node_');
	sFileName := CONCAT (sFileName,INT_TO_STRING(MAIN.nAcuNodeId));
	sFileName := CONCAT (sFileName,'.dat');		(* add bin file extension *)
	iSeq := iSeq + 1;
3:	(* Creat file *)
	fbFileOpen.sPathName := CONCAT (sAccessPath, sFileName);

	fbFileOpen(sNetId:= sFileTargetNetId,			(* Access target id  *)
		sPathName:= ,	(* Access Path *)
		nMode:= FOPEN_MODEBINARY OR FOPEN_MODEWRITE,		(* Binary Format & Write Mode & Overrite contents   *)
		ePath:= PATH_GENERIC,
		bExecute:= TRUE,
		tTimeout:= DEFAULT_ADS_TIMEOUT,
		bBusy=> ,
		bError=> ,
		nErrId=> ,
		hFile=> );
		
	IF 	NOT(fbFileOpen.bBusy) AND NOT(fbFileOpen.bError) THEN
		fbFileOpen(bExecute:= FALSE);
		iSeq := iSeq + 1;
	ELSIF fbFileOpen.bError THEN
		fbFileOpen(bExecute:= FALSE);
		iExceptionSeq := iSeq;
		iSeq := 999;
	END_IF
		
4:	(* Creat binary info header *)
	fbFileWrite(sNetId:= sFileTargetNetId, 
		hFile:= fbFileOpen.hFile, 
		pWriteBuff:= ADR(DatHeader), 
		cbWriteLen:= SIZEOF(DatHeader), 
		bExecute:= TRUE, 
		tTimeout:= DEFAULT_ADS_TIMEOUT, 
		bBusy=> , 
		bError=> , 
		nErrId=> , 
		cbWrite=> );

	IF NOT(fbFileWrite.bBusy) AND NOT(fbFileWrite.bError) THEN
		fbFileWrite(bExecute:= FALSE);
		iSeq := iSeq + 1;
	ELSIF fbFileWrite.bError THEN
		fbFileWrite(bExecute:= FALSE);
		iExceptionSeq := iSeq;
		iSeq := 999;
	END_IF
5:	(* seek to cyclic record position  *)
	fbFileSeek(sNetId:= sFileTargetNetId, 
		hFile:= fbFileOpen.hFile, 
		nSeekPos:= SIZEOF(DatHeader), 		(* Seek to record start position *)
		eOrigin:= SEEK_SET, 
		bExecute:= , 
		tTimeout:= DEFAULT_ADS_TIMEOUT, 
		bBusy=> , 
		bError=> , 
		nErrId=> );
		
	IF NOT(fbFileSeek.bBusy) AND NOT(fbFileSeek.bError) THEN
		fbFileSeek(bExecute:= FALSE);
		iSeq := 10;
	ELSIF fbFileSeek.bError THEN
		fbFileSeek(bExecute:= FALSE);
		iExceptionSeq := iSeq;
		iSeq := 999;
	END_IF

10:		(* Check Data exist *)
	IF NOT(MAIN.fbBufferAccess.bFifoEmpty) THEN		
		pRequestData^ := TRUE;			(* request get data from buffer to DatAcqBlock *)
		iSeq := iSeq + 1;
	END_IF
11:
	fbFileWrite(sNetId:= sFileTargetNetId, 
		hFile:= fbFileOpen.hFile, 
		pWriteBuff:= ADR(DatAcqBlock), 
		cbWriteLen:= SIZEOF(DatAcqBlock), 
		bExecute:= TRUE, 
		tTimeout:= DEFAULT_ADS_TIMEOUT, 
		bBusy=> , 
		bError=> , 
		nErrId=> , 
		cbWrite=> );

	IF NOT(fbFileWrite.bBusy) AND NOT(fbFileWrite.bError) THEN
		fbFileWrite(bExecute:= FALSE);
		iSeq := iSeq + 1;
	ELSIF fbFileWrite.bError THEN
		fbFileWrite(bExecute:= FALSE);
		iExceptionSeq := iSeq;
		iSeq := 999;
	END_IF
12:	(* Seek to next block *)
	fbFileSeek(sNetId:= sFileTargetNetId, 
		hFile:= fbFileOpen.hFile, 
		nSeekPos:= fbFileSeek.nSeekPos + SIZEOF(DatAcqBlock), 		(* Seek to record start position *)
		eOrigin:= SEEK_SET, 
		bExecute:= , 
		tTimeout:= DEFAULT_ADS_TIMEOUT, 
		bBusy=> , 
		bError=> , 
		nErrId=> );
		
	IF NOT(fbFileSeek.bBusy) AND NOT(fbFileSeek.bError) THEN
		fbFileSeek(bExecute:= FALSE);
		iSeq := iSeq + 1;
	ELSIF fbFileSeek.bError THEN
		fbFileSeek(bExecute:= FALSE);
		iExceptionSeq := iSeq;
		iSeq := 999;
	END_IF
13:	(*  Check if the record times & length reached  *)
	nCurRecodeNo := nCurRecodeNo + 1;
	IF nCurRecodeNo >= nRecordAcqBlockPerFile THEN
		iSeq := 20;		(* FileClose & creat New file *)
	ELSE
		iSeq := 10;		(* GobBack do further content write *)
	END_IF

	
20:	(* File Close *)
	fbFileClose(sNetId:= sFileTargetNetId, 
		hFile:= fbFileOpen.hFile,  
		bExecute:= TRUE, 
		tTimeout:= DEFAULT_ADS_TIMEOUT, 
		bBusy=> , 
		bError=> , 
		nErrId=> );

	IF NOT(fbFileClose.bBusy) AND NOT(fbFileClose.bError) THEN
		fbFileClose(bExecute:= FALSE);
		iSeq := iSeq + 1;
	ELSIF fbFileSeek.bError THEN
		fbFileClose(bExecute:= FALSE);
		iExceptionSeq := iSeq;
		iSeq := 999;
	END_IF
21:	(* Option call bin to txt file parser process *)
	pStartParser := ADR (PRG_Slow.fbTxtParser.bExecute);
	IF PRG_Slow.fbTxtParser.bBusy THEN			(* Parser process to slower then cyclic acquisition *)
		nTxtParserLate := nTxtParserLate + 1;
	ELSE
		pPathPassThrough := ADR(PRG_Slow.fbTxtParser.sFilePathSource); 
		pPathPassThrough^ := fbFileOpen.sPathName;

		pPathPassThrough := ADR(PRG_Slow.fbTxtParser.sFilePathDestination);
		pPathPassThrough^ := F_ReplaceByString(fbFileOpen.sPathName,'.dat' , '.csv');
	
		pStartParser^ := TRUE;
		iSeq := 0;
	END_IF
END_CASE
	]]></ST>
    </Implementation>
    <LineIds Name="PRG_DatFileCreat">
      <LineId Id="27" Count="0" />
      <LineId Id="10" Count="0" />
      <LineId Id="43" Count="0" />
      <LineId Id="46" Count="0" />
      <LineId Id="51" Count="0" />
      <LineId Id="229" Count="0" />
      <LineId Id="104" Count="1" />
      <LineId Id="60" Count="0" />
      <LineId Id="62" Count="0" />
      <LineId Id="64" Count="4" />
      <LineId Id="61" Count="0" />
      <LineId Id="69" Count="0" />
      <LineId Id="260" Count="0" />
      <LineId Id="298" Count="0" />
      <LineId Id="261" Count="0" />
      <LineId Id="257" Count="0" />
      <LineId Id="262" Count="0" />
      <LineId Id="300" Count="1" />
      <LineId Id="109" Count="0" />
      <LineId Id="114" Count="0" />
      <LineId Id="77" Count="0" />
      <LineId Id="82" Count="0" />
      <LineId Id="75" Count="0" />
      <LineId Id="83" Count="2" />
      <LineId Id="55" Count="0" />
      <LineId Id="293" Count="0" />
      <LineId Id="316" Count="0" />
      <LineId Id="318" Count="6" />
      <LineId Id="329" Count="0" />
      <LineId Id="331" Count="0" />
      <LineId Id="328" Count="0" />
      <LineId Id="297" Count="0" />
      <LineId Id="87" Count="0" />
      <LineId Id="112" Count="0" />
      <LineId Id="110" Count="0" />
      <LineId Id="90" Count="8" />
      <LineId Id="89" Count="0" />
      <LineId Id="120" Count="1" />
      <LineId Id="123" Count="0" />
      <LineId Id="129" Count="0" />
      <LineId Id="126" Count="2" />
      <LineId Id="130" Count="0" />
      <LineId Id="124" Count="0" />
      <LineId Id="122" Count="0" />
      <LineId Id="56" Count="0" />
      <LineId Id="136" Count="0" />
      <LineId Id="138" Count="7" />
      <LineId Id="132" Count="0" />
      <LineId Id="147" Count="0" />
      <LineId Id="133" Count="0" />
      <LineId Id="148" Count="4" />
      <LineId Id="134" Count="0" />
      <LineId Id="58" Count="0" />
      <LineId Id="57" Count="0" />
      <LineId Id="156" Count="0" />
      <LineId Id="162" Count="6" />
      <LineId Id="157" Count="0" />
      <LineId Id="176" Count="0" />
      <LineId Id="169" Count="6" />
      <LineId Id="48" Count="0" />
      <LineId Id="177" Count="0" />
      <LineId Id="49" Count="0" />
      <LineId Id="179" Count="0" />
      <LineId Id="206" Count="0" />
      <LineId Id="208" Count="0" />
      <LineId Id="207" Count="0" />
      <LineId Id="182" Count="0" />
      <LineId Id="188" Count="17" />
      <LineId Id="187" Count="0" />
      <LineId Id="183" Count="0" />
      <LineId Id="209" Count="16" />
      <LineId Id="116" Count="1" />
      <LineId Id="263" Count="0" />
      <LineId Id="118" Count="0" />
      <LineId Id="230" Count="0" />
      <LineId Id="232" Count="0" />
      <LineId Id="234" Count="0" />
      <LineId Id="231" Count="0" />
      <LineId Id="236" Count="0" />
      <LineId Id="115" Count="0" />
      <LineId Id="237" Count="0" />
      <LineId Id="243" Count="0" />
      <LineId Id="245" Count="4" />
      <LineId Id="238" Count="0" />
      <LineId Id="242" Count="0" />
      <LineId Id="250" Count="6" />
      <LineId Id="239" Count="1" />
      <LineId Id="372" Count="0" />
      <LineId Id="354" Count="0" />
      <LineId Id="361" Count="0" />
      <LineId Id="370" Count="0" />
      <LineId Id="375" Count="1" />
      <LineId Id="381" Count="0" />
      <LineId Id="383" Count="1" />
      <LineId Id="378" Count="0" />
      <LineId Id="373" Count="1" />
      <LineId Id="364" Count="0" />
      <LineId Id="47" Count="0" />
      <LineId Id="44" Count="0" />
    </LineIds>
  </POU>
</TcPlcObject>