This is the same as the fsFileWriteString, but it will add a carriage return value (0D hex or 13 decimal) followed by a new line value (0A hex or 10 decimal) in the end of the string. Each write will start from the location set by the file data pointer (changed with fsFileSeek). After each successful write operation, the file data pointer is automatically incremented.
Input:
fd : FILE
FILE descriptor for the file retrieved from fsFileOpen or fsFileCreate.
str : STRING
The string to be written to the file.
Returns: INT
Number of bytes written to file.
Declaration:
FUNCTION fsFileWriteStringNL : INT;
VAR_INPUT
fd : FILE;
str : STRING;
END_VAR;
Example:
INCLUDE rtcu.inc
VAR
fdLog : FILE;
doLog : BOOL := TRUE;
END_VAR;
FUNCTION WriteLog;
VAR_INPUT
text : STRING;
END_VAR;
VAR
rc : INT;
END_VAR;
IF fsFileStatus(fd := fdLog) = 0 THEN
rc := fsFileWriteStringNL(fd := fdLog, str := text);
IF rc <> (strLen(str := text) + 2) THEN
DebugFmt(message := "error \1 when writing string to prg1.log", v1 := rc);
doLog := FALSE;
fsFileClose(fd := fdLog);
END_IF;
END_IF;
END_FUNCTION;
PROGRAM FileExample;
VAR
fdGpslog : FILE;
gps : gpsFix;
doGpsLog : BOOL := TRUE;
iter : DINT := 1;
str : STRING;
rc : INT;
END_VAR;
fsMediaOpen(media := 0);
gpsPower(power := ON);
IF fsFileExists(name := "prg1.log") THEN
fdLog := fsFileOpen(name := "prg1.log");
ELSE
fdLog := fsFileCreate(name := "prg1.log");
END_IF;
IF fsFileStatus(fd := fdLog) <> 0 THEN
DebugFmt(message := "File $"prg1.log$" not open, error code=\1", v1 := fsFileStatus(fd := fdLog));
doGpsLog := FALSE;
END_IF;
IF fsFileExists(name := "gpslog.dat") THEN
fdGpslog := fsFileOpen(name := "gpslog.dat");
ELSE
fdGpslog := fsFileCreate(name := "gpslog.dat");
END_IF;
IF fsFileStatus(fd := fdGpslog) <> 0 THEN
DebugFmt(message := "File $"gpslog.dat$" not open, error code=\1", v1 := fsFileStatus(fd := fdGpslog));
END_IF;
BEGIN
gps();
IF gps.mode > 1 THEN
IF doLog THEN
WriteLog(text := "GPS position calculated");
END_IF;
IF doGpsLog THEN
fsFileWrite(fd := fdGpslog, buffer := ADDR(gps.linsec), length := SIZEOF(gps.linsec));
fsFileWrite(fd := fdGpslog, buffer := ADDR(gps.latitude), length := SIZEOF(gps.latitude));
fsFileWrite(fd := fdGpslog, buffer := ADDR(gps.longitude), length := SIZEOF(gps.longitude));
END_IF;
END_IF;
IF doLog THEN
str := strFormat(format := "Iteration \4", v4 := iter);
WriteLog(text := str);
END_IF;
Sleep(delay := 5000);
iter := iter + 1;
END;
END_PROGRAM;
|