Odd characters when reading from SD card

Hi there. I'm a relative newbie to both C/C++ programming and the Arduino as well. I am having a problem with reading from the SD card that I hope you all can help me with. I have tried searching for the solution here on the Arduino forum, and on the internet in general, to no avail.

I have one Arduino that is running a program called GRBL that controls a CNC machine. I am using another Arduino with a TFT display and SD card slot to provide an interface for the Arduino running GRBL. The interface program is getting pretty involved, but all is well except that when reading a text file from the SD there are odd characters that are read at the beginning of the file. These odd characters, when sent to the other Arduino result in an error. Since the code is rather involved, I decided to write a short sketch that just reads a text file and echoes it to the serial port. The only other thing the test code does is ignore '\r' characters. I am getting the same results as my main program, so I think my error lies in this code somewhere. For example, in my test text file, the first line is:

G00G21G90X-0.5375Y3.5375Z3.3598F4000

but what is being read is:

G00G21G90X-0.5375Y3.5375Z3.3598F4000

Those first few characters,  are the problem. I have checked the text file with a text editor and those characters are not in there.

I added a hack workaround to my interface that simply ignores the first three characters and then everything works great, but I want to understand what is going on here. Below is the test code I wrote that generated the exact same problem as my main program. I just can't figure out where these characters are coming from.

Any help would be much appreciated.

#include <SPI.h>
#include <SD.h>

File GCodeFile;

//============================================
void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  while (!Serial) {
    ; // wait for serial port to connect. 
  }  
}

//============================================
void loop() {
  if (!SD.begin(10,11,12,13)) {
    return;
  }
  GCodeFile = SD.open("1K.NC");
  if (GCodeFile) {
    // SD opened
    while (GCodeFile.available()){
      char TMP = GCodeFile.read();
      if (TMP != '\r'){
        Serial.print(TMP);
      }
    }
  }
}

How did you create the file? Even the simplest text editor, Notepad, can add a few control chars at the beginning to indicate what character encoding to use for the file.

The UTF-8 representation of the BOM is the byte sequence 0xEF,0xBB,0xBF. A text editor or web browser misinterpreting the text as ISO-8859-1 or CP1252 will display the characters  for this.

Thank you. That answers what those characters are, but your reply indicates that they are being misinterpreted. How do I keep my program from misinterpreting in this way?

  1. Save the file in a way that doesn't add the characters: "plain text" or whatever.
  2. Look at the values of the characters, if greater than '~' or less than ' ' then ignore it.