Writing Variables to Flash
TurnipTruck
Posts: 1,485
Greetings,
I would like to start writing variables to flash memory of Netlinx masters. Could someone give me a few pointers?
Let say I have an integer varaible of 50 bytes, nVaraible
I would like to write it to the memory during the course of program running then be able to retreive it at a startup.
Thanks!
I would like to start writing variables to flash memory of Netlinx masters. Could someone give me a few pointers?
Let say I have an integer varaible of 50 bytes, nVaraible
I would like to write it to the memory during the course of program running then be able to retreive it at a startup.
Thanks!
0
Comments
Alternatively use file_open, file_write_line, file_read_line, file_close.
Something about PERSISTENT is that it doesn't work in modules Only in main program
Vince
Thanks.
So you can do something like that to get back content of your structure from your file at startup:
File_Index = FILE_OPEN('RSS_News_Reader_Feeds.backup',FILE_READ_ONLY) // Open File in Read Only Mode
IF (File_Index > 0) // File exists with content of structure so we read it
{
FILE_READ(File_Index,Buffer_Feeds,MAX_LENGTH_STRING(Buffer_Feeds)) // Read data from file and put it in buffer
STRING_TO_VARIABLE(Feeds_Bookmark,Buffer_Feeds,1) // Put data back from buffer in structure
FILE_CLOSE(File_Index)
There's a function in a button event to initate write the file to RAM which I usually put on the "EXIT" device button and another function in the devices data_event to read when the device comes online. I haven't tested this to see if this is where it should be for this device but that's where it is now.
This should almost be plug and play once you change names around.
The function "fnXM_FileRead(CHAR icReadFile[])" doesn't appear to do any thing in this example so don't let it confuse you. I just grabbed it while getting other stuff.
Any way with the change from STACKS back to a created buffer it's decoding properly so now it's almost plug and play.
Fair enough, but you *can* use persistent variables in a module, you just can't declare them in the module. You have to pass them through the module header.
As adding a new variable is such a pain when you pass it through the header, I use a large generic array so that the header never changes and you can just add a subscript constant to an include file to refer to a new variable.
Excepted you can't do that with structure :-(
Yep, that's why you have to use a single generic array instead if you don't want to change the module header all the time..
However, I will write to flash if I have a large amount of data to store, or to log events.
Good point. As I'm new to writing modules, I'm loving the fact that they can be droppoed right into a program without modifying declarations in any way. But, it seems reasonable to pass in the occasional variable that needs to be persistent.
Plan A:
a) Declare the new variable in the mainline in as many instances as you need, all with different names.
b) Add it to each of the module instance declarations in the mainline, making sure you get the different spellings right and you put them in the same place each time.
c) Add it to the module header, deciding whether you want to use the same naming, and ensuring that you declare it in the same way, and you put it in the right place.
d) Write the code that uses it, ensuring that you use the internal name not the external name if they are different.
Plan B:
a) Add a constant to your general include file which is a subscript to the generic array.
b) Write the code that uses it.
Plan A isn't that arduous but I found when building a large multi-room project (80 rooms in a dozen phases) that incrementally grew (sometimes the best way to build large projects) that I was endlessly adding configuration variables (sounds iffy but it was fine) and Plan B was much more likely to compile first time and work first time so I could get on with the important stuff.
Of course you do need wrapper code for your generic array, but you only have to write that once.