Index
1. Introduction
2. RS-485
3. Master-Slave
4. Configuration
5. Code example
Introduction
In this post, you will see how the RS-485 protocol is, the master-slave relation and you will go in-depth in how to configure this communication in the best possible way.
RS-485
This protocol is common in industrial control systems and in this type of application. It is used in industrial PLC Arduino (Programmable Logic Controllers) and factory floors among other applications. It is applied as the physical layer underlying many standard and proprietary automation protocols used to implement industrial control systems such as Modbus.
Communicating by MODBUS RTU through a RS485 serial interface (Seneca Z-D-in Module)
RS485 Troubleshooting Guide
Read the Post >>>
Master-Slave
Usually, when we talk about a master-slave arrangement, the master device initiates all the communication activity, providing itself the bias. There are many different communications but, in this one, the master is typically located in the center along with the RS-485 cable set, and two slaves are located at the physical end of the wires, making the terminations.
There are three types of relations between the master and the slave from the point of view of the Tx and Rx (Transmitter and Reciever); the Simplex, the Full Duplex and the Half Duplex.
The Simplex is a type of unidirectional transmission in which the Rx cannot respond to the Rx.
Half Duplex allows transmission in both directions. Although Tx and Rx share the same frequency, Tx can only occur in one way simultaneously.
Full Duplex allows transmission in both directions and for the same channel simultaneously, using two different frequencies; one for Tx and another for Rx.
Configuration
To explain the configuration of the RS-485, we will take an example: the Sinamics V20 Converter from Siemens.
A master PLC controller Arduino can connect a maximum of 31 converters (slaves) via a serial interface and control them with the USS serial bus protocol. A slave cannot transmit if it has not been initialized by the master before, so direct transmission of information between the different slaves is not possible.
In the case of Half-Duplex communication, messages are always sent in the same format:
And, in the configuration of Arduino IDE for Serial you can see that you have to follow the next Syntax and Parameters (the configuration has to be set in the setup section and you can initialize as many serial ports as your working device has) :Â
If you put both images together, first of all, you can see that you have to set the Baudrate which is the communication speed of the port and it has to be the same in the Tx and Rx if they have to communicate through the same frequency channel. After that, we have to set the configuration parameters:
8 Data Bits: here you have to set how many data bits you have to work with.
1 Bit of Parity: here you have to write e (for even), o (for odd), or n (for nothing). It is important to set this correctly because the parity bit is a binary bit that indicates whether the number of bits with the value 1 in a group of bits is even, odd, or none. This is an important error detection method (it compares the 1's with the bit of parity to check if the transmission is successful.
1/2 Bits of Stop: it is important to set 1 or 2 bits of the stop. It is important to know that with 1 bit, efficiency is 80%, and with 2 bits it drops to 72.7% but, sometimes, this extra bit can be a useful way to add a little extra time, especially at high baud rates and/or using soft UART, where time is required to process the received byte.
If you do not set the Serial configuration parameters apart from the baud rate speed that is always required, it will set up the default parameters configuration, that is SERIAL_8N1 (8 -> bits of data, N -> none, without parity, 1 -> bit of stop). Regarding the baud rate, it is important to always adjust the right speed according to your working device (you can consult the supported baud rates for every shield or device in their own datasheets).Â
Code Example
/*
  Copyright (c) 2019 Boot&Work Corp., S.L. All rights reserved
  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU Lesser General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.
  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  GNU Lesser General Public License for more details.
  You should have received a copy of the GNU Lesser General Public License
  along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// Arduino Mega using all four of its Serial ports
// (Serial, Serial1, Serial2, Serial3),
// with different baud rates and configurations:
void setup() {
 Serial.begin(9600,SERIAL_8N1); //the default configuration
 Serial1.begin(38400,SERIAL_5E1); //even parity
 Serial2.begin(19200, SERIAL_5O1); //odd parity
 Serial3.begin(4800);
 Serial.println("This is Serial");
 Serial1.println("This is Serial 1");
 Serial2.println("This is Serial 2");
 Serial3.println("This is Serial 3");
}
void loop() {
 // put your main code here, to run repeatedly:
}
Â
How to configure the Serial Port on industrial Arduino IDE