fsFileWrite (Function)

Top  Previous  Next

Architecture:

X32 / NX32 / NX32L

Device support:

All

Firmware version:

1.00 / 1.00.00


This function will write a buffer to a FILE - starting from the location set by the file data pointer (changed with fsFileSeek). After each successful write operation the file datapointer is automatically incremented.

 

Please also see fsFileWriteString for string writing.

 

Input:                

fd : FILE

FILE descriptor for the file retrieved from fsFileOpen or fsFileCreate.

 

buffer : PTR

Address of the buffer that contains the data.

 

length : INT

Number of bytes to write.

 

Returns: INT

Number of bytes written to file.

 

Declaration:

FUNCTION fsFileWrite : INT;
VAR_INPUT
  fd     : FILE;
  buffer : PTR;
  length : INT;
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) 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;