| Author |
Message |
|
Ford Prefect
Senior Roboticist
Joined: Sat Mar 01, 2008 12:52 pm Posts: 936 Location: a small planet in the vicinity of Beteigeuze
|
 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)
_________________ Ford Prefect
Never purchase release 1.x ! (ancient programmer's wisdom) "Don't argue with idiots. They'll drag you down to their level and then beat you with experience."
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 |
|
 |
|
starwarslegokid
Moderator
Joined: Wed Jan 31, 2007 3:39 am Posts: 298 Location: San Diego, California. USA
|
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 |
|
 |
|
Ford Prefect
Senior Roboticist
Joined: Sat Mar 01, 2008 12:52 pm Posts: 936 Location: a small planet in the vicinity of Beteigeuze
|
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];
_________________ Ford Prefect
Never purchase release 1.x ! (ancient programmer's wisdom) "Don't argue with idiots. They'll drag you down to their level and then beat you with experience."
|
| Wed Mar 12, 2008 3:16 am |
|
 |
|
Dick Swan
Creator
Joined: Fri Feb 09, 2007 9:21 am Posts: 613
|
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 |
|
 |
|
Ford Prefect
Senior Roboticist
Joined: Sat Mar 01, 2008 12:52 pm Posts: 936 Location: a small planet in the vicinity of Beteigeuze
|
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?
_________________ Ford Prefect
Never purchase release 1.x ! (ancient programmer's wisdom) "Don't argue with idiots. They'll drag you down to their level and then beat you with experience."
|
| Wed Mar 12, 2008 4:45 am |
|
 |
|
Dick Swan
Creator
Joined: Fri Feb 09, 2007 9:21 am Posts: 613
|
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 |
|
 |
|
Ford Prefect
Senior Roboticist
Joined: Sat Mar 01, 2008 12:52 pm Posts: 936 Location: a small planet in the vicinity of Beteigeuze
|
hi,
thanks for the basic English reply
but I still haven't understood everything...
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...?
_________________ Ford Prefect
Never purchase release 1.x ! (ancient programmer's wisdom) "Don't argue with idiots. They'll drag you down to their level and then beat you with experience."
|
| Wed Mar 12, 2008 5:15 am |
|
 |
|
Dick Swan
Creator
Joined: Fri Feb 09, 2007 9:21 am Posts: 613
|
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 |
|
|