How to connect two industrial PLCs through RS-485
22 May, 2020 by
How to connect two industrial PLCs through RS-485
Andrei Postolache

Introduction

RS-485, also known as EIA-485, is defined as a differential multipoint bus system; it is perfect to transmit data with high speed to ranges up to 12m. One of its most important characteristics is that its twisted pair of cables reduce the noise-induced in the transmission line. Multiple receivers may be connected to such a network in a linear, multi-drop bus. These characteristics make RS-485 useful in industrial control systems and similar applications.

Previous reading

We recommend you read the following posts in order to understand the program of this post. We used the following blog posts to do this example:

Requirements

In order to work with serial TTL communication, you will need any of our industrial programmable logic controllers for industrial automation:

Industrial Shields controllers: Wifi & Bluetooth Controller FamilyEthernet Controller FamilyGPRS / GSM Controller Family.

Hardware

To make the connection between the devices, we follow the schematic below.

Schematic to make the connection between the devices

Software

The following code has the function of communicating the Master and the Slave sending the instruction to activate the relay to turn on the water pump.

An example of Simplecomm Master communication:

#include <RS485.h>
#include <SimpleComm.h>
// Create SimplePacket for sending and receiving data
SimplePacket packet;
// Define master address
uint8_t masterAddress = 0;
// Define slave address to communicate with
uint8_t slaveAddress = 1;
// Value to send as packet data
int value = 5;
////////////////////////////////////////////////////////////////////////////////////////////////////
void setup() {
Serial.begin(9600L);
  // Start RS485
  RS485.begin(19200L);
  RS485.setTimeout(20);
  // Start SimpleComm
  SimpleComm.begin(masterAddress);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void loop() {
  static unsigned long lastSent = millis();
  // Send packet periodically: once per second
  if (millis() - lastSent >= 10000) {
  // Set request packet data
  packet.setData(value);
  // Send request to slave
  if (SimpleComm.send(RS485, packet, slaveAddress)) {
  lastSent = millis();
  Serial.print("Sent value: ");
  Serial.println(value);
  }
  }
  // Get responses
  if (SimpleComm.receive(RS485, packet)) {
  // Update value from the response
  value = packet.getInt();
  Serial.print("Received value: ");
  Serial.println(value);
  }
}
An example of Simplecomm Slave communication:
#include <SimpleComm.h>
// Create SimplePacket for sending and receiving data
SimplePacket request;
SimplePacket response;
// Define slave address to communicate with
uint8_t slaveAddress = 1;
////////////////////////////////////////////////////////////////////////////////////////////////////
void setup() {
Serial.begin(9600L);
  // Start RS485
  RS485.begin(19200L);
  RS485.setTimeout(20);
  // Start SimpleComm
  SimpleComm.begin(slaveAddress);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void loop() {
  // Get requests
  if (SimpleComm.receive(RS485, request)) {
  int value = request.getInt();
  Serial.print("Received value: ");
  Serial.println(value);
if ( value==5){
  digitalWrite(R0_8,HIGH);
  delay(5000);
  digitalWrite(R0_8,LOW);

}
  // Process value
  //value++;
  // Send response to the request packet source
  response.setData(value);
  if (SimpleComm.send(RS485, response, request.getSource())) {
  Serial.print("Sent value: ");
  Serial.println(value);
  }
  }
}
 
 

#include <RS485.h>

Subscribe to our  YouTube channel if you want to keep up to date with our tutorials.


Find what you are looking for
Search in our Blog

How to connect two industrial PLCs through RS-485
Andrei Postolache
22 May, 2020
Share this post
Archive

Looking for your ideal Programmable Logic Controller?

Take a look at this product comparison with other industrial controllers Arduino-based. 

We are comparing inputs, outputs, communications and other features with the ones of the relevant brands.


Industrial PLC comparison >>>

Do you want more information?

Just fill the form!

Tell me more!