Write text file


Is there way that I can write to a text file to my documents without a FTP client?

all I need is if a Digital input becomes false that it will log that and I know where to start
troubleshooting. I have 8 digital inputs that have to be true at all times, if one turns false the system has to shut down for safety reasons.

I would like if the text file would just keep growing, or if the option is there to delete data that’s a month or older.

any code or advise would be greatly appreciated.



Hi David,

Not sure if you saw this post where I included a couple of links to resources about file and ftp comm handles. I’m guessing you’re using a PAC? If so, the built-in file system (accessible via FTP) can be written to using that “file:” comm handle I mentioned.

I’m curious, though, why do you want to store this information to a file (vs. perhaps a persistent table)? I’m guessing you’ll upload it to a PC later? Will it include more information, like maybe about what happened before that digital point changed, meaning you had to shut down? Will the PAC this is running on be part of the system that’s shut down? If so, and the file has not been stored to flash, it will disappear.

Can you tell us more so we can ponder many options and the pros/cons? Sounds like an interesting problem!



No I just want to save it for about a week,
if a fan stops (meaning one of my air proven switches opens) it will shutdown the oven and the operator has to go and restart the oven and make sure that flames and everything comes on and oven goes again.
I like to have a optoscript runing in a chart that will monitor the digital inputs, if during a runing (cure) session a digital point opens it will write time/date and digital input to a text file or any method, so if the people at night shift have had a problem/shutdown then I can look in the morning when I come in to see what triger the shutdown, which part of the oven fail, it’s just a record for maintainance reason only.

If (IsVariableTrue(Cure_On)) then
if (not(Di_Combustion_North)) then
save record of di_Combustion_North
do Nothing and check the next if.
it only monitors it if the system is in cure mode, means it’s baking.
I have a bout 13 inputs to monitor if one if them fails the oven can’t continue, it will triger an alarm if one of the inputs fail, and I like to know what triger the alarm.

hope it makes sence
maybe the PAC has already a feature to do this.



Thinking outside the box…

While PAC Control provides a useful control environment to program anything that you might imagine, there is also a whole world of predefined functionality in the I/O memory map for lazy engineers. Your problem can easily be solved with no programming, just by configuring the event/reaction functionality of PAC Brains.

  • The event would be to detect the state of a digital input.
  • The reaction would be to send an event message via SNMP to your PC
By using a simple SNMP trapper program in your PC, the received events can be saved directly to an ASCII file complete with system time/date stamp.

For more details and an introduction into the wonderful world of event/reaction programming, including email messaging and advanced plug ins, check out Chapter 4 of 1704 PAC Manager User Guide http://www.opto22.com/site/documents/doc_drilldown.aspx?aid=3161. It’s guaranteed to blow your mind wide open!

You can pickup the SNMP Trapper program from http://www.bttsoftware.co.uk/snmptrap.html, an excellent piece of simple software that is well worth a donation. (Think of all the time it has saved you!):cool:

Requesting Code Sample - Writing to Text file in OptoScript

Wow, that is really outside the box! However, as much as I love creative outside-the-box thinking, I’m not sure I would recommend SNMP or event/reactions to anyone who didn’t require them for some other reason. Because:

  1. Although SNMP stands for Simple Network… I forget the rest because I start giggling at the “Simple” because it is NOT.

  2. Event/reactions were the method of programming you had before we put the control engine (and the ability to run a nice, easy-to-understand flow chart) into your hardware.

For this application, it sounds like he’s wanting simple record, like the equivalent of a printout, that he can have a look at the next morning to see how the night went. If a text file is created on his device he could look at it from practically any PC using just Windows Explorer – no need for another piece of software to be installed.

Perhaps I’ll whip up some samples for both approaches and we could compare and contrast, just for fun!



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!"; 
  //do Nothing and check the next if....
// 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 
  // 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);
    // we couldn't open the file, maybe log the info to the scratch pad 
    // or send an SNMP trap instead

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!



thank you very much
I will give it a run, I think this is exactly what I was looking for.


You’re welcome! Thanks for the question.


Thanks Mary

As Mark Twain wrote

"She was wise, subtle, and knew more than one way to skin a cat"
Or maybe SNMP just stands for [B]S[/B]imply [B]N[/B]ot [B]M[/B]ary’s [B]P[/B]reference?

Agreed that most of the worlds problems can be solved with OptoScript, but coming from a bygone era, where a computers hidden power was unleashed through accessing its BIOS, you still can’t beat the speed, simplicity and realtime functionality only available in the I/O MemoryMap, especially when working in milliseconds or even microseconds.

"Using the Opto22 I/O MemoryMap you can play notes you have never even heard before"
Not for beginners, but direct memorymap access is still an excellent complement to OptoScript, and should form part of any OptoEngineers programming toolbox. But of course as any OptoMaster knows, the force must be used wisely!

My favourite PAC Control instructions and a great place to start learning about the hidden gateway between OptoControl and the PAC I/O MemoryMap underworld is:

[B]Read Number from I/O Unit Memory Map
Write Number to I/O Unit Memory Map[/B]

FYI. Within our [I]“7th Level OptoMaster [I]Enlightenment”[/I] [/I]holistic training, the PAC I/O MemoryMap Area is referred to in religious cosmological terminology, as being the Opto22 equivalent of [B][I]Sheol[/I][/B]. You might like to look that one up Mary!:cool:


Wow! Such entertaining and educational posts to brighten my day this morning. I also looked up the “skin a cat” quote – didn’t realize Mark Twain (I love Twain!) was involved: http://www.worldwidewords.org/qa/qa-mor1.htm

I’ve also used the Memory Map for converting chunks of data from one type to another faster than can be done in OptoScript. Not sure if that’s all the way up at the 7th Level OptoMaster, but maybe 3 or 4. :slight_smile:


I like the simplicity of the code you give me it serves the purpose that I need it for.
now I’m wondering if there is a way to either keep the txt file to a limit size like starting to delete old data month and older, if not then I guess I have to setup a schedule that will delete the txt file maybe every month.

hopefully I will never get that many errors recorded but I like to make it so that I don’t have to worry about little things like getting a txt file with 1000s of line of text, with notepad sometimes having problem opening huge txt files.

I only need about a weeks log.

thanks again


Just out of curiosity David, would using “PAC Sim” as your controller simulator help? What that software does is that it simulates the PAC controller on your PC, and with your control procedure configured to PAC Sim as your controller, and with PAC Sim running, all your data will be logged onto your hard drive.

The bad thing is that all your data log files will be stored in the same directory as the PAC Sim ([I]which is typically C:\Windows\system32\OptoCom directory[/I]). And no, you cannot change the installation folder for PAC Sim.


FYI, although the default location for files created by PAC Sim is the installation folder, you can specify a different path as part of the filename to put it somewhere else. PAC Sim does have a time limit on it, which might put a damper on things. However, SoftPAC™, will not have this limit!

PAC Display’s Historical Logging could also be used to log values on the PC too.

In terms of what banmandf has going so far, I figured the person who looked at the file could delete it when they were done.
But automatically limiting the file length could be done in a couple of ways. Let me think about that a bit and make few suggestions here soon…


David -

Rather than worry about how much room you have locally on that controller, is the PAC on a network that’s connected to the Internet? Is it running a firmware version 9.1 or newer?

I’ve been working on a little example that will let you poke data directly into a google doc spreadsheet. That might be handy if you want to check the log from anywhere!



This is the PAC Display Runtime Of the Oven on a touchscreen windows 7 panel mount pc.
I do use Pac Sim when I update or develop at home, but at work I just Run it Of the Snap-Pac-R1,
it has run this week flawless, maybe 1 or 2 alarm, which is very good considering its only 3 week installed.

the reason I wanted to only keep partial of the txt file or keep it like only 30 lines long, is we are going to build 2 more controllers, and the will be installed 13 hours away from where I live, so if I can keep the whole process as automated as possible is just good for me.
but it’s not a big deal, it will likely take years before the file would get to big.
I was looking at a table with limited lenght like 100, but I could not figure the table out, since I have not have a need to work with a table,

but I did find a bat file code that I might be able to make it work.

thanks for all the input from opto22 you have all been a great help.



The code works perfect on the SNAP PAC Sim, but as soon as I run it on the SNAP PAC Controller R1 it will save the txt file to flash but the txt file does not show in windows explorer,
So you mention Google Doc. I’m wondering if there is a way I can write directly to txt file on my computer hard drive and not to the flash,
I have seen opto software that will save reciept files to windows directory but I’m not sure how to do that. if I coult do that it would save the flash space on the control engine.

thanks David


Hi David,

Windows explorer is one of, if not THE worst FTP client you could ever use. We have nothing but trouble with it when we tried to use in our monthly free training sessions here at Opto 22.
There are plenty of other FTP clients out there, I suggest you download one and try it. (One I like is filezilla).
I would do that before you go down other more complicated roads like uploading your data to Google Docs.

You cant, from the controller, write your data to the PC’s hard drive.
You can write data from the controller to the PC via PAC Display.
As you say, the recipe is the method to use in this regard.
The PAC Display Users Guide will give you the good stuff on how to get this working… Its pretty trivial.
Also dont forget the PAC Display historical data logger function. It also works a treat and you could log any of the ovens data to hard drive in just a few mouse clicks.




Hi David,

Actually, I like FileZilla’s SERVER too. If you have that running on your PC, it’s pretty easy to use an FTP comm handle to send a file from your PAC to the PC running the FileZilla server. You can also use the Send Communication Handle Command with an ftp comm handle to append data to an existing file on that “remove server.”

So many options!

Let us know what you try.



ok, I did not realize that I could not save to hard drive directly, the code otherwise works great, so now I just have to set up a FileZilla server to transfer it to the computer,

the micro SD slot thats on the controller can I use it to log to the SD card instead of controller memory?
if yes what would the communication handler look like? now it’s file:a,Logfile.txt, how easy is it for the SD

thanks for the help/info,
it records the alarms as expected.
I’m looking forward for my next opto project.


Hi David,

Yes! Actually writing to the sd card might be better since:

  1. you don’t have to worry about storing to the controller’s flash (which is an extra step, and if you do it in a fast loop by accident you can wear out that flash “in a flash”)!

  2. just add /sdcard0/ into your path, so you’d have: “file:a,/sdcard0/Logfile.txt”

Just keep in mind the limitations:

  1. sd card must be 2GB max

  2. sd card filenames must have an 8.3 format, so you could have Logfile0.txt and Logfile1.txt but no more than 8 characters for the name and 3 for the extension.

We’re looking forward to your next Opto project too!