Examples - Datalogger (simple)

Top  Previous  Next

//-----------------------------------------------------------------------------
// Datalogger testprogram
//-----------------------------------------------------------------------------
INCLUDE rtcu.inc
 
VAR
  LogWriter : LogWrite; // FB used for writing log entries
  LogReader : LogRead; // FB used for reading entry at current read position
END_VAR;
 
//---------------------------------------------------------------------------
// Mainprogram
//---------------------------------------------------------------------------
PROGRAM test;
  // Test if the Datalogger is initialized
  IF NOT LogIsInitialized(key:=4712) THEN
    // Initialize datalogger system, 2 values per record, 10 records in total
    LogInitialize(key:=4712, numlogvalues:=2, numlogrecords:=10);
    DebugMsg(message:="Log initialized");
  ELSE
    DebugMsg(message:="Log was already initialized");
  END_IF;
 
  // Check how many records we have room for in the datalogger
  // Should be 10
  DebugFmt(Message:="(a) LogMaxNumOfRecords() = \4", v4:=logMaxNumOfRecords());

  // Check how many values in each record
  // Should be 2
  DebugFmt(Message:="(b) LogValuesPerRecord() = \1", v1:=logValuesPerRecord());

 

  // Make an entry in the datalogger
  LogWriter(tag:=1, value[1]:=10, value[2]:=20);
  // And another
  LogWriter(tag:=2, value[1]:=11, value[2]:=21);
 

  // Check how many records present in the datalogger
  // Should be 2
  DebugFmt(Message:="(c) LogNumOfRecords() = \4", v4:=logNumOfRecords());

 

  // Set read position to the oldest (first) record
  LogFirst();
  // Fetch data from read position
  LogReader(); // LogReader.tag is 1, value[1] is 10, value[2] is 20
  // Advance read position (forward in time)
  LogNext();
  LogReader(); // LogReader.tag is 2, value[1] is 11, value[2] is 21
  // LogReader.tag should be 2
  DebugFmt(Message:="(d) LogReader.tag = \1", v1:=LogReader.tag);

 

 // Advance read position (backward in time)
  // We are now back to the first record
  LogPrev();
  LogReader(); // LogReader.tag is 1, value[1] is 10, value[2] is 20
  // LogReader.tag should be 1
  DebugFmt(Message:="(e) LogReader.tag = \1", v1:=LogReader.tag);

 

  // Modify the current record
  LogReWriteTag(tag:=123); // This will change the tagvalue of the current record to 123
  // Now read record
  LogReader(); // LogReader.tag is now 123, value[1] is 10, value[2] is 20
  // LogReader.tag should be 123
  DebugFmt(Message:="(f) LogReader.tag = \1",   v1:=LogReader.tag);

  DebugFmt(Message:="(f) LogReader.year = \1",  v1:=LogReader.year);

  DebugFmt(Message:="(f) LogReader.month = \1", v1:=LogReader.month);

  DebugFmt(Message:="(f) LogReader.day = \1",   v1:=LogReader.day);

  DebugFmt(Message:="(f) LogReader.hour = \1",  v1:=LogReader.hour);

  DebugFmt(Message:="(f) LogReader.minute = \1",v1:=LogReader.minute);

  DebugFmt(Message:="(f) LogReader.second = \1",v1:=LogReader.second);

BEGIN

 

END;
 
END_PROGRAM;