Home AMX User Forum NetLinx Studio

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!

Comments

  • AMXJeffAMXJeff Posts: 450
    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'))"
    	
    }
    
  • TurnipTruckTurnipTruck Posts: 1,485
    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!
  • viningvining Posts: 4,368
    TurnipTruck wrote:
    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.
  • TurnipTruckTurnipTruck Posts: 1,485
    vining 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.

    It's non-zero (46 to be exact). send strings back to the master result in zero.
  • Joe HebertJoe Hebert Posts: 2,159
    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.:)
  • TurnipTruckTurnipTruck Posts: 1,485
    Joe Hebert wrote: »
    And you should be good to go.

    I'm good to go! Thanks, works like a champ.
Sign In or Register to comment.