Writing and Reading Flash
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!
0
Comments
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'))" }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!
It's non-zero (46 to be exact). send strings back to the master result in zero.
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.:)
I'm good to go! Thanks, works like a champ.