tlexi - 2020-06-10

I am taking over a project with code from another person. I have a PLC that currently has inputs in from pressure sensors and thermocouples. It then scales that data to PSI and temperature in fahrenheit. The way the data is set up from each of those sensors is to be formatted into an array. So, once the data is scaled it is in an array that is also in the Network Variable List of the program. I am trying to take each of these values from the array, record the value every certain amount of time (say 1 recording per second for sake of clarity), and then export each piece of data to a CSV file for every second. Not sure where to even go with this. This is the code I was left with, but I feel as if it it unnecessarily complicated?

//This is the support class for File_Handler
FUNCTION_BLOCK fileWrite
VAR_INPUT
    xWrite : BOOL;
    sData : STRING(200);
    uiLineLength : INT := 200;
    sDirectory : STRING := 'C:\ProgramData\CODESYS\CODESYSHMIWinV3\D5050FE1\PlcLogic\data';
    //sDirectory : STRING := '/home/cds-apps/PlcLogic/data/';
    sFilename : STRING;
END_VAR
VAR_OUTPUT
    BytesWritten : __XWORD;
    BytesWrittenTotal: DWORD;
    xDone: BOOL;
END_VAR
VAR
    hFile_: sysfile.RTS_IEC_HANDLE := sysfile.RTS_INVALID_HANDLE;
    FileWriteResult: sysfile.RTS_IEC_RESULT;
    FileOpenResult: sysfile.RTS_IEC_RESULT;
    state: INT;
    sys_Us_start: SYSTIME;
    sys_Us_end: SYSTIME;
    WriteTimeMS: ULINT;
END_VAR
sFilename := CONCAT(sDirectory, sFilename);
hFile_ := SysFileOpen(szFile:= sFilename, am:= ACCESS_MODE.AM_APPEND_PLUS, pResult:= ADR(FileOpenResult));
SysTimeGetUs(pUsTime:=sys_Us_start );
BytesWritten := SysFileWrite(hFile:= hfile_, pbyBuffer:= ADR(sData), ulSize:= uiLineLength, pResult:= ADR(FileWriteResult));    
BytesWrittenTotal := BytesWrittenTotal + BytesWritten;
SysTimeGetUs(pUsTime:=sys_Us_end );
WriteTimeMS := (sys_Us_end - sys_Us_start)/1000;
SysFileClose(hFile:= hFile_);

I am not sure where to go with this code. It does create a CSV file, but I was looking to be able to create a CSV file for a piece of data every second? If anyone has any thoughts or resources I could check out that would be great.