Write text file

Hi David,

Okay, I wrote a little code to show how you might log this info to a file, which I’ll also attach (WriteTextFile.Archive.D07182012.T093541.zip (2.59 KB)):

// Initialize info to log to be an empty string;
sInfoToLog = ""; // If it says empty throughout the checks, then we have nothing to log
 
// Check for trouble
If (IsVariableTrue(Cure_On)) then
  if (not(Di_Combustion_North)) then
    //save record of di_Combustion_North     
    // Here's where you log the note to self
    sInfoToLog = "Cure_On is true while Di_Combustion_North is not!"; 
  else 
  //do Nothing and check the next if....
  endif
endif
 
// After we finish checking everything, we'll see if we made any notes 
// that should go in the file:
if ( GetStringLength( sInfoToLog ) > 0 ) then // we have something to log  
  // Build timestamp 
  DateToStringDDMMYYYY(sDate);
  TimeToString(sTime);  
  // Prepend the date/time and add a CR LF at the end to make it easier to read
  sInfoToLog = sDate + " " + sTime + " " + 
                     sInfoToLog + chr(0xD) + chr(0xA);  
 
  // assuming chFile has already been initialized to something like: "file:a,LogFile.txt"
  // (the "a" after "file:" means we're appending to the file if it already exists
  nCHResult = OpenOutgoingCommunication(chFile);  
  if (nCHResult == 0) then // we opened the file okay
    nCHResult = TransmitString(sInfoToLog, chFile);
    CloseCommunication(chFile);
  else
    // we couldn't open the file, maybe log the info to the scratch pad 
    // or send an SNMP trap instead
  endif
endif

After I run this, a file will be created on my controller. I can see this using Windows Explorer like this:


The contents look something like this (after one time through):
18/07/2012 08:22:37 Cure_On is true while Di_Combustion_North is not!

I’m guessing you’d delete the file after you read it? You would need to make sure you don’t fill up the file space on the PAC – which is about 2MB. Also, if you think the power might go out before you have a chance to read that file, there are options for storing data in non-volatile memory too.

Hope that helps!

-OptoMary

1 Like