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.

13
  • 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
  • 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 4:15
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.

1
  • 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.