Writing and Reading Flash
TurnipTruck
Junior Member
Greetings,
Getting things on and off the flash is something that still elludes me.
Could someone give me some example code for writing an integer variable value to flash and then reading it back into that integer variable.
Thanks!
Getting things on and off the flash is something that still elludes me.
Could someone give me some example code for writing an integer variable value to flash and then reading it back into that integer variable.
Thanks!
Comments
-
You do not need to use VARIABLE_TO_STRING AND STRING_TO_VARIABLE, but I believe this is the best way to write any variable to flash, end of soap box.
DEFINE_VARIABLE TEST DEFINE_FUNCTION WriteIntegerFlash(CHAR cFileName[], INTEGER nValue) { STACK_VAR SLONG slFileHandle; STACK_VAR CHAR cConvertedValue[100]; slFileHandle = FILE_OPEN(cFileName, FILE_RW_NEW); // VALID FILE HANDLE IF (slFileHandle > 0) { VARIABLE_TO_STRING(nValue, cConvertedValue, 1); FILE_WRITE(slFileHandle, cConvertedValue, LENGTH_STRING(cConvertedValue)); FILE_CLOSE(slFileHandle); } } DEFINE_FUNCTION INTEGER ReadIntegerFlash(CHAR cFileName[]) { STACK_VAR SLONG slFileHandle; STACK_VAR CHAR cConvertedValue[100]; STACK_VAR INTEGER nValue; slFileHandle = FILE_OPEN(cFileName, FILE_READ_ONLY); // VALID FILE HANDLE IF (slFileHandle > 0) { FILE_READ(slFileHandle, cConvertedValue, MAX_LENGTH_STRING(cConvertedValue)); STRING_TO_VARIABLE(nValue, cConvertedValue, 1); FILE_CLOSE(slFileHandle); } RETURN nValue; } DEFINE_PROGRAM WAIT 100 { TEST = TEST + 10; WriteIntegerFlash('test.dat',TEST); SEND_STRING 0,"'INTEGER=',ITOA(ReadIntegerFlash('test.dat'))" } -
Good Stuff!!
DEFINE_FUNCTION WriteIntegerToFlash (CHAR cFileName[], INTEGER nValue_) { STACK_VAR SLONG slFileHandle STACK_VAR CHAR cConvertedValue[100] slFileHandle=FILE_OPEN(cFileName,FILE_RW_NEW) IF (slFileHandle>0) { VARIABLE_TO_STRING (nValue_,cConvertedValue,1) FILE_WRITE (slFileHandle,cConvertedValue,LENGTH_STRING(cConvertedValue)) FILE_CLOSE (slFileHandle) } ELSE SEND_STRING vdvDVM,"'FLASH FILE WRITE ERROR ',ITOA(slFileHandle)" } DEFINE_FUNCTION INTEGER ReadIntegerFromFlash (CHAR cFileName[]) { STACK_VAR SLONG slFileHandle STACK_VAR CHAR cConvertedValue[100] STACK_VAR INTEGER nValue_ slFileHandle=FILE_OPEN (cFileName,FILE_READ_ONLY) IF (slFileHandle>0) { FILE_READ (slFileHandle,cConvertedValue,LENGTH_STRING(cConvertedValue)) STRING_TO_VARIABLE (nValue_,cConvertedValue,1) FILE_CLOSE (slFileHandle) RETURN nValue_ } ELSE SEND_STRING vdvDVM,"'FLASH FILE READ ERROR ',ITOA(slFileHandle)" } //---------- WriteIntegerToFlash("'CHANNEL',cDeviceNumber,'.dat'",nValue) nValue=ReadIntegerFromFlash("'CHANNEL',cDeviceNumber,'.dat'")
Above is what I created with your help. Unfortunately, my return value is always zero, although the correct file name is created on the flash. Any suggestions? Thanks! -
TurnipTruck wrote:
Well what's the value of nValue when you call the function "WriteIntegerToFlash()". For testing you might want to add some more SEND_STRING 0's or step through it in debugger.Unfortunately, my return value is always zero, -
Well what's the value of nValue when you call the function "WriteIntegerToFlash()". For testing you might want to add some more SEND_STRING 0's or step through it in debugger.
It's non-zero (46 to be exact). send strings back to the master result in zero. -
Change this:
FILE_READ (slFileHandle,cConvertedValue,LENGTH_STRING(cConvertedValue))
To this:
FILE_READ (slFileHandle,cConvertedValue,MAX_LENGTH_STRING(cConvertedValue))
Or this:
FILE_READ (slFileHandle,cConvertedValue,100)
And you should be good to go.
When cConvertedValue is declared it has no length so FILE_READ was asking for a max of 0 characters which will get you nowhere fast.:) -
Joe Hebert wrote: »And you should be good to go.
I'm good to go! Thanks, works like a champ.
Categories
- All Categories
- 2.5K AMX General Discussion
- 922 AMX Technical Discussion
- 514 AMX Hardware
- 502 AMX Control Products
- 3 AMX Video Distribution Products
- 9 AMX Networked AV (SVSI) Products
- AMX Workspace & Collaboration Products
- 3.4K AMX Software
- 151 AMX Resource Management Suite Software
- 386 AMX Design Tools
- 2.4K NetLinx Studio
- 135 Duet/Cafe Duet
- 248 NetLinx Modules & Duet Modules
- 57 AMX RPM Forum
- 228 MODPEDIA - The Public Repository of Modules for Everyone
- 943 AMX Specialty Forums
- 2.6K AMXForums Archive
- 2.6K AMXForums Archive Threads
- 1.5K AMX Hardware
- 432 AMX Applications and Solutions
- 249 Residential Forum
- 182 Tips and Tricks
- 146 AMX Website/Forums
