Shared Serial Communication With PC and Multiple Arduino's

Good day everyone

So i have a Windows form application which is connected to my 'MASTER' Arduino with a USB cable. I can write to the port and then execute operations on the Arduino accordingly.
My master Arduino is connected to 3 other 'SLAVE' Arduino's via TTL-RS485 modules.

My question is - How can share the serial communication between all the Arduino's so that if I send a command via the PC that every Arduino receives the command?
Also, the SLAVES need to send back data to the PC with the serial communication.

Any help would really be appreciated
Thank you

you can have multiple slaves listening on their TTL-RX line connected to the TTL-TX line of your PC. that will work, so all the slaves can get the message

but you can't have all the Tx joined together and sending data back to the Rx of your PC.

Adding a diode on each Tx line and a pull-up can probably do the trick so that you don't destroy your Tx pins but you also need to solve the issue that only one slave will be able to talk at a given time.

of course all the GND needs to be joined

I'd use a different protocol amongst the Arduinos, e.g. I2C.

@mr_tropica, when using RS485 with multiple slaves, slaves are identified by an ID. So when you send a message like ID data the slave for who it's intended will pick it up while the others ignore it. You're free to use a common ID (e.g. 0) that all slaves will listen to.

To get data from a slave, you need to use polling and poll each slave in sequence using the unique ID.

@J-M-L, OP is using (or intends to use) RS485.

I had this in mind (for PC --> Arduinos)

Ah, I see.

I was thinking of an Arduino with two serial ports (or software serial).

yes that can work too

in this case the master Arduino needs to echo the commands to the slave Arduinos.

Did he not say RS-485 links?

Why would you want separate RS-485 links? It is specifically a bus protocol. :astonished:

It's not a multi-master bus as required by a master PC and a master Arduino. This suggests to use different protocols between the PC and master Arduino and the master Arduino and its slaves. This suggests to me a full duplex serial protocol between PC and master Arduino and something different (I2C...) to the slaves. Except if a Mega is used with multiple Serial ports.

fair question. not sure what OP wants / needs are

If you put the slaves on SPI bus, you wouldn't have this problem.

@GoForSmoke, @DrDiettrich

I don't think that OP mentioned a distance between master and slaves. But the fact that he/she mentions RS485 might be a pointer :wink:

I2C was my first option but unfortunately the distance between the Arduino's is around 1m each for a total of 4m between the last slave and the master

Here is a schematic of how i have my TTL-RS485 and my Arduinos set up now

My code for the master

#include <SPI.h>

#define enablePin 2
String dfd = "";
bool stringComplete = false;

int tim = 10;
bool isActive = true;

void setup()
{
  Serial.begin(9600);
  pinMode(enablePin, OUTPUT);
  digitalWrite(enablePin, HIGH);
}

void loop()
{
  while(Serial.available())
  {
    char c = (char)Serial.read();
    dfd += c;
    if(dfd == "HELLO")
    {
      CallBack("Call Back");
    }
  }
  if (stringComplete) 
  {
    GetCommand(dfd);
    dfd = "";
    stringComplete = false;
  }
  if(isActive == false)
  {
    if(tim > 0)
    {
      tim -= 1;
    }
    else
    {
      Timeout();
    }
  }
  delay(200);
}

void CallBack(String g)
{
  delay(50);
  digitalWrite(enablePin, HIGH);
  isActive = true;
  tim = 10;
  Serial.println(g);
  dfd = "";
}

void GetCommand(String cmd)
{
  if(cmd == "PM\n")
  {
    Serial.println("TEST");
    delay(50);
    digitalWrite(enablePin, LOW);
    isActive = false;
  }
}

void Timeout()
{
  tim = 10;
  isActive = true;
  digitalWrite(enablePin, HIGH);
}

void serialEvent()
{
  while (Serial.available()) 
  {
    char inChar = (char)Serial.read();
    dfd += inChar;
    if (inChar == '\n') 
    {
      stringComplete = true;
    }
  }
}

Code for Slave

#define enablePin 2
String cmd = "";

void setup()
{
  Serial.begin(9600);
  pinMode(enablePin, OUTPUT);
  digitalWrite(enablePin, LOW);
}

void loop()
{
  while(Serial.available())
  {
    char c = (char)Serial.read();
    cmd += c;
    if(cmd == "TEST")
    {
      Response("HELLO");
    }
    
  }
  
  delay(100);
}

void Response(String Res)
{
  delay(100);
  digitalWrite(enablePin, HIGH);
  Serial.println(Res);
  delay(50);
  digitalWrite(enablePin, LOW);
  cmd = "";
}

> Blockquote

Humm, use ESP32's with WiFI and MQTT for comms. Install MQTT Broker, for free, onto the PC. On the PC use Node-Red as a traffic manager or wrote your own code.

I2C can handle meters of distance, SPI needs to be short for speed.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.