Chaining serial connection

The idea is that I have a mega that's connected to micro that is connected to usb mini host that uses serial data.

I got the connection to work when it is PC<->Mega<->Micro<->PC. I can connect PC<->Mega<->USB (when connected to mega ofc) and PC<->Micro<->USB host.

Mega uses built-in serial and serial1. Micro uses built-in for connection to pc and softwareserial1 to mega and softwareserial2 to usb host.

Mega code

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Serial1.begin(9600);
  while(!Serial){}

 
      Serial.println("Running");

}

void loop() {
  // put your main code here, to run repeatedly:
  while(Serial.available()>0)
  {
    char in = Serial.read();
    Serial1.write(in);

  }
 
  while(Serial1.available()>0)
  {
    char out=Serial1.read();
    Serial.write(out);
  }

  }

Micro code:

#include <SoftwareSerial.h>
SoftwareSerial mySerial(10,11); //connection to Mega
SoftwareSerial mySerial2(8,9); //connection to MIni host


void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  while(!Serial){}

  mySerial.begin(9600);
  mySerial2.begin(9600);
  Serial.println("Running");

}

void loop() {
  // put your main code here, to run repeatedly:
  while(mySerial.available()>0)
  {
    char in = mySerial.read();
    mySerial2.write(in);
    Serial.write(in);
  
  }

  while(mySerial2.available()>0)
  {
    char out=mySerial2.read();
    mySerial.write(out);
    Serial.write(out);
  }
 
  }

When I send '?' or HELP to the usb mini host it responds with settings and information which I have been using to test the connection.

Like so:

Running
?

USB Host Ps3 Dual Shock Controller v1.02

SERIAL - Set Serial Data Output On/Off
(ON) - [ON|OFF]
HEX - Set Serial Data Output Hexadecimal On/Off
(ON) - [ON|OFF]
BAUD - Set Serial Port Baud Rate
[2400|4800|9600|14400|19200|38400|57600|115200]
I2C - Set I2C Address
(41 ) [1 - 127]
HELP or ? - display help

But as I said when I want to type into the Megas Serial and that should go to micro and then to the usb and back doesn't work. What gives?

I tested it the other way around and now it works, so the problem is the micro. Is there a way to make it work on micro?

Straight connection pc<->Micro<->USB works on the same softwareserial2 pins (8&9) so the problem apparently is using two software serials?

only one softwareserial can listen at a time. you can set it calling the listen function

tuomasjar:
Straight connection pc<->Micro<->USB works

I don't understand your diagrams. In my head the connection sequence would be PC <-> USB cable <->Micro

Can you post a clearer diagram (ideally a photo of a pencil drawing Simple Image Guide ).

A Micro has a spare HardwareSerial port so why are you using SoftwareSerial?

Using 2 or more instances of SoftwareSerial does not generally work.

...R

The USB means a USB Mini host board, not USB Cable.

Used 2 software serials so I could also connect to the serial monitor on PC for troubleshooting.

When I get this to work I would like to replace wired connection between micro and mega to RF wireless. Or could I replace the whole micro with RF serial?

Should I switch software to connection between arduinos and hardware connect to the usb host?

Robin2:
A Micro has a spare HardwareSerial port so why are you using SoftwareSerial?

tuomasjar:
Used 2 software serials so I could also connect to the serial monitor on PC for troubleshooting.

I tried to use hardware serial, but it didn't work either. I tried to switch to an UNO with same pins connected to no avail.

In the serial monitor it didn't show what I had sent when using micro/UNO as a middleman, but when I use MEGA it shows it double. And of course in other configurations it shows it once properly when testing connection.

tuomasjar:
I tried to use hardware serial, but it didn't work either. I tried to switch to an UNO with same pins connected to no avail.

I don't know what a mini USB host is - please post a link to its datasheet.
And what is the USB host connected to?

Your drawing in Reply #4 does make the connections a good deal clearer.

But I don't understand why you are not using Serial1 on the Micro for one of the serial connections and SoftwareSerial for the other.

And to get down to basics, what is the point of having the Micro at all? Why not just connect the Mega to the mini USB host?

...R

USB host is connected to a ps3 controller.

http://www.hobbytronics.co.uk/usb-host-mini

Idea is to have the micro to read controller and forward it to mega by using possibly using this:

for longer range remote control.

tuomasjar:
Idea is to have the micro to read controller and forward it to mega by using possibly using this:

Overview | Adafruit RFM69HCW and RFM9X LoRa Packet Radio Breakouts | Adafruit Learning System

for longer range remote control.

In that case I would expect you could use Serial1 on the Micro for one of the Serial connections and SoftwareSerial for the other one.

Even better would be to communicate with the mini USB using I2C and then use Serial1 for communication with the Mega.

That miniUSB device looks interesting.

...R

I tried using hardware serial, but didn't get any response from micro to mega. Can I try to monitor the micro via USB while connected also to Mega? Or is there too much interference?

Should I use some other data type than char? or other metod than write?

tuomasjar:
I tried using hardware serial, but didn't get any response from micro to mega.

When you say that I don't have the slightest idea of what you tried or what the specific outcome was.

Post a detailed diagram showing how everything was connected AND post the code that you tried AND post an example of the output that you got.

A Micro can easily communicate with a Mega using Serial1 on the Micro.

...R

First post was too long. Let me post a test at a time...

Test 1.
Connection basically is serial monitor - mega - micro - serial monitor

Code on the micro:

#include <SoftwareSerial.h>
SoftwareSerial mySerial(10,11); //connection to Mega



void setup() {
  // put your setup code here, to run once:
  mySerial.begin(9600);
  
  Serial.begin(9600);
  while(!Serial){}

  mySerial.println("Running Micro");

}

void loop() {
  // put your main code here, to run repeatedly:

   while(Serial.available()>0)
  {

    char out=Serial.read();
    mySerial.write(out);
  }

  while(mySerial.available()>0)
  {
    char in = mySerial.read();
    Serial.write(in);
  
  }

  }

Code on the Mega:

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Serial1.begin(9600); //connection to Micro
 // Serial2.begin(9600); //connection to USB Host
  while(!Serial){}

 
      Serial.println("Running MEGA");

}

void loop() {
  // put your main code here, to run repeatedly:
  while(Serial1.available()>0)
  {
    char in = Serial1.read();
    Serial.write(in);


  }
 
  while(Serial.available()>0)
  {
    char out=Serial.read();
    Serial1.write(out);

  }

  }

Connection:

OUtput:

Works as intended

Test case 2: Mega in the middle

Micro code same as previous.

Mega code:

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600); //connection to computer
  Serial1.begin(9600); //connection to Micro
  Serial2.begin(9600); //connection to USB Host
  while(!Serial){}

 
      Serial.println("Running MEGA");

}

void loop() {
  // put your main code here, to run repeatedly:
  while(Serial1.available()>0)
  {
    char in = Serial1.read();
    Serial2.write(in);
    Serial.write(in);

  }
 
  while(Serial2.available()>0)
  {
    char out=Serial2.read();
    Serial1.write(out);
    Serial.write(out);

  }

  }

Connection:

Output:

Works as intended

Test case 3: the intended connection

Micro code:

#include <SoftwareSerial.h>
SoftwareSerial mySerial(10,11); //connection to Mega



void setup() {
  // put your setup code here, to run once:
  mySerial.begin(9600);
  
  Serial.begin(9600);
  while(!Serial){}

  mySerial.println("Running Micro");

}

void loop() {
  // put your main code here, to run repeatedly:

   while(Serial.available()>0)
  {

    char out=Serial.read();
    mySerial.write(out);
  }

  while(mySerial.available()>0)
  {
    char in = mySerial.read();
    Serial.write(in);
  
  }

  }

Mega Code:

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Serial1.begin(9600); //connection to Micro
 // Serial2.begin(9600); //connection to USB Host
  while(!Serial){}

 
      Serial.println("Running MEGA");

}

void loop() {
  // put your main code here, to run repeatedly:
  while(Serial1.available()>0)
  {
    char in = Serial1.read();
    Serial.write(in);


  }
 
  while(Serial.available()>0)
  {
    char out=Serial.read();
    Serial1.write(out);

  }

  }

Connection:

Output:

doesn't work

Regarding Test case 3 ...

On a Micro Serial is not connected to pins 0 and 1, it only works via the USB connector. Serial1 is what links via pins 0 and 1

...R

Oh. :o Okay. No wonder it was not working :o

Boy I feel stupid now. :confused:

Robin2:
Regarding Test case 3 ...

On a Micro Serial is not connected to pins 0 and 1, it only works via the USB connector. Serial1 is what links via pins 0 and 1

...R

But it does mess up the upload if I have something connected to the pin 0 and 1. I mean when I try upload a sketch and I have something connected to the pins 0 and 1 for serial connection...

Is it a Micro?

tuomasjar:
But it does mess up the upload if I have something connected to the pin 0 and 1. I mean when I try upload a sketch and I have something connected to the pins 0 and 1 for serial connection...

It's a while since I used my Micro but I'm pretty sure I was able to upload code with the Micro's pins 0 and 1 in use. I think it behaves like a Leonardo.

...R