Samraksh / eMote

eMote OS -- Multiple Ports (using .NET MF v4.3)
0 stars 0 forks source link

DataStore has problems running under Visual Studios #371

Open ChrisAtSamraksh opened 8 years ago

ChrisAtSamraksh commented 8 years ago

If I run code that is supposed to erase the NOR initially and then writes and reads data to the NOR it seems to behave fine when run from MFDeploy but when run from within Visual Studios it will not actually erase the NOR. The new data will be appended to the end of the previous data and everything can be read out after that. There are probably other issues when running from within Visual Studios in relation to the DataStore because often my code will fail again and again and then later seem to work fine. I think what was happening is that I was running from within VS and at some point I ran a test from the TestRig which seems to fix the problem, so the next time I run under VS the code works.

My guess is that NOR corruption also can't be fixed until the code is run outside VS, but hopefully fixing the inability to erase the NOR from within VS fixes the possible corruption problem.

The code I used to test is: using System; using Microsoft.SPOT; using Samraksh.eMote.NonVolatileMemory;

namespace test { public class Program { public static void Main() { try { UInt16 experimentIndex; UInt16 size; DataStore dStore; byte[] writeBuffer; byte[] readBuffer; DataReference[] data; DataReference[] dataObj; int errorCounter = 0, errorLimit = 10; DataStoreReturnStatus _retVal;

            bool eraseDataStore = false;
            Debug.Print("Initializing datastore");
            dStore = DataStore.Instance(StorageType.NOR, eraseDataStore);
            experimentIndex = 10;
            size = 5;
            readBuffer = new byte[size];
            writeBuffer = new byte[size];
            data = new DataReference[experimentIndex];
            dataObj = new DataReference[experimentIndex];

            if ((_retVal = dStore.DeleteAllData()) != DataStoreReturnStatus.Success)
            {
                Debug.Print("Cannot delete the data store; return value " + _retVal);
            }

            Debug.Print("Initializing data");
            for (UInt32 dataIndex = 0; dataIndex < experimentIndex; ++dataIndex)
            {
                data[dataIndex] = new DataReference(dStore, size, ReferenceDataType.BYTE);
                for (int i = 0; i < size; i++)
                {
                    writeBuffer[i] = (byte)('c' + (i%26));
                }

                if (data[dataIndex].Write(writeBuffer, size) != DataStoreReturnStatus.Success)
                {
                    Array.Clear(writeBuffer, 0, writeBuffer.Length);
                    errorCounter++;
                    if (errorCounter > errorLimit)
                    {
                        Debug.Print("Data write failure - test Level_0E failed");
                        return;
                    }
                    else
                    {
                        continue;
                    }
                }

                Array.Clear(writeBuffer, 0, writeBuffer.Length);
            }

            Debug.Print("Creating datareference");
            DataReference[] dataRefArray = new DataReference[experimentIndex];

            Debug.Print("reading datareference");
            //Get the data references into dataRefArray.
            ushort arrayOffset = 0;
            if (dStore.ReadAllDataReferences(dataRefArray, arrayOffset) != DataStoreReturnStatus.Success)
            {
                Debug.Print("ReadAllDataReferences - test Level_0E failed");
                return;
            }
            while (dataRefArray[9] != null)
            {
                Debug.Print("reading dataRefArray");
                for (UInt32 dataIndex = 0; dataIndex < dataRefArray.Length; ++dataIndex)
                {
                    if (DataStoreReturnStatus.Failure == dataRefArray[dataIndex].Read(readBuffer, 0, size))
                    {
                        Debug.Print("Read failed - test Level_0E failed");
                        return;
                    }
                    else
                    {
                        Debug.Print("Read succeeded");
                    }
                    Debug.Print(((char)readBuffer[0]).ToString() + " " + ((char)readBuffer[1]).ToString() + " " + ((char)readBuffer[2]).ToString() + " " + ((char)readBuffer[3]).ToString() + " " + ((char)readBuffer[4]).ToString());
                    Array.Clear(readBuffer, 0, readBuffer.Length);
                }
                arrayOffset += experimentIndex;
                if (dStore.ReadAllDataReferences(dataRefArray, arrayOffset) != DataStoreReturnStatus.Success)
                {
                    Debug.Print("ReadAllDataReferences != DataStoreReturnStatus.Success");

                }
            }

            Debug.Print("Test Level_0E successfully completed");
        }
        catch (Exception ex)
        {
            Debug.Print(ex.Message);
            Debug.Print("Test Level_0E failed");
            return;
        }

    }
}

}

AnanthAtSamraksh commented 8 years ago

but when run from within Visual Studios it will not actually erase the NOR.

Just a minor thing, but in the code bool eraseDataStore = false is set to false. Did you set it to true when running from Visual Studio?

ChrisAtSamraksh commented 8 years ago

I tested a number of combinations of: DataStore.Instance(StorageType.NOR, eraseDataStore); and dStore.DeleteAllData();

I never could get the NOR to erase under VS even if I set (eraseDataStore = true) and I used dStore.DeleteAllData().

If I stopped debugging under VS and connected MFDeploy and hit the reset button on the .NOW then the NOR would be erased every time and only experimentIndex records written. Or if I stopped debugging and hit the reset on the .NOW and then started debugging under VS, then the NOR would have 2*experimentIndex records written (the experimentIndex records from after the reset button and the experimentIndex records from running VS).