ArdSketch icon indicating copy to clipboard operation
ArdSketch copied to clipboard

send code from .txt

Open PabloCaSan opened this issue 4 years ago • 0 comments

I was trying to send some gcode lines from a .txt file stored in my SD, but when running your program, it only sent the first line. This is what happens when commenting the Serial.print(getSerial(1)); in void sendGcode()

Captura de Pantalla 2020-06-25 a la(s) 12 16 32

I was using a Teensy 3.6 to send the gcode to an Arduino UNO wired to a CNC Shield, so I adapted the code to work with this. These are the variables and library I used:

#include <SdFat.h>
SdFatSdio SD;
#define SD_CS_PIN SS
File myFile;
boolean restart = true;

I also changed the baud ratio to 9600 to match the one running in the Arduino. And changed the while(!SD.begin(53)) to while(!SD.begin()), because otherwise I had an error due to the Teensy and the library I was using, if you use an Arduino, you may not need to change this.

Finally I solve this issue by editing the void sendGcode() like this:

void sendGcode(){
  
  //READING GCODE FILE AND SEND ON SERIAL PORT TO GRBL

  //START GCODE SENDING PROTOCOL ON SERIAL 1

  String line = "";
  
    Serial1.print("\r\n\r\n");      //Wake up grbl
    delay(2);
    emptySerialBuf(1);
    if(myFile){                                                                      
      while(myFile.available()){    //until the file's end
          String line = myFile.readStringUntil('\r');   //read line in gcode file  <----------- What I changed
          Serial.print(line);   //send to serials
          Serial1.print(line);
          //Serial.print(getSerial(1));  //print grbl return on serial
    }
  }
  else
    fileError();
    
  Serial.println("Finish!!\n");
  myFile.close();
  delay(2000);
}

And as you can see, you don't need String readLine(File f) anymore.

This is the result:

Captura de Pantalla 2020-06-25 a la(s) 12 20 55

Thanks for sharing your code, it helped me a lot, I share this in case anyone has the same issue as I did. Greetings! :)

PabloCaSan avatar Jun 25 '20 17:06 PabloCaSan