I'll send the whole sketch with options indicated for commenting out different approaches, here in this post, but if that's too confusing, let me know and I'll send individual sketches demoing each result.
The problem seems to be that while I can use the header structure info to get & validate the data part by using headerBuf.dataBlockLen from the header,
Code: Select all
Serial.write(dataBuffer, headerBuf.dataBlockLen); // WORKS
The way that seemed most obvious to me was:
Code: Select all
Serial.write(headerBuf.fileName);
the filename(albeit with padded zeroes, I try to remove them later in another effort), a character 'd' that's the dummy I sent with the file to simulate checksum, AND the whole text test file!! Which shouldn't be being sent by this command, I assume.
(There is always the possibility that my terminal program screws up echo displays, but I don't think that's the case here, though I'll try to bear that in mind.)
Here's the sketch, which has to have various sections commented out to see the program flow for each different attempt to show the filename as an echo.
Code: Select all
// now trying to do receiving MP3 files by arranging to know how many bytes of data to read
// need to completely redo EuTerm,
// to configure and send header and mp3 data;
// THEN make this sketch receive & process header & data correctly
// header type for ALL chunks:
typedef struct Header_t
{
uint32_t syncPattern; //4 bytes with a fixed value, e.g. 0xa5a5a5a5, marks the beginning of a header
uint8_t dataBlockLen; //number of characters in data block
char fileName[13]; // the filename all the portions of MP3 data should be put into on SD card
uint8_t checksum; //checksum of data block ; changed this from uint16 to 8 temporarily
};
Header_t headerBuf; // this is a variable (or "structure"?) that end up containing each chunks header
// after it's sent by EuTerm and read here
int led = 13; // used in testing received serial transmission
byte dataBuffer[100]; // at this time, I may only be sending 50 bytes at a time of data + filename
void setup(){
Serial.begin(9600);
Serial.flush(); // just seems like a good idea???
pinMode(led, OUTPUT);
digitalWrite(led, LOW); // turn it off
}
void loop() {
// the logic here should be something like:
// EuTerm: get list of files to send, find first etc, get chunk of file to send, organize header info,
// send header, send data, wait, see if data ok, do another chunk or another file.
// Uno sketch: read header & data, use header info to read data, if ok send to SD card, tell EuTerm ok or not .
if (Serial.available() > 0){
Serial.readBytes((char*)&headerBuf, sizeof(Header_t)); // this presumably 'stops' the loop, reads until all of header in headerBuf
if (0xa5a5a5a5 == headerBuf.syncPattern) {//make sure sync pattern matches
digitalWrite(led, HIGH); // shows if sync pattern sent correctly
Serial.readBytes((char*)&dataBuffer, headerBuf.dataBlockLen ); // reads all data?
// THIS SENDS THE *DATA* BACK TO EuTerm FOR *VALIDATION OF RECEPTION*:
// THIS BY ITSELF *WORKS PERFECTLY* AS LONG AS HEADER HAS 13 SPACES RESERVED FOR FILENAME AND
// EuTerm PADS END OF FILE NAME WITH ZEROS TO MAKE IT HAVE 13 CHARACTERS.
// Serial.write(dataBuffer, headerBuf.dataBlockLen); // WORKS
//====================================================================================
// A NUMBER OF DIFFERENT EFFORTS TO SEE THE *FILENAME*;
// COMMENT OUT ALL BUT ONE AT A TIME, AS INDICATED:
//- - - - - - - - - - - - - - - - - - - - - -
// FIRST WAY:
// Serial.write(headerBuf.fileName); // FIRST WAY ECHOES TOO MUCH!!
// an echo: t1.txt0000000dFirst file is a short test file.
// supposed to be just the text file NAME, but shows also data (contents of text file)
// as well as the character 'd' which is the dummy checksum
//----------------------------------------------------------------------------------
// FOLLOWING FOR IS FOR BOTH 2nd AND 3rd WAY, COMMENT OUT FOR FIRST, RETAIN FOR BOTH 2nd & 3rd:
for (int n = 0; n < 12 ; n++ ) // KEEP THIS FOR BOTH 2ND AND THIRD WAY
{ // AND THIS CURLY BRACE, TOO, and curly brace below 3rd way
// SECOND WAY:
// Serial.write(headerBuf.fileName[n]); // SECOND WAY WORKS *BY ITSELF*,
// WITH THE TRAILING ZEROES IN THE FILENAME
// THIRD WAY:
if (headerBuf.fileName[n] = '.' ){ // BUT THIS, DOESN'T!
Serial.write(headerBuf.fileName[n]); // I'M TRYING TO BEGIN TO CUT OUT TRAILING ZEROES
break; // IT ECHOES THE '.',
}
Serial.write(headerBuf.fileName[n]); // BUT HAS *NOT* ECHOED LEADING CHARACTERS PREVIOUS TO THE '.' AS I EXPECTED (I think it should go though this if the 'if' condition for '.' not met, and then break out of the if when '.' IS found.)
} // KEEP THIS CURLY BRACE FOR BOTH 2nd & 3rd WAYS, BUT NOT FIRST
} // sync pattern in header matches expected sync value
}
}
(Note that the reason for all the commenting out is that I only send one echo back to my terminal program at a time, so it will show the echo simply.)