1

I'm trying to build my own CNC machine. Since I need to control a large number of motors at the same time, I want to control it via an Arduino master and several slaves with CNC shields. Now I'm trying to set up one motor by sending G-code via SoftwareSerial on the slave. I'm not getting any response from mySerial.

Can you please tell me if I need to integrate my code into the Grbl library?

Here's an example of a slave code:

#include <Wire.h>
#include <SoftwareSerial.h>
#define SLAVE_ADDR 9
#define ANSWERESIZE 5
SoftwareSerial mySerial(10, 11); // RX, TX для GRBL
String answer = "Hello";
String command = "";
void setup() {
 // Инициализация Wire для I2C
 Wire.begin(SLAVE_ADDR);
 Wire.onRequest(requestEvent); // Функция для отправки данных
 Wire.onReceive(receiveEvent); // Функция для получения данных
 mySerial.begin(115200); // GRBL работает на 115200 бод по умолчанию
 Serial.begin(9600); // Для отладки
 Serial.println("Initializing GRBL...");
 delay(1000); // Ждем загрузки GRBL
 
 // Включаем шаговые двигатели (посылаем команду на активацию)
 mySerial.println("$X"); // Разблокировка GRBL
}
void loop() {
 // Проверяем, есть ли команда для выполнения
 if (command.length() > 0) {
 Serial.println("command: " + command); // Выводим команду для отладки
 executeCommand(command); // Передаем команду GRBL для выполнения
 command = ""; // Очищаем команду после выполнения
 }
}
// Функция для обработки полученных данных через I2C
void receiveEvent(int howMany) {
 command = ""; // Очищаем строку перед началом чтения
 while (Wire.available()) {
 char c = Wire.read(); // Читаем байт и конвертируем в символ
 command += c; // Добавляем символ к строке команды
 }
 Serial.println("Received I2C Data!"); // Уведомляем о получении данных
}
// Функция отправки ответа обратно на мастер
void requestEvent() {
 byte response[ANSWERESIZE];
 for (byte i = 0; i < ANSWERESIZE; i++) {
 response[i] = (byte)answer.charAt(i);
 }
 Wire.write(response, sizeof(response)); // Отправляем ответ
 Serial.println("Sent response to master!"); // Уведомляем о отправке
}
// Функция для передачи команды GRBL
void executeCommand(String command) {
 // Отправляем команду G-кода через Serial (на GRBL)
 Serial.print("Sending to GRBL: ");
 Serial.println(command);
 // Отправляем команду на GRBL через SoftwareSerial
 mySerial.println(command);
 
 // Ждем выполнения команды
 delay(100); // Настройте задержку в зависимости от сложности движения
}

17:52:18.951 -> Received I2C Data!

17:52:18.951 -> cSent response to master!

17:52:19.000 -> ommand: G91 G1 X1600 F1000

17:52:19.018 -> Sending to GRBL: G91 G1 X1600 F1000

receiveEvent and requestEvent works fine, but there are some problem in executeCommand or mb in SoftwareSerial settings.

asked Oct 23, 2024 at 15:13
1
  • Welcome to SE/Arduino! Please take the tour to learn how this site works, and read some of the pages of the help center. Then come back and edit your question to clarify and extend. -- Are you aware that SoftwareSerial is sensitive to other interrupts and that it cannot work fullduplex? You might want to try an other library. -- Are you aware that serial transmission takes time on UART and I²C? Your receiving loop might miss later bytes/characters. -- Please translate your comments to English. Commented Oct 24, 2024 at 5:53

1 Answer 1

0

SoftwareSerial is disabling the interrupts for a long time and might interfere with Wire interrupts and hardware serial interrupts. You can try using AltsoftSerial which adds less latency to other interrupts. If still not working you might want to change to an Arduino with at least two hardware serial ports or find another way to debug your code and use hardware serial for sending G-codes.

answered Oct 31, 2024 at 12:57

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.