2

Fritzing Schema connection 2 ArduinosI 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.

BernardG
  • 175
  • 1
  • 11
  • 1
    First, you shouldn't connect the TX. Having two arduino driving a single line could cause some serious problems, when one tries to drive it HIGH, and the other LOW. Secondly, there is no need to buffer the serial data coming in, till you read a newline. Just see if there is a byte available, and if so send the byte forward using Serial.write. This will also negate the need for String which is very inefficient. – Gerben Dec 14 '16 at 18:36
  • Sorry, but how can I initialize SoftwareSerial without a TX pin? In previous tests, I did as you say, just send every received byte, did not do any better. – BernardG Dec 14 '16 at 19:24
  • 1
    You can specify a pin in software. Just don't connect a wire between pin 10 and the TX pin on the target board. As the the other part. I though there could have been a speed issue, but that's apparently not it. – Gerben Dec 14 '16 at 19:31
  • A diagram of how you have it all connected would be useful. I think @Gerben is assuming you have the device connected to the existing serial lines (sniffing them), but I think you've got it connected like a man-in-the-middle... Right? – Mark Smith Dec 14 '16 at 19:31
  • I guess I could make quickly a fritzing picture, bu I don't think it's needed: I have one Arduino with GRBL on it, let's name it 'GRBL' I have a 2nd Arduino, with the code shown on it. Named 'USB' The USB Arduino is linked to the computer, and after flashing my code to it, I connect to it via a serial terminal. USB is also connected, via Pin 10 on grbl Pin 0 (TX on RX) and Pin 11 on grbl pin 1 (RX on TX). Both are also connected by a GRND wire. I have also connected USB +5 to GRBL VIN, but it seems it's not really required. – BernardG Dec 14 '16 at 19:43
  • 1
    @BernardG I suspect nobody is going to bother to decode that block of text. It's clear to you because you have it all in front of you and you've designed it. It's not very clear to me. A diagram would really help. – Mark Smith Dec 15 '16 at 04:15
  • @MarkSmith - I added a schema as you asked for. Hope it makes it clear. – BernardG Dec 15 '16 at 10:59
  • SoftwareSerial at 115200 is optimistic. – Mikael Patel Dec 15 '16 at 11:55
  • @MikaelPatel - I was starting to think along this line. I will change the parameter in GRBL to 57600, and do the same in my code. Eventually go lower if needed. I'll report here my findings. – BernardG Dec 15 '16 at 17:03
  • I just switched to 57600, as I said earlier, and without changing anything in code, it start to be much better. For example, Grbl welcome message is now correct, without garbage. There is still work to be done, but it seems I am on the right track now. – BernardG Dec 15 '16 at 17:35
  • @BernardG, please forgive my ignorance, I am new to Arduino. In your first comment, you mentioned just forwarding each byte, not waiting until end of line. How would you intercept commands without buffering them? Perhaps you could "start" to buffer, until such a point in time that the input can't match any of the commands to intercept, then forward byte-by-byte until next new line? Otherwise it seems that you would need to buffer? But I agree that this would get in the way if throughput... – AndrewP Dec 16 '16 at 02:22
  • @AndrewP. You are right, doing so would not allow me to intercept commands. It was just for testing if I could have an Arduino "in-between" transparently. Ince sure of that, I would have wrote the code needed to buffer. But I am not sure to continue in that direction, as it really looks like the normal speed of 195200 is way too high for SoftwareSerial. – BernardG Dec 17 '16 at 11:47
  • @BernardG, Ah, cheers for that :) good luck with the build! – AndrewP Dec 19 '16 at 00:10

1 Answers1

3

I finally solved my problem by buying an Arduino Due! It was a problem of speed, not of code. The Uno is just not powerfull enough to read an write to Software Serial at 115200 Bauds, or even 9600, which I tried.

The Due has absolutly no problem. I made a small code test to intercept "words" coming form "USB" To "GRBL", and it works just as expected, while GCODE is send (using Grbl-Panel on the PC) through the Due (USB) to the Uno (Grbl).

It's the very first time I solve what I thought to be a code problem by an hardware solution.

BTW, in case anyone wonder, yes, I am using a level shifter between the 2.

BernardG
  • 175
  • 1
  • 11
  • 1
    Hi, Bernard, thanks for sharing the code example and the answer to your original question. I just got Arduino Due and trying to use your example. However, I get "SoftwareSerial.h: No such file or directory". I wonder which software serial library you used that worked with Arduino Due? – Tomas Sep 14 '20 at 21:41