I need to have an Arduino "spying" on communications between the PC and another Arduino with GRBL. The goal is to intercept a couple of GCode commands and act on them instead of Grbl.
My first step is to attempt to be as transparent as possible, but so far, I am not getting from Grbl the clear answer I expect. For example, I get Grbl 1.1d [?2???B???u5 instead of Grbl 1.1d ['$' for help]
Here is my code so far.
#include <SoftwareSerial.h>
int PortOneTX = 10;
int PortOneRX = 11;
const String USB = "USB";
const String GRBL = "GRBL";
char endOfLine = '\n';
SoftwareSerial portOne(PortOneRX, PortOneTX);
String grblBytes = "";
String USBBytes = "";
void setup()
{
Serial.begin(115200);
while (!Serial)
{
; // Do nothing, just wait till we are connected through USB
}
portOne.begin(115200);
portOne.write("\r\n");
delay(2); ;
}
String stringtoWrite = "";
void loop()
{
if (portOne.available() > 0)//while (portOne.available())
{
stringtoWrite = portOne.readStringUntil(endOfLine);
for (int i = 0; i < stringtoWrite.length(); i++)
{
Serial.write(stringtoWrite[i]);
}
Serial.write(endOfLine);
}
if (Serial.available() > 0)//while (Serial.available())
{
stringtoWrite = Serial.readStringUntil(endOfLine);
for (int i = 0; i < stringtoWrite.length(); i++)
{
portOne.write(stringtoWrite[i]);
}
portOne.write(endOfLine);
}
}
I feel that I should be using events instead of if (serial.available) in the loop, but I am not sure how to go about it. I thought it would be easy!
Anyway, as I want Grbl-Panel to work totally transparently, I need this first step to be Ok before doing anything else.
Thanks for your help.