Salve a tutti,
dopo aver perso ore ed ore a testare codice e a ricercare topics sul Forum, ovviamente senza successo, chiedo aiuto e chiarimenti.
Si tratta di un datalogger classico di dati GPS a 1 Hz. Il problema è che "ogni tanto" salta la scrittura di qualche carattere sulla memoria SD. Ecco un estratto del file. In nero la scrittura corretta, in blu quella memorizzata male.
123114.000,4530.7600,N,00903.6611,E,6.06,270614,g2,6,581,3470
123115.000,4530.7595,N,00903.6588,E,4.58,270614,g2,6,581,3470
123116.000,4530.7594,N,00903.6574,E,2.28,270614,g2,6,581,3470
123117.000,4530.7593,N,00903.6566,E,0.00,270614,g2,6,581,3470
123118.000,4530.7593,N,00903.6566,E,0.00,270614,g2,6,581,3470
3119.000,4530.7592,N,00903.6566,E,0.00,270614,g2,6,581,3470
123120.000,4530.7592,N,00903.6566,E,0.00,270614,g2,6,581,3470
123121.000,4530.7590,N,00903.6533,E,7.00,270614,g2,6,581,3470
123122.000,4530.7586,N,00903.6500,E,9.82,270614,g2,6,581,3470
123123.000,4530.7578,N,00903.6459,E,10.97,270614,g2,6,581,3470
123124.000,4530.7566,N,00903.6412,E,12.91,270614,g2,6,581,3470
...
23228.000,4530.6292,N,00902.5816,E,60.95,270614,g2,6,581,3470
23229.000,4530.6267,N,00902.5579,E,59.82,270614,g2,6,581,3470
3230.000,4530.6241,N,00902.5347,E,58.73,270614,g2,6,581,3470
3231.000,4530.6216,N,00902.5121,E,57.60,270614,g2,6,581,3470
3232.000,4530.6193,N,00902.4899,E,56.48,270614,g2,6,581,3470
3233.000,4530.6169,N,00902.4681,E,55.40,270614,g2,6,581,3470
3234.000,4530.6147,N,00902.4469,E,54.35,270614,g2,6,581,3470
3235.000,4530.6124,N,00902.4260,E,53.35,270614,g2,6,581,3470
...
Se reindirizzo l'output al monitor, non si verifica alcun problema.
Ho pensato che Arduino UNO avesse una velocità non adeguata e quindi ho provato ad utilizzare anche ChipKIT UNO 32, ma il comportamento è il medesimo.
Arduino UNO o CHIPKIT UNO 32
Shield SD con memoria 2GB
GPS seriale 9600 su Seriale1
Il codice seguente non produce l'output indicato sopra, ma di fatto genera il medesimo problema.
#include <SD.h>
#include <PString.h>
// On the Ethernet Shield, CS is pin 4. Note that even if it's not
// used as the CS pin, the hardware CS pin (10 on most Arduino boards,
// 53 on the Mega) must be left as an output or the SD library
// functions will not work.
// Default SD chip select for Uno and Mega type devices
const int chipSelect_SD_default = 10; // Change 10 to 53 for a Mega
// chipSelect_SD can be changed if you do not use default CS pin
const int chipSelect_SD = chipSelect_SD_default;
char buffer [200];
PString dataString (buffer,sizeof(buffer));
File dataFile;
void setup()
{
Serial.begin(9600);
Serial.print("Initializing SD card...");
Serial1.begin(9600);
// Make sure the default chip select pin is set to so that
// shields that have a device that use the default CS pin
// that are connected to the SPI bus do not hold drive bus
pinMode(chipSelect_SD_default, OUTPUT);
digitalWrite(chipSelect_SD_default, HIGH);
pinMode(chipSelect_SD, OUTPUT);
digitalWrite(chipSelect_SD, HIGH);
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect_SD)) {
Serial.println("Card failed, or not present");
// don't do anything more:
return;
}
Serial.println("card initialized.");
dataFile = SD.open("datalog.txt", FILE_WRITE);
if (!dataFile) {
Serial.println("error opening datalog.txt");
return;
}
dataFile.close();
}
void loop()
{
// read from port 1, send to port 0:
if (Serial1.available()) {
int inByte = Serial1.read();
Serial.print(char(inByte)); // echo su monitor dei caratteri che arrivano dalla seriale
if (inByte!=10 && inByte!=13) { //non memorizzare in dataString anche i caratteri di controllo
dataString.print(char(inByte));
} //if inbyte != 10, 13
else // if inByte = 10, 13
{
if (inByte == 10) {
dataFile = SD.open("datalog.txt", FILE_WRITE);
if (!dataFile) {
Serial.println("... File error");
return;
}
else
{
dataString.println(); // aggiungo alla stringa <CR> e <LF>
dataFile.print(dataString); // scrivo la stringa su SD
Serial.println(" SD writing:... ");
Serial.print(dataString); // creo l'echo a monitor
dataFile.close(); // assicurati che tutti i bytes siano stati scritti
dataString.begin(); // resetta la stringa
Serial.println(" ... done!");
}
} //if inByte = 10
}
} // if serial available
}
Qualche suggerimento?
Grazie!
Marco