I have written some config data to an SD Card and the format is flexible i.e. I can write it any format.
The present format is : "1000.0001.B01.A"
I was thinking the "." can be used as a separator.
Can anyone tell me why is the data being printed twice?
I believe that there is more than one line written in conf.txt and the while (datFile.available())is reading everything. That can happen if you open the serial monitor to confirm the creation of a file. You can read the card in the computer to confirm.
Your code is close. Here is a slight modification which only reads the first 15 bytes, regardless of what is in the file.
/* Read from SD Card and create variables. */
#include <SdFat.h> // SD Card library
#include <SPI.h>
char a[5];
char b[5];
char c[4];
char d[2];
char buffer[20];
SdFat SD;
File datFile;
char fileName[] = "conf.txt";
// conf.txt 1000.0001.B01.A
void setup()
{
pinMode(10, OUTPUT);
// Start Serial
Serial.begin(9600);
while (!Serial) {
; // wait till port connects
}
Serial.println(F("Serial started.")); // test print
// SD Card setup
Serial.print(F("Initializing SD card..."));
// SD Card setup result
if (!SD.begin(4, SPI_HALF_SPEED)) {
SD.initErrorHalt();
}
Serial.println(F(" done."));
Serial.print(F("Reading config file... "));
// Open config file:
datFile = SD.open("conf.txt");
if (datFile)
{
// Read file contents:
datFile.readBytes(buffer, 15);
Serial.println(buffer); //confirm what was read
// Close config file:
datFile.close();
}
else
{
Serial.println(F("Error opening config file."));
}
for (int i = 0; i < 15; i++) {
Serial.print(i);
Serial.print(" : ");
Serial.println(buffer[i]);
}
char *a = buffer + 0;
char *b = buffer + 5;
char *c = buffer + 10;
char *d = buffer + 14;
Serial.println(a);
Serial.println(b);
Serial.println(c);
Serial.println(d);
}
void loop()
{
}
PaulS:
Is that even reasonable? How big is the file?
Tough. You MUST have a hard limit - the size of the array to store data in.
I mean I don't want a fixed number, some files are 8 char the biggest is about 30 char.
Ok, so I guess I can use sizeof(array), but something "looks" wrong
if (datFile) {
// Read file contents:
while (datFile.available()) {
Serial.readBytes(buffer, sizeof(buffer));
}
}
From what I have read about "sizeof" command. It is only looked at when the sketch is compiled. That is a static number from that point on.
I am working on something simular to this. I am reading from a serial port but the data from the bar codes I will be reading are different in length. Wanting to keep my sketch as small as possible. I have written the code with more loops than needed. I am now looking for a carriage return to "break" out of the "while" loop and continue on with the sketch when the buffer is empty and a CR is detected. If you create your file with say a NULL at the end, use that to break out of your loop.
Hope I was not barking up the wrong tree. And this was of help.
Paul1958:
From what I have read about "sizeof" command. It is only looked at when the sketch is compiled. That is a static number from that point on.
I am working on something simular to this. I am reading from a serial port but the data from the bar codes I will be reading are different in length. Wanting to keep my sketch as small as possible. I have written the code with more loops than needed. I am now looking for a carriage return to "break" out of the "while" loop and continue on with the sketch when the buffer is empty and a CR is detected. If you create your file with say a NULL at the end, use that to break out of your loop.
Hope I was not barking up the wrong tree. And this was of help.
Paul
sizeof(array) is set as soon as you declare the array, and yes sizeof() won't change.
I will be declaring a separate array for each file, so I will use the relevant sizeof() to read from the file.
I know the theory for what I want to do, but sometimes it is like pulling teeth trying to get straightforward help here.
I guess I am back to wasting another few hours searching for the correct syntax myself - kinda defeats the purpose of having a forum for help.
PaulS:
Nonsense. What is difficult is finding someone to tell you how to do something that can't be done.
Syntax for what?
I believe that what I am trying to do can be done.
It is probably just your interpretation of what I am trying to do that makes you believe it can't be done.
aisc:
I believe that what I am trying to do can be done.
It is probably just your interpretation of what I am trying to do that makes you believe it can't be done.
I would recommend looking at what Ladyada does in her GPS library for double-serial buffering and how she parses.... very simple to understand. Adafruit GPS.cpp
I hacked up to fit my need, but the INO shows how I utilized the concept in this project.
Thanks for the links Ray.
I did have a look at the GPS sketch - it is way beyond the simple objective I was trying to achieve.
Anyway after some reading and a lot of mucking about I had already figured out a solution and now have a function that reads a file's entire contents from an SD card and spits it out as a variable.