Writing a table as a Blob on a FRAM

Hi,

Using Imp001 I am trying to write a table as a BLOB on a FRAM using the following code:

`
local data = {
“foo” : “bar”,
“timestamps” : [1436983175, 1436984975, 1436986775, 1436988575, 1436990375],
“readings” : [32.5, 33.6, 32.8, 32.9, 32.5],
“otherData” : {
“state” : true,
“test” : “test”
}
}

local serializedData = Serializer.serialize(data);

// Write the data to SPI Flash @ 0x0000
//spiFlash.enable();
//spiFlash.erasesector(0x0000);
//spiFlash.write(0x0000, serializedData, SPIFLASH_PREVERIFY | SPIFLASH_POSTVERIFY);
//spiFlash.disable();

local err = fram.write(0x0000, serializedData, fram.SPIFLASH_POSTVERIFY);

// Read the header information
local dataBlob = fram.read(0x00, 3);
// Get the length from the first two bytes
local len = dataBlob.readn(‘w’);

// Move to the end of the blob
dataBlob.seek(0, ‘e’);

// Read the length of the data starting at the end of the header
fram.readintoblob(0x03, dataBlob, 2);

// Disable the SPIFlash since we’re done
// spiFlash.disable();

// Deserialize the blob
local data = Serializer.deserialize(dataBlob);

// Log some data to make sure it worked:
server.log(data.foo); // bar
server.log(data.otherData.state); // true
server.log(data.otherData.test); // test

server.log(“Readings:”);
for (local i = 0 ; i < data.timestamps.len() ; i++) {
server.log(data.timestamps[i] + ": " + data.readings[i]);
}
`

I am getting the following error message:

2016-03-23 23:58:09 UTC-4 [Device] binary: 63 75 72 72 65 6e 74 20 70 6f 69 6e 74 65 72 20 69 73 3a 20 20 74 65 73 74 73 00
2016-03-23 23:58:09 UTC-4 [Device] ERROR: the index ‘spiFlash’ does not exist
2016-03-23 23:58:09 UTC-4 [Device] ERROR: at main:245
2016-03-23 23:58:15 UTC-4 [Status] Device connected
2016-03-23 23:58:15 UTC-4 [Device] binary: 63 75 72 72 65 6e 74 20 70 6f 69 6e 74 65 72 20 69 73 3a 20 20 74 65 73 74 73 00
2016-03-23 23:58:15 UTC-4 [Device] ERROR: the index ‘spiFlash’ does not exist
2016-03-23 23:58:15 UTC-4 [Device] ERROR: at main:245
2016-03-23 23:58:19 UTC-4 [Status] Agent restarted: reload.
2016-03-23 23:58:21 UTC-4 [Status] Downloading new code; 21.39% program storage used
2016-03-23 23:58:24 UTC-4 [Device] binary: 63 75 72 72 65 6e 74 20 70 6f 69 6e 74 65 72 20 69 73 3a 20 20 74 65 73 74 73 00
2016-03-23 23:58:24 UTC-4 [Device] ERROR: io error in blob.readn(format)
2016-03-23 23:58:24 UTC-4 [Device] ERROR: at main:247
2016-03-23 23:58:30 UTC-4 [Status] Device connected

‘io error in blob.readn(format)’ is, IIRC, a sign that you’re attempting to read past the end of the blob.

Hi smittytone,

I sorry to insist, new to ElectricIMp and Squierl.

I am using the following:
#require “mb85rc.class.nut:1.0.0”
#require “framstore.class.nut:1.0.0”
#require “Serializer.class.nut:1.0.0”

The line of code producing the error message is:
local length = dataBlob.readn('w');

The error is the following:
2016-03-24 18:59:22 UTC-4 [Device] ERROR: io error in blob.readn(format)
2016-03-24 18:59:22 UTC-4 [Device] ERROR: at main:241

I am trying to adapt the sample code in
Code Libraries: Serializer 1.0.0

This is my adapted code (part of it).
`
local data = {
“foo” : “bar”,
“timestamps” : [1436983175, 1436984975, 1436986775, 1436988575, 1436990375],
“readings” : [32.5, 33.6, 32.8, 32.9, 32.5],
“otherData” : {
“state” : true,
“test” : “test”
}
}
server.log(Serializer.sizeof(data));
local serializedData = Serializer.serialize(data);

// /local err = fram.write(0x0000, serializedData, fram.SPIFLASH_POSTVERIFY);
local err = fram.write(0x0000, serializedData, fram.SPIFLASH_POSTVERIFY);

// Read the header information
local dataBlob = fram.read(0x000, 3);
// Get the length from the first two bytes
local length = dataBlob.readn(‘w’);

// Move to the end of the blob
dataBlob.seek(0, ‘e’);

// Read the length of the data starting at the end of the header
fram.readintoblob(0x03, dataBlob, length);

`

Try putting dataBlob.seek(0, 'b'); ahead of line 241 to ensure the pointer is at the start. Does this fix the problem?

Hi smittytone,

That did the trick. Thanks!

But now I have another question. if the data table grows to the total size of the FRAM when it is deserialized would it fit in the imp’s memory?