PDA

View Full Version : How do I print to a file?


NCSUseniorDesign
11-10-2009, 02:19 PM
I attempted to use Python scripts to print to a file...but the import sys command doesn't work (assuming sys isn't installed).

So, how can I print to a file? Without using write line()?

I'd prefer to do it as follows:

myfile = open("textfile.txt","w")
print >> myfile, "Latitude: " lat_deg
myfile.close()

In this case, I'm using the provided gpsNMEA.py script provided with Portal. I want to print the GPS coordinates to a file.

Is there a Portal Script that will do this for me?

Thanks

kbanks
11-10-2009, 03:49 PM
You should be able to use all of Python's capabilities from within Portal.

Here for example is a modified version of your script that I just used to write to a file:


from sys import *

def test():
lat_deg = 1234
myfile = open("textfile.txt","w")
print >> myfile, "Latitude: ", lat_deg
myfile.close()


1) I did have to add "from sys import *" at the top. It is true that I have Python installed on my PC (I'm a developer). I don't know if that makes a difference or not.

2) I had to add a comma to the "print" line. Python did not like the literal string "Latitude: " being immediately followed by variable lat_deg.

Reminder - you can "check" scripts without trying to upload them into Portal or a SNAP Node.

3) I stuck a dummy value in lat_deg just to have something to print.

Since you mentioned the gpsNmea.py script, I wonder if you are mixing up Portal scripting with Node scripting?

gpsNmea.py (as written) is meant to run in a SNAP Node. It IS true in node scripts that you cannot use capabilities like "print to file"...

If you mean to run a script within Portal, be sure to load it into the "Portal" node in the list.

NCSUseniorDesign
11-12-2009, 06:29 PM
So, a few things:

1. Thanks for replying so quickly, I appreciate the help.

2. We WERE trying to print to a file from Node scripts rather than the Portal script, BUT...we were semi-successful. While using a "writeLine()" function it DID print to a file from the Node script, but it did not print the Global variables' values, nor do I know the syntax to print the values of those global variables.

3. I then attempted to write a small Portal script but it was having issues with "from sys import *". How do I get sys into the proper library? I installed Python to the computer, but don't know where the "sys" is located.

4. Also, from the Portal Script, how do I get access to the Global variables in the Node scripts???

kbanks
11-12-2009, 07:09 PM
2. We WERE trying to print to a file from Node scripts rather than the Portal script, BUT...we were semi-successful. While using a "writeLine()" function it DID print to a file from the Node script,

By "Node script", I mean a script loaded into a real SNAP Engine (such as the Synapse RF Engine). Because it is loaded into an embedded node (with no disk or file system), there is no way for it to write directly to a file of any kind.

A Node script could make an RPC call to Portal, who could be running a script of his own. The function in THAT script could write to a file.

Is that what your writeLine() function did (Did it invoke a function in your Portal script)?

but it did not print the Global variables' values, nor do I know the syntax to print the values of those global variables.

There is no special syntax to print a global variable. If your script has a global variable named foo, then


print foo


will print it.

If instead of printing it yourself, you are trying to send it to Portal (so HE can print it to a file), then you would use something like:


rpc(PORTAL_ADDR,"writeToFile",foo)


3. I then attempted to write a small Portal script but it was having issues with "from sys import *". How do I get sys into the proper library? I installed Python to the computer, but don't know where the "sys" is located.

Since it is working on my PC, I don't have any ideas. Mark may post something on this tomorrow.

4. Also, from the Portal Script, how do I get access to the Global variables in the Node scripts???

You can't. As I talked about above, the remote nodes will have to REPORT (via RPC) anything you want Portal to know.

Have you looked at the new "Writing Portal Scripts" App note over in the BETA section (look for a ZIP file)?

NCSUseniorDesign
11-12-2009, 08:03 PM
Ok, I'm pretty sure it is now down to a problem with the "from sys import *" or "Python Library Directory". We also used another thread of yours relating to "logging data to a file" and tried to use the logSupport.py script. But again, had issues with the "from logSupport import *". Or at least, that's what we feel it is.

We attempted to use rpc function to send data from a Node to the Portal and I'm sure that will work well and all...but it keeps giving me an error.

Am I supposed to download Python and install it in a specific directory within the Portal folder?

Thanks again for your help

mgenti
11-13-2009, 09:26 AM
Ok, I'm pretty sure it is now down to a problem with the "from sys import *" or "Python Library Directory".

Can you post the script you are currently trying? Also, are you remembering to tell Portal that the scripts has changed after you save it?

Am I supposed to download Python and install it in a specific directory within the Portal folder?

No, the full installation of python is not required to run Portal scripts, it is only useful if you need a library we do not offer. The sys library is included with Portal.

Have you had a chance to look through the developing Portal scripts app note?

kbanks
11-13-2009, 09:29 AM
Are you getting the error messages when you "check" the script? Or when you "upload" it?

(Look in the Portal Reference Manual if you don't understand the distinction).

When "checking" a script, you have to tell Portal (via a field in the GUI) what kind of script you mean for it to be...

NCSUseniorDesign
11-17-2009, 01:31 PM
I get the error when doing both "checking" and "uploading".

perhaps that is the issue though...how do I change what type of script it is? Where in the GUI?

I don't have the code 'on me' currently but below is my attempt at recreating the portal script:

from sys import*

def writeToFile(foo):
myfile = open("C:\..etc..","w")
print >> myfile, "Latitude: ", foo
myfile.close()

*NOTE* in the gpsNmea.py Node script I wrote an rpc command to send the lat_deg global to the portal script via the writeToFile somewhat as follows (syntax may not be perfect, again i don't have the code on me):

rpc(\x00\x00\x01\x01,"writeToFile", lat_deg)

When that didn't work...i attempted to use the "logSupport.py" script I found on another thread. imported it, tried to RPC it, but it kept giving me errors when I attempted to "check" it and "upload it".

Hopefully specifying what type of script it is on the GUI will fix that. But it always gives me an error at line 1 where the "from sys import*" is...

Jheath
11-17-2009, 03:51 PM
If you open the source file in the Portal editor (File->Open File) then you should be presented with a number of option along the title bar (Save icons, Search dialog, etc.). One of the options is a pull-down menu called 'platform'. If you select 'Portal', the green check-mark icon will then check you script for proper syntax in the context that it is meant for Portal and not for a SNAP node.

NCSUseniorDesign
11-19-2009, 05:44 PM
Ok, so...I opened the file but in my title bar, it has the save icons, the search menu, but there is no "Platform" pull-down menu?!

I am using Portal 2.1.2.2

Is that the same version you are using?

OK...well, when I uploaded it THIS time, it did not have an error uploading. It is currently printing zeros to my file, which is what I expected...I'll keep you posted. thanks for your help.

I will probably be posting here soon. ha

kbanks
11-19-2009, 06:49 PM
I am using Portal 2.1.2.2

Is that the same version you are using?

No. We are all using code from the 2.2 series (look in the BETA section of this forum).

Features like the "PLATFORM Pulldown" are new for 2.2. You should upgrade when convenient, and take a look at the SNAP 2.2 Migration Guide, the SNAP 2.2 Firmware Release Notes, and the Portal 2.2 Release Notes to see the highlights of what has changed.