Deserialization error

Good day all,

I am using the following libraries running on an IMP003

#require “Serializer.class.nut:1.0.0”
#require “SPIFlashLogger.class.nut:1.1.0”

I am getting the following error message and cannot seem to fin out how to correct it.
2016-06-06 10:31:34 UTC-4 [Device] ERROR: Deserialisation error at 0x05ef7c: CRC err: 0x3b != 0x6c 2016-06-06 10:31:34 UTC-4 [Device] ERROR: Deserialisation error at 0x082f7e: CRC err: 0x57 != 0x1a 2016-06-06 10:31:34 UTC-4 [Device] ERROR: Deserialisation error at 0x023f85: CRC err: 0x07 != 0xcf 2016-06-06 10:31:35 UTC-4 [Device] ERROR: Deserialisation error at 0x054f85: CRC err: 0xf5 != 0x8e 2016-06-06 10:31:35 UTC-4 [Device] Finished sending and all entries are erased

this is code where I think the error is being produced.

`function writeToAgent (){
//server.log(“Entered writeToAgent”);
// Increment a counter
if (!(“count” in nv)) nv.count <- 1;
else nv.count++;

// If we have more than 100 samples
//if (nv.count > 5 && wifiState == 1) {

if (wifiState == 1) {
// Send the samples to the agent
logger.readAsync(
function(dataPoint, next) {
//server.log(“Started sending all entries”);
// Send the dataPoint to the agent
agent.send(“data”, dataPoint);
// Wait a little while for it to arrive
imp.wakeup(0.5, next);
},
function() {
server.log(“Finished sending and all entries are erased”);
// Reset counter
nv.count <- 1;
//nv.id <- 0;
// Go to sleep when done
imp.onidle(function() {
// Get and store position pointers for next run
local position = logger.getPosition();
//server.log("pointer position is: "+ position);
nv.position <- position;

            // Sleep for 1 minute
            //imp.deepsleepfor(60);
        });
    }
);

} else {
// Go to sleep
imp.onidle(function() {
// Get and store position pointers for next run
local position = logger.getPosition();
nv.position <- position;

    // Sleep for 1 minute
    //imp.deepsleepfor(60);
});

}
//server.log(“Exited writeToAgent”);
}`

I think we need an idea of what data you’re attempting to serialize and how. I can’t see any calls to Serializer.deserialize() in the code above - ie. where your constructor is (ie. how SpiFlashLogger is initialized and how the data is written to it before reading)

Hi smittytone,

the constructor used is the following :
`// Initialize Logger to use the entire SPI Flash
logger <- SPIFlashLogger();

// Check if we have position information in the nv table:
if (“nv” in getroottable() && “position” in nv) {
// If we do, update the position pointers in the logger object
logger.setPosition(nv.position);
} else {
// If we don’t, grab the position points and set nv
local position = logger.getPosition();
nv <- { “position”: position, “id”: 0 };
};`

The data being serialize is the following:
impid: 0c2a690be061, latitude: , longitude: , logtime: 1465226095, temperature: -1.66641,wifistate1 This is the data received by AGENT. The code is working as it is saving the data and then sending it to the Agent. But for some reason it throws the Deserialization error.

The code used to write the data is:
`function saveData(){
//server.log(“Entre a saveData”);
sensorData <- {
id = [],
time = [],
lat = [],
lon = [],
temp = [],
wifi = []
};
//set new id
i = nv.id +1;
server.log(i);

    //Save the Temp and GPS data to table
    sensorData.id.append(i);
    sensorData.wifi.append(wifiState);
    sensorData.time.append(time());
    sensorData.lat.append(readingLat);
    sensorData.lon.append(readingLon);
    sensorData.temp.append(readingTemp);
          
    //update nv table with last id
    nv.id <-i;
    logger.write(sensorData);
    
    sensorData.clear();
    sensorData=[];
    //server.log("Sali de saveData");

}`

This is a tricky one to debug, because you’re (quite reasonably) using the default Serializer established by SPIFlashLogger. I think what I would do is embed the SPIFlashLogger library code manually (copy and paste the code from GitHub) so you can add in some debug code of your own to help you monitor the serialize-write… read-deserialize process and get some idea about what’s happening. For example, you might want to check that the data read back is the data your wrote in, allowing you to see if the error is in the CRC code calculation, or the data is being garbled. I suspect the latter but don’t have imp003 kit here to try.