Slowly it's getting better, the code almost does what I want it to do. A *.BIN file is written, and when renaming that *.BIN file to *.TXT file or *.CSV file on my windows machine then the result looks like this:
Time: 21713,A-B: -0.071,A: 0.393,B: 0.464,Additional_Data...
Time: 21733,A-B: 0.026,A: 0.416,B: 0.390,Additional_Data...
Time: 21753,A-B: -0.026,A: 0.412,B:NUL 0.438,Additional_Data...
Time: 21773,A-B: 0.113,A: 0.516,B: 0.403,Additional_Data...
Time: 21793,A-B: 0.016,A: 0.354,B: 0.338,Additional_Data...
Time: 21813,A-B: 0.071,A: 0.435,B: 0.364,Additional_Data...
Time: 21833,A-B: 0.122,A: 0.274,B: 0.151,Additional_Data...
Time: 21853,A-B: -0.084,A: 0.116,B: 0.200,Additional_Data...
Time: 21873,A-B: -0.016,A: 0.293,B: 0.309,Additional_Data...
Time: 21893,A-B: 0.216,A: 0.538,B: 0.322,Additional_Data...
Time: 21913,A-B: -0.061,A: 0.416,B: 0.477,Additional_DaNULta...
Time: 21933,A-B: 0.229,A: 0.480,B: 0.251,Additional_Data...
Time: 21953,A-B: -0.039,A: 0.361,B: 0.400,Additional_Data...
Time: 21973,A-B: -0.132,A: 0.416,B: 0.548,Additional_Data...
There is somewhere a NUL character added every 512 bytes, but I couldn't figure out how to avoid that. Except of that the logged data looks ok, and apparently it's quite fast. The code that I logged the data above is as follows:
dataString += String("Time: ");
dataString += String(int(time));
dataString += String("\t");
dataString += String("A-B: ");
dataString += String(ADCvalue_0-ADCvalue_1,3);
dataString += String("\t");
dataString += String("A: ");
dataString += String(ADCvalue_0,3);
dataString += String("\t");
dataString += String("B: ");
dataString += String(ADCvalue_1,3);
dataString += String("\t");
dataString += String("Additional_Data... ");
dataString += String("\n"); //Create a new line on the SD card
int length = dataString.length();
if (length > 512)
{
noInterrupts();
SDbufferString = dataString.substring(0,511); //Create a string with 512 bytes for writing on the SD card.
dataString = dataString.substring(511,length); //Remove the 512 bytes for the SD card from the main string.
char charBuf[512];
SDbufferString.toCharArray(charBuf,512); //Convert String to Char Array
memcpy(pCache, &charBuf, 512); //Write binary data to cache
if (!sd.card()->writeData((uint8_t*)pCache)) ;
bn++; //increment block number
count = 0;
memset(pCache, '0', 512);
interrupts();
}
The binaryToCsv conversion code does not work, there is only garbage to see later on the computer. But as already mentioned above, when renaming the BIN file to TXT on my computer then the result is as above.
The binaryToCsv code I used is the following:
void binaryToCsv() {
uint8_t lastPct = 0;
int buf[512];
uint32_t t0 = millis();
uint32_t syncCluster = 0;
SdFile csvFile;
char csvName[13];
BufferedWriter bw;
if (!myFile.isOpen()) {
Serial.println(F("No current binary file"));
return;
}
myFile.rewind();
//if (!binFile.read(&buf , 512) == 512) error("Read metadata failed");
// create a new csvFile
strcpy(csvName, binName);
strcpy_P(&csvName[BASE_NAME_SIZE + 3], PSTR(".CSV"));
if (!csvFile.open(csvName, O_WRITE | O_CREAT | O_TRUNC)) {
error("open csvFile failed");
}
Serial.println();
Serial.print(F("Writing: "));
Serial.print(csvName);
Serial.println(F(" - type any character to stop"));
bw.init(&csvFile);
bw.putStr_P(PSTR("Time,")); //Write header at top of file
bw.putStr_P(PSTR("A-B,"));
bw.putStr_P(PSTR("A,"));
bw.putStr_P(PSTR("B"));
bw.putCRLF();
uint32_t tPct = millis();
while (!Serial.available() && myFile.read(&buf, 512) == 512) {
uint16_t i;
//read 512 bytes of data here i.e. 512 samples
for (i = 0; i < 512; i++) {
bw.putChar(buf[i]);
}
//bw.putCRLF();
if (csvFile.curCluster() != syncCluster) {
bw.writeBuf();
csvFile.sync();
syncCluster = csvFile.curCluster();
}
//looks like code to print out % done
if ((millis() - tPct) > 1000) {
uint8_t pct = myFile.curPosition()/(myFile.fileSize()/100);
if (pct != lastPct) {
tPct = millis();
lastPct = pct;
Serial.print(pct, DEC);
Serial.println('%');
}
}
if (Serial.available()) break;
}
bw.writeBuf();
csvFile.close();
Serial.print(F("Done: "));
Serial.print(0.001*(millis() - t0));
Serial.println(F(" Seconds"));
}