View unanswered posts | View active topics It is currently Sat Jul 26, 2014 9:16 am






Reply to topic  [ 8 posts ] 
read /write files to NXT flash memory 
Author Message
Guru
User avatar

Joined: Sat Mar 01, 2008 12:52 pm
Posts: 1030
Post read /write files to NXT flash memory
is it now possible to read and write files to NXT flash memory?

(e.g., sample data during run time,
write them to a log file,
shut down NXT,
at next start up: read sampled data
and continue at the same state as before shut down)

_________________
regards,
HaWe aka Ford
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;task main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PutPixel(x,y);}}}while(1)}


Last edited by Ford Prefect on Wed Mar 12, 2008 7:24 am, edited 1 time in total.



Tue Mar 11, 2008 11:29 am
Profile
Moderator
Moderator
User avatar

Joined: Wed Jan 31, 2007 3:39 am
Posts: 299
Location: San Diego, California. USA
Post 
Yes, you can save data into a text file. You can find all the commands in the help topics found in the menu:

Help/ Help Topics/ File Access

Let me know if this helps.
Scott B-)

_________________
Mmmm Legos B-)

My Robot Projects:
http://www.freewebs.com/robotprojects/


Tue Mar 11, 2008 7:25 pm
Profile WWW
Guru
User avatar

Joined: Sat Mar 01, 2008 12:52 pm
Posts: 1030
Post 
no, sorry, I dont want to save text, I want to save int and float variables.

e.g. about 1000 short int of this structure have to be stored:

short Map[36][36];

_________________
regards,
HaWe aka Ford
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;task main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PutPixel(x,y);}}}while(1)}


Wed Mar 12, 2008 3:16 am
Profile
Creator
Creator

Joined: Fri Feb 09, 2007 9:21 am
Posts: 614
Post 
There are built-in functions for reading and writing short, long and float variables. These write the "raw" values of 2-bytes, 4-bytes and 4-bytes to the file so that when you read them back you have to ensure that you read exactly the order and number of fields that you initially wrote.

One potential issue is that the NXT and the PC have, as I recall, different "Endianness" (i.e. is least significant byte first or last byte written) so you may have to do conversion if you're writing on NXT and then reading on PC.

Another quirk of the NXT hardware is that writing to flash takes 3 to 6 milliseconds per 256-byte sector written during which time the user program is stalled since the hardware cannot do any other flash memory accesses (like read next opcode of user program) when write is in progress.

The best way to solve the "Endianness' problem is to write ASCII text to the file and then have the PC convert these back to numbers.

Another quirk is that you must tell the file system in advance when file is created how much memory to allocate to the file. If you specify wrong, then the file may be erased when you close it. Or you may have extra unrecovered space left at the end of the file if you write too few bytes. The type of 'error' treatment depends on whether you created a linear (i.e. contiguous locations in memory) file or discontiguous file. The file system expects that you write exactly the allocated size of the file.

If you fail to close a file, then the NXT firmware should hopefully close it for you when your program terminates.


Wed Mar 12, 2008 3:29 am
Profile
Guru
User avatar

Joined: Sat Mar 01, 2008 12:52 pm
Posts: 1030
Post 
sorry, I don't understand anything of what you explained above.
Could you please translate it to German?
Or even express it much more simple?

_________________
regards,
HaWe aka Ford
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;task main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PutPixel(x,y);}}}while(1)}


Wed Mar 12, 2008 4:45 am
Profile
Creator
Creator

Joined: Fri Feb 09, 2007 9:21 am
Posts: 614
Post 
The "write float" function will take a float variable and write it as the next four consecutive bytes in the file. I can't remember the order, but it is probably least significant byte first and the fourth byte is the most significant.

For example, if you want to write 100 variables in an array a simple loop over the array contents containing a write string will do it.

The "read float" function will do the opposite. It will read the next four bytes of the file into a float variable.

I think the NXT and PC have opposite "endianness" (i.e. byte order). If you tried to read above using a C program in Windows, then Windows might expect the LSB as the last byte rather than the first. Endianness is hardware platform dependent.

The way to eliminate dependency on the "endianness" is to write the data in ASCII readable text. So if the float value is 15.3 you convert to ASCII text followed by a comma to indicate end of value. This is easy to do with the string formating routines and then use the "write string' function. The problem is reading the data back where you'll have to write a parser to scan the ASCII text char by char and convert to numeric value. There's an example in the "Bluetooth Samples" folder that show how to do this when reading an ASCII text string from a GPS receiver.

Hope above is a better explanation.


Wed Mar 12, 2008 4:59 am
Profile
Guru
User avatar

Joined: Sat Mar 01, 2008 12:52 pm
Posts: 1030
Post 
hi,
thanks for the basic English reply ;-)
but I still haven't understood everything... :(

Dick Swan wrote:
I think the NXT and PC have opposite "endianness" (i.e. byte order). If you tried to read above using a C program in Windows, then Windows might expect the LSB as the last byte rather than the first. Endianness is hardware platform dependent.

I don't want to store on the PC, I want to store a "big file" on the NXT when the NXT program terminates, and I want to read this file on the NXT at every new start of the program.

What do I need to know, writing and later reading just on the NXT flash memory
a) integer
b) float
c) boolean values?

Maybe you can give me some kind of example code, just writing / reading a file with 4 different values of each variable type...?

_________________
regards,
HaWe aka Ford
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;task main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PutPixel(x,y);}}}while(1)}


Wed Mar 12, 2008 5:15 am
Profile
Creator
Creator

Joined: Fri Feb 09, 2007 9:21 am
Posts: 614
Post 
You already have the example you want. It's in the sample files folder of ROBOTC names "NXT File IO Tests.c".


Wed Mar 12, 2008 5:33 am
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 8 posts ] 

Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.