Go Down

Topic: Serial Repeater (Read 1 time) previous topic - next topic

AllanB

I made a cnc where an Arduino is controlling the motors. It works very fine with GRBL flashed to the Arduino. It receives and send serial communication via USB to and from my PC. No problems.

I would like to run my GRBL Arduino (uno rev3) from another Arduino (uno r2) - my control Arduino. The control Arduino should do a number of things but also be able to repeat serial communication from PC to GRBL Arduino. I connected GRBL Arduino's D0 and D1 to my Control Arduino D7 and D8 intending to use SoftwareSerial on these pins. rx->tx, tx->rx

I can't make my Arduinos talk to each other. This is the parts of my code, which should do the job. What I sent from my PC is shown at the LCD. And the initial "Ready ..." arrives at my PC. So the loop is running OK. Can anybody figure out whats wrong.

Code: [Select]
#include <SoftwareSerial.h>
#define rxPin 7
#define txPin 8
...
SoftwareSerial mySerial =  SoftwareSerial(rxPin, txPin);
...
void serialRepeater(){
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Serial repeater");
  Serial.begin(9600);
  Serial.println("Ready ...");
  while (!Serial)
  mySerial.begin(9600);
  mySerial.print("$");
  while (mode==3) {   
    if (mySerial.available()>0){
      serData=mySerial.read();
      Serial.write(serData);
      lcd.print(serData);
    }
    if (Serial.available()) {
      serData=Serial.read();
      mySerial.write(serData);
      lcd.print(serData);
      Serial.write(serData);
    }
    if (Button(btnReverse)) {
      while (Button(btnReverse)) {}
      mode=0;
    }
  } 
  lcd.clear();
  mainmenu();
}

void loop() {
   mode=3;
   serialRepeater();
}

AllanB

Just got a hole through.

I minimized the code. Now it looks like this:
Code: [Select]
void serialRepeater(){
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Serial repeater");
  Serial.begin(9600);
  Serial.println("Ready ...");
  mySerial.begin(9600);
  mySerial.listen();
  while (mode==3) {   
    while (mySerial.available()){
      Serial.write(mySerial.read());
    }
    while (Serial.available()>0) {
      mySerial.write(Serial.read());
    }
    if (Button(btnReverse)) {
      while (Button(btnReverse)) {}
      mode=0;
    }
  } 
  lcd.clear();
  mainmenu();
}


Only problem now is, that it looses characters if message is long.

PaulS

Code: [Select]
  while (!Serial)
  mySerial.begin(9600);

Why?

snippets-r-us.com is down the internet a ways. Here, we get to see ALL of your code.

AllanB

Yes "while (!Serial) " is indeed the error. It must be an error I made when I was copying code from an example. I'm not used to program in C, so my eyes just didn't catch it.
Now my only problem is, that when GRBL dump current settings, the text becomes more and more erroneous. Here's an example of the dump:

Code: [Select]
$0 = 200.000 (steps/mm x)
$1 = 200.000 (steps/mm y)
$2 = 200.000 (steps/mm z)
$3 = 50 (microseconds step pulse)
$4 = 250.000 (mm/min default feed rate)
$ = 5000 (m/indeaul sekrae)
$ =0.00(m/ac sgmnt
7  20 ste prtiner msk bnay  100100
8  1.00 aceleatoninmmse^2
9  005 (crnrig untin dvitin n m)
'x=vlu' o etpaamte o jst'$'todup uren sttigs
o


And the failures are not at the same positions each time.

robtillaart


The problem is in this code I think as it promotes deadlock
Code: [Select]
  while (mode==3) {   
    while (mySerial.available()){   <<<<<<<<<<
      Serial.write(mySerial.read());
    }
    while (Serial.available()>0) { 
      mySerial.write(Serial.read());
    }
    if (Button(btnReverse)) {
      while (Button(btnReverse)) {}
      mode=0;
    }
  } 


If serial is getting data constatntly the second loop gets no time

try to replace it with

Code: [Select]
 
while (mode==3)
{   
  if (mySerial.available()) Serial.write(mySerial.read());
  if (Serial.available()>0) mySerial.write(Serial.read());
  if (Button(btnReverse) == HIGH)   // more explicit
  {
    while (Button(btnReverse) == HIGH ) ;  // wait for release
    mode=0;
  }


Now the code check both directions alternatingly
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Go Up