SDFat library – how to get work parseInt() ?

stm32_discoverer
Fri Feb 17, 2017 9:34 pm
Hi, i’m trying to get an integer from an array from text file on my SD card? But in old library SD.h it was easy like
myFile = SD.open("test.txt");
if (myFile) {
while (myFile.available() && i<len) {
x = myFile.parseInt();
Serial.println(x);
}
myFile.close();

victor_pv
Fri Feb 17, 2017 9:49 pm
I think this thread may be related:
https://forum.arduino.cc/index.php?topic=326573.0

The issue is not specific to the stm core, but rather the way the sdfat library works.


stm32_discoverer
Fri Feb 17, 2017 10:10 pm
yeah, I already saw this link, tried it, didn’t help

victor_pv
Fri Feb 17, 2017 11:09 pm
stm32_discoverer wrote:yeah, I already saw this link, tried it, didn’t help

stm32_discoverer
Sat Feb 18, 2017 7:37 am
I’m using SDFat-beta from greiman

stm32_discoverer
Sat Feb 18, 2017 7:43 am
But I don’t see #define ARDUINO_FILE_USES_STREAM 1

Pito
Sat Feb 18, 2017 9:49 am
#include <SPI.h>
#include "SdFat.h"
SdFat SD;
const uint8_t csPin = SS;
File file;
..
file = SD.open("stream.txt", O_RDWR|O_CREAT|O_TRUNC);
if (!file) {
Serial.println(F("open error"));
return;
}
;
..
// Rewind the file and read the number with parseInt().
file.seek(0);
int i = file.parseInt();
Serial.println(i);
file.close();

stm32_discoverer
Sat Feb 18, 2017 12:42 pm
weird, but this sketch does not read my sd card

stm32_discoverer
Sat Feb 18, 2017 12:45 pm
That’s my code
#include <SPI.h>
#include "SdFat.h"
SdFat sd1(1);
// SdFatEX sd1(1);
const uint8_t SD1_CS = PA4; // chip select for sd1

//SdFat sd2(2);
//// SdFatEX sd2(2);
//const uint8_t SD2_CS = PB12; // chip select for sd2

#define errorExit(msg) errorHalt(F(msg))
#define initError(msg) initErrorHalt(F(msg))
unsigned long duration;
int my_array[1000];
int num;
int c;

void setup() {
Serial.begin(115200);
delay(10000);

// initialize the first card
if (!sd1.begin(SD1_CS, SD_SCK_MHZ(18))) {
sd1.initError("sd1:");
}

// initialize the second card
// if (!sd2.begin(SD2_CS, SD_SCK_MHZ(18))) {
// sd2.initError("sd2:");
// }
Serial.println(F("------sd1 root-------"));
sd1.ls();
// set the current working directory for open() to sd2
//sd1.chvol();

File file1;
if (!file1.open("data.txt", O_RDWR | O_CREAT | O_TRUNC)) {
sd1.errorExit("file1");
}
for (int x=0;x<1000;x++){
parseUInt();
Serial.println(num);
}
file1.close();
Serial.println(F("Done"));
}
void loop(){}

int parseUInt () {
int num=-1;
while (true) {
int c = file1.read();
if (c>47 && c<58) {
if (num<0) num=0;
num=num*10+c-48;
} else if (num>-1 || c==-1) return num;
}
}


stm32_discoverer
Sat Feb 18, 2017 12:50 pm
even if I replace the function – parseUInt () to int c = file1.parseInt(); – code does not work

Pito
Sat Feb 18, 2017 1:01 pm
What does it mean “code does not work” ??
Do not try to mess with complex sketches, instead try to go step-by-step.
Try the original example (my above code snippet is from) from the SdFat until it works perfectly. And after then you may play with the code.

https://github.com/greiman/SdFat/blob/m … rseInt.ino

Also mind the C language requires some rules are followed. The file1 must be declared global, otherwise it will not be visible inside a function.
See my snippet above.


victor_pv
Sat Feb 18, 2017 1:48 pm
stm32_discoverer wrote:But I don’t see #define ARDUINO_FILE_USES_STREAM 1

Leave a Reply

Your email address will not be published. Required fields are marked *