Go Down

Topic: Writing binary data from sd card to the Serial port. (Read 1 time) previous topic - next topic

hdmafukidze

I have a csv file stored on an sd card (test.csv). I need to send the file to the serial port as binary and view the number of bytes written.

sample code:
//open the file for reading

myFile = SD.open("test.csv);
if (myFile) {

// read from the file

    while (myFile.available()) {
          Serial.write(myFile.read());
          }
// close the file
myFile.close();
}

according the the reference available on the website, it says write() will return the number of bytes written, though reading that number is optional .

My problem is, how do i get the number of bytes written because my next application entirely depends on this number.

PaulS

Quote
according the the reference available on the website, it says write() will return the number of bytes written, though reading that number is optional .

My problem is, how do i get the number of bytes written because my next application entirely depends on this number.

"My problem is I need to know the number of bytes written." Then, discarding the output of Serial.write() is NOT optional, is it?

PaulS

Code: [Select]
int totalBytes = 0; // Start with zip

totalBytes += Serial.write(someStuff); // Add the number written this time.


It ain't rocket surgery.

PaulS

I don't see why you have to count the characters written to the serial port, when you know how many are in the file.

You should be storing the myFile.read() value in a byte, and then Serial.write()ing that byte, anyway, to assure that only valid data is sent.

Jaakko_Fagerlund


thanks a lot i have tried that and it works for stings and integers (below 255) but it does not work for a scv file stored on an sd card.

Then use unsigned long or something else than an int.

Otherwise just use the SD library function to get the file size.

Go Up