0

I am trying to implement some small program using below code, now this code works fine and I'm getting data to the slave. But I have more than one slave, so now I'm not sure how to send the Address of slave and will a slave identify that a message is for him? The program below works perfectly. I'm now trying to find out how to name a master and with slaves and then send message correctly to them.

One more thing, in the Master code I found that I didn't see anything which sends the message, I am using the Hardware serial port. Does this not require any trigger to send?

Master Code -

int enablePin = 2;  // use pin 2 for enable pin 

void setup() {
  // initialize serial:
  Serial.begin(9600);
  // setup enable pin for server (always high)
  pinMode(enablePin, OUTPUT);
  delay(10);
  digitalWrite(enablePin, HIGH);
}

void loop() {
  // send message to slave ("A") with different value
  Serial.println("SEND MESSAGE OVER RS485");
  for (int i = 0; i < 5; i++) {
    switch (i) {
      case 0 : 
              Serial.println("AKSHAY"); 
              break;
      case 1 : 
              Serial.println("MANSING"); 
              break;
      case 2 : 
              Serial.println("ABHISHEK"); 
              break;
      case 3 : 
              Serial.println("KISLEY"); 
              break;
    }
    delay(2000);
  }
}

Slave Code -

int enablePin = 2; // ebanble pin for slave

String inputString = "";         // a string to hold incoming data
boolean stringComplete = false;  // whether the string is complete

void setup() {
  // initialize serial:
  Serial.begin(9600);
  // setup enable pin for slave (always low)
  pinMode(enablePin, OUTPUT);
  delay(10);
  digitalWrite(enablePin, LOW);
  // set digital pin 8 - 11 for output
  for (int i = 8; i < 12; i++) {
    pinMode(i, OUTPUT);
  }
}

void loop() {
  // print the string when a newline arrives:
  if (stringComplete) {
    // make sure this message for slave "A"
    if (inputString.charAt(0) == 'A') {  
      for (int i = 1; i < 5 ; i++) {
        if (inputString.charAt(i) == '1')
          digitalWrite(7 + i, HIGH);
        else
          digitalWrite(7 + i, LOW);
      }
    } 
    // clear the string:
    Serial.println(inputString);
    inputString = "";
    stringComplete = false;
  }
}

/*
  SerialEvent occurs whenever a new data comes in the
 hardware serial RX.  This routine is run between each
 time loop() runs, so using delay inside loop can delay
 response.  Multiple bytes of data may be available.
 */
void serialEvent() {
  while (Serial.available()) {
    // get the new byte:
    char inChar = (char)Serial.read(); 
    // add it to the inputString:
    inputString += inChar;
    // if the incoming character is a newline, set a flag
    // so the main loop can do something about it:
    if (inChar == '\n') {
      stringComplete = true;
    } 
  }
}
  • 1
    your questions are so basic. as comment to your previous question you wrote that you understand the RS485. so you should know, that the RS485 transports every byte immediately. the software protocol you use to address and frame the messages is on you. or choose some existing protocol and library. Modbus is commonly used over RS485 – Juraj May 14 at 4:33
  • 1
    Using RS485 is the one way of achieving you are looking for, another way is to use software serial library in arduino where you can use another pins for communication with another slave with same or different baudrate. – Vaibhav May 14 at 5:28
  • 1
    @Vaibhav, you are not helping. RS485 adapter is to Arduino wired over UART. hw serial or sw serial can be used to communicate with the adapter – Juraj May 14 at 7:21
  • 1
    @Juraj actually I hadn't include adapter in my point. I was suggesting to use UART communication directly. – Vaibhav May 14 at 7:24
  • 1
    @Akshay, first learn how to do serial communication. how to compose and parse messages. you can learn it with the Nano and Serial Monitor majenko.co.uk/blog/reading-serial-arduino – Juraj May 14 at 7:25

Your Answer

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

Browse other questions tagged or ask your own question.