Arduino Forum

Products => Arduino 101 => Topic started by: macallmcqueen on Jun 30, 2016, 09:45 pm

Title: How do I use read and write in the SerialFlash library?
Post by: macallmcqueen on Jun 30, 2016, 09:45 pm
Hello,

To get started with the read() and write() functions of the SerialFlash library, I'm trying to write a simple program that writes a char to the 2MB flash chip, reads it back from the chip into a buffer, and prints out the contents of that buffer to confirm that it does, in fact, contain the char that I originally wrote out.

So far I haven't had any success with this, and none of the provided example programs demonstrates how to print out a specific value that has been read from memory. I expect that I'm using the wrong data types and/or sizes for the buffers, calling read and write with incorrect numbers as their second input, or incorrectly casting the values I read in from the chip when I attempt to print them out. I've tried adjusting my methods in a variety of ways, but I still haven't been able to retrieve any coherent data that I've written to the chip.

If anyone who understands how to use this library would be willing to show me an example of this basic task (writing a value to the chip and then reading it back in so that I can use or print it), I would be extremely grateful.

Thanks!
Title: Re: How do I use read and write in the SerialFlash library?
Post by: macallmcqueen on Jul 01, 2016, 05:18 pm
Hi friends, I figured out what I was doing wrong. I'll explain it below in case anyone else has trouble getting started with SerialFlash.

----------

When you write() data to the chip, the library keeps track of where you are in the file. Your theoretical cursor will be at the end of whatever data you wrote. If you immediately call read(), your program will attempt to read from the part of memory located right after where you just wrote, so it won't actually read in your data. To fix this, you've gotta use the seek() function to move to the current position of your cursor minus the size of the data you wrote. Example:

     int SIZE = 1; // the size of the data I'm trying to write, which is in this case a char

     char contents[1] = {98}; // a buffer containing the character 'b'
     
     char buffy[1]; // another buffer, to hold data read in from the chip

     file.write(contents, SIZE); // write the contents of the buffer containing 'b' to the chip

     file.seek(file.position() - SIZE); // move the cursor backward!

     file.read(buffy, SIZE); // read 'b' into buffy

     Serial.println((char)*buffy); // this successfully prints out 'b'
Title: Re: How do I use read and write in the SerialFlash library?
Post by: 03sarath on Oct 19, 2019, 07:22 pm
Hey macallmcqueen,


Thanks for your effort,That's really a great job.

If you don't can you please share the sample code that you created. :)