Hello everyone
I loaded the GRBL library on my Arduino Uno board. I connected my motors with CNC shield. When I send any gcode the engines are moving. I have 1 Arduino Mega board. I have connected SDCard to this card. And I opened a text file in it and wrote a GCode. I connected the Rx-Tx-GNDs of the 2 boards to each other. I am reading gcode from SDCard properly. But when I send the gcode I read to Arduino Una, I get "error: Bad number format" error on the serial terminal. Baudrate is 115200. I would be very grateful if you could help.
Can't help without seeing your code. Specifically, how you are reading the serial input, whatever processing is done to the input value, and how you are sending it to the serial port. When you say you connected Rx-Tx-Gnd of the 2 boards to each other, do you mean literally that you connected Rx of one board to Rx of the other? Maybe you crossed them as they should be (One Rx goes to the other Tx) but from your text question, without any wiring diagram, code, etc. we would only be guessing as to what is going wrong.
Hello,
I attached the code I wrote below. The reason why I closed 3 lines of code with // is to check whether it sends the data it reads on the SDCard. Although I read the lines I closed on the serial port of the other card, I cannot read the information I read from the SDCard on the serial port of the other card. I made the connections to be Rx-Tx, Tx-Rx. I don't foresee a problem with the hardware.
#include <SPI.h>
#include <SD.h>
File myFile;
void setup() {
Serial.begin(115200);
//Serial.print("Initializing SD card...\n");
if (!SD.begin(10)) {
// Serial.println("initialization failed!...\n");
return;
}
// Serial.println("initialization done...\n");
myFile = SD.open("test2.txt");
if (myFile) {
while (myFile.available()) {
Serial.write((char)myFile.read());
}
myFile.close();
}
else {
Serial.println("error opening test.txt");
}
}
void loop() {
// put your main code here, to run repeatedly:
}
upp
Isn't there a conflict using Serial on the UNO to both receive GCODE from the Mega and send messages to Serial Monitor?
Most people use a PC program called Universal GCODE Sender to feed GCODE to their CNC machine.
Your GCODE Sender seems to be blasting GCODE as fast as it can. The stepper motors can't move fast enough to keep up with 115200 baud. You need to modify your GCODE sender to read messages from the GRBL to control the flow of data. Without that, the UNO's buffer is going to fill up and start throwing away input. The GCODE with random chunks thrown away is not going to make sense.
How can I send the Gcodes I read from the SDCard connected to the Arduino Mega to my Ardiuno Uno board with grbl upload? I found such a code and when I upload the code I get these responses in the serial terminal. Can you help with this?
#include <SD.h>
File myFile;
boolean restart = true;
void setup(){
Serial.begin(115200);
Serial1.begin(115200);
}
void loop(){
checkSD();
while(restart){
openFileSD();
sendGcode();
}
}
void checkSD(){
// Check if SD card is OK
// On the Ethernet Shield, CS is pin 4. It's set as an output by default.
// Note that even if it's not used as the CS pin, the hardware SS pin
// (10 on most Arduino boards, 53 on the Mega) must be left as an output
// or the SD library functions will not work.
while(!SD.begin(10)){
Serial.println("Please insert SD card...\n");
delay(1000);
}
Serial.println("SD card OK...\n");
delay(1000);
}
void openFileSD(){
String fileName = "" ;
char fileNameChar[100]={0}; // char array for SD functions arguments
Serial.println("Enter name for a gcode file on SD : \n");
emptySerialBuf(0);
fileName=getSerial(0);
for(int i=0;fileName.charAt(i)!='\n';i++) //convert String in char array removing '\n'
fileNameChar[i]=fileName.charAt(i);
if(!SD.exists(fileNameChar)){ //check if file already exists
Serial.print("-- ");
Serial.print(fileNameChar);
Serial.print(" doesn't exists");
Serial.println(" --");
Serial.println("Please select another file\n");
delay(200);
openFileSD();
}
else{
myFile = SD.open(fileNameChar, FILE_READ); //create a new file
Serial.print("-- ");
Serial.print("File : ");
Serial.print(fileNameChar);
Serial.print(" opened!");
Serial.println(" --\n");
}
}
void emptySerialBuf(int serialNum){
//Empty Serial buffer
if(serialNum==0){
while(Serial.available())
Serial.read();
}
else if(serialNum==1){
while(Serial1.available())
Serial1.read();
}
}
void waitSerial(int serialNum){
// Wait for data on Serial
//Argument serialNum for Serial number
boolean serialAv = false;
if(serialNum==0){
while(!serialAv){
if(Serial.available())
serialAv=true;
}
}
else if(serialNum==1){
while(!serialAv){
if(Serial1.available())
serialAv=true;
}
}
}
String getSerial(int serialNum){
//Return String from serial line reading
//Argument serialNum for Serial number
String inLine = "test2";
waitSerial(serialNum);
if(serialNum==0){
while(Serial.available()){
inLine += (char)Serial.read();
delay(2);
}
return inLine;
}
else if(serialNum==1){
while(Serial1.available()){
inLine += (char)Serial1.read();
delay(2);
}
return inLine;
}
}
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
line = readLine(myFile); //read line in gcode file
Serial.print(line); //send to serials
Serial1.print(line);
Serial.print(getSerial(1)); //print grbl return on serial
}
}
else
fileError();
myFile.close();
Serial.println("Finish!!\n");
delay(2000);
}
void fileError(){
// For file open or read error
Serial.println("\n");
Serial.println("File Error !");
}
String readLine(File f){
//return line from file reading
char inChar;
String line = "";
do{
inChar =(char)f.read();
line += inChar;
}while(inChar != '\n');
return line;
}
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.