Pages: [1]   Go Down
Author Topic: Writing binary data from sd card to the Serial port.  (Read 888 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

* SD read _ write (1.15 KB - downloaded 5 times.)
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 551
Posts: 46227
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 551
Posts: 46227
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
int totalBytes = 0; // Start with zip

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

It ain't rocket surgery.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 551
Posts: 46227
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 18
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 551
Posts: 46227
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Thats the function i am looking for, if you know you can tell me.
You are already using it...
Code:
while (myFile.available())
Logged

Pages: [1]   Go Up
Jump to: