fsFileWriteStringNL (Function)

Top  Previous  Next

Architecture:

X32 / NX32 / NX32L

Device support:

All

Firmware version:

1.00 / 1.00.00


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
    // Write text to log
     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;
 
// Media
fsMediaOpen(media := 0);
gpsPower(power := ON);
 
// Open file for program log
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;
 
// Open file for log of GPS positions
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
    // Log
    IF doLog THEN
        WriteLog(text := "GPS position calculated");
    END_IF;
    // log position
    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;
 
  // Log
  IF doLog THEN
     str := strFormat(format := "Iteration \4", v4 := iter);
     WriteLog(text := str);
  END_IF;
 
  Sleep(delay := 5000);
  iter := iter + 1;
END;
 
END_PROGRAM;