SD card read() returns -1 before end of file

UPDATE I've found the issue. The ethernet board I'm using for the SD card reader is not receiving enough power, because I'm also powering an LED strip.

I'm trying to build a project and I need to load data from an SD card but reading from the SD card seems to fail part way through the file.

It fails after reading 54,182 bytes, I was thinking maybe it's because of some file buffer or something.

I am using an Arduino Uno with the Ethernet board (temporarily, just for its SD card slot)

Here's a simplified version of my program (where it's breaking)

...
while (csvFile.available()) {
char b = csvFile.read(); // read 1 byte
Serial.print(b); // for debugging
delay(1); // for debugging

if ((int)b < 0) {
Serial.println("found negative value: " + String((int)b)); // for debugging
csvFile.close();
break;
[tt]    }[/tt]

[tt]  ...
  }

[/tt]

[tt]...

[/tt]
Do I need to do anything to make sure the stream has bytes available to be read? I will be reading the file as fast as I can eventually, but it fails even going slow (waiting 1 ms after each byte read)

I should mention, I am using SD.h version 1.2.4

the file is opened like this:
File entry = dir.openNextFile(); (where dir is a directory)

if the file has the right name I read it, otherwise I close it and continue to look for the file similar to the example code here:

You likely have a single byte somewhere in the file that has a negative value. You should be checking specifically for a return of -1, not just a negative value.
Paul

It depends on the content of your file. The read method returns an int, not a char or byte. You probably have the value 0xFF in the file and because you use a char variable, it's seen as -1.

I know what's in the file and I double checked that it's not -1 (0xFF)

this is the file: (ellipses added to show pattern and fit better in this display)
...
213,213,213,213,213,213,213,213,213,213,213,213,213,213,213,213,213,213,...
228,228,228,228,228,228,228,228,228,228,228,228,228,228,228,228,228,228,...
228,228,228,228,228,228,228,228,228,228,228,228,228,228,228,228,228,228,...
244,244,244,244,244,244,244,244,244,244,244,244,244,244,244,244,244,244,...
...

This is what my program prints:
...
213,213,213,213,213,213,213,213,213,213,213,213,213,213,213,213,213,213,...
228,228,228,228,228,228,228,228,228,228,228,228,228,228,228,228,228,228,...
244,244,244,244,244,244,244,244,244,244,244,244,244,244,244,244,244,244,244,244,244,244,244,2⸮found negative value: -1

I've also tried reformatting the SD card and copying the same file over. The same issue occurs at the same spot. I can try moving that line to another spot in the file and see what it does.

when I get a chance to try it again.

Ok, update: I've modified the file, moving the trouble section to the top of the file to see if it's the file that's wrong and it seems that it reads that section fine at the beginning and actually made it through 66451 bytes before it broke again (before the end of file)

so it doesn't seem to be the file

I've solved my issue. I believe the Ethernet board is not receiving enough power causing the SD reads to fail.

This is caused by the LED strip I am powering through the 5v pin and I'm only on usb power. I just tried it and it still fails with additional power (50 leds).

I hopefully will have a better time with the smaller board I have coming in the mail for reading SD cards, and providing additional power in other ways (powering the LEDs separately)

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.