[SOLVED] Serial Communication: 1 master and multiple slaves

Hi there,

Hope you all doing well.

I'm new to serial communication. My idea is to read integer values from slave using serial communication via the master's serial monitor.

I need to connect 5 other arduinos in this way. I wanted to to check with basics.

I wrote the code for master and a single slave to test this. Used arduino mega for master and uno for slave. And used Tx1,Rx1 for master. But, Serial monitor keep printing only "-1" for all my readings.

My slave code:

//Address informations
#define MasterID 0;
#define myID 1

//ACK byte definitions
#define byte_1_ACK 100
#define byte_2_ACK 101
#define byte_3_ACK 102
#define byte_4_ACK 103
#define byte_5_ACK 104
#define byte_6_ACK 105

int called_ID;


void setup() {
  Serial.begin(9600);
  //if(!Serial.available());
}


void loop() {


  called_ID = Serial.read(); 

  if(called_ID = myID){

  while(Serial.read()== byte_1_ACK){
    Serial.write(10);
    }

  while(Serial.read()== byte_2_ACK){
    Serial.write(20);
    }
  
  while(Serial.read()== byte_3_ACK){
    Serial.write(30);
    }
    
  while(Serial.read()== byte_4_ACK){
    Serial.write(40);
    }

  while(Serial.read()== byte_5_ACK){
    Serial.write(50);
    }

  while(Serial.read()== byte_6_ACK){
    Serial.write(60);
    }

  called_ID = 0;   
  }
}

And my Master code is:

//Master program

//Address informations
#define MasterID 0

#define slave_1 1
#define slave_2 2
#define slave_3 3
#define slave_4 4
#define slave_5 5
#define slave_6 6

//ACK byte definitions
#define byte_1_ACK 100
#define byte_2_ACK 101
#define byte_3_ACK 102
#define byte_4_ACK 103
#define byte_5_ACK 104
#define byte_6_ACK 105


void setup() {
  Serial.begin(9600);
  Serial1.begin(9600);
  if (!Serial.available());
  if (!Serial1.available());

}

void loop() {
  
  Read_slave_1();
  delay(1000);  
}

And the Read_slave_1() function is:

int val[5];

void Read_slave_1(){

  while(Serial1.available()){
    Serial1.write(slave_1);
    delay(5);
  }
  val[0] = Serial1.read();

  while(Serial1.available()){
    delay(5);
    Serial1.write(byte_1_ACK);
  }
  val[1] = Serial1.read();

  while(Serial1.available()){
    delay(5);
    Serial1.write(byte_2_ACK);
  }
  val[2] = Serial1.read();

  while(Serial1.available()){
    delay(5);
    Serial1.write(byte_3_ACK);
  }
  val[3] = Serial1.read();

  while(Serial1.available()){
    delay(5);
    Serial1.write(byte_4_ACK);
  }
  val[4] = Serial1.read();

  while(Serial1.available()){
    delay(5);
    Serial1.write(byte_5_ACK);
  }
  val[5] = Serial1.read();

  while(Serial1.available()){
    delay(5);
    Serial1.write(byte_6_ACK);
  }
  val[6] = Serial1.read();

  ///////////////////////////////
  Serial.print("Slave 1 Readings: ");
  Serial.print(val[0]);
  Serial.print(" | ");
  
  Serial.print(val[1]);
  Serial.print(" | ");

  Serial.print(val[2]);
  Serial.print(" | ");

  Serial.print(val[3]);
  Serial.print(" | ");

  Serial.print(val[4]);
  Serial.print(" | ");

  Serial.print(val[5]);
  Serial.print(" | ");

  Serial.print(val[6]);
  Serial.println(" | ");

  Serial.println();
  Serial.println("****************************************");
  delay(100);

  
}

Would be a great help if any one can help me. And, please be cool, may be my code is wrong as I'm too new to microcontroller programming.

Have a nice day :slight_smile:

Which Arduino boards are you using ?
How are all the RX and TX connected ?
Did you connect the GND as well ?

When you do a Serial.read(), it return -1 if nothing was available. You have to check if something is available before you do a Serial.read().

By the way, you shall also take care that multiple slaves can not be connected directly to the same RX pin on the Master.

You will need to add 3-state buffers on the slave TX, and if your distance becomes too high for 5V signal, then you must forget RS232 too (since it does not support the 3 state concept). You will need to use a RS485 transceiver between the slave TX and the master RX.

Benoit

By the way, you shall also take care that multiple slaves can not be connected directly to the same RX pin on the Master.

Yes they can if a small signal diode is used for isolation.

Hello Koepel,

Thank you so much for the reply.

I'm using Arduino Mega for master and a UNO for slave. As master's Tx0, Rx0 are connected to USB, I used, Tx1, Rx1. Yup, I connected the ground. When doing with 5 slaves, I'm gonna use RS485-Modules. But, currently, I'm checking in very short distance.

Can you please check my code and point me if anything is wrong?

Thank you very much again :slight_smile:

Serial is not RS485. You can't connect more than one TX lines together. You can't simulate RS485 without real RS485 chips and the proper wiring.

Serial.available() lets you know if there are incoming characters to read. You seem to be using it as if it was looking at the outgoing data.

Thank you @MorganS,

Thank you so much for the reply.

I'll try with the RS485 module. And, yeah I'll change the code and try. And, please suggest me if you have any example to follow.

Thanx

BenKissBox,

Thank you so much for the reply. My idea is also to use an RS485 module.
Thanx :slight_smile:

To fix your code, define the protocol first.
Do you want to send small packets of data ? I prefer to start that with 'stx' and end it with 'etx'.

To make everything run smooth in Arduino it is preferred to let the loop() run as fast as possible. No delay(), no waiting in while-loops. Only testing what needs to be done.
That makes it easier to add new code. Instead of delay() the function millis() can be used. Instead of waiting for data, just copy the data into a local buffer, and once all the data is in the buffer, process the data.

To copy the data into a local buffer, it is easier when the beginning and end of a data packet is know. Therefor I prefer the 'stx' and 'etx'. But others on this forum might use other solutions.

Do you want to add a checksum to the data packet ?

Thank you @Koepel,

The STX and ETX idea worked well. I used '\n' instead of STX and the program runs perfectly.

Thank you so much.
Have a nice day.. :slight_smile:

Cheers (Y)

Hi "hello_folks"
Can you send for me code after using a local buffer and 'stx' and 'etx'? I am a fresh student and I want to research for Arduino code. I hope you will help me. Thank you for your topic.