Look for in our Blog. You will find multiple applications, solutions, code examples. Navigate using the tag cloud or search using specific criteria

Configuration of RS-485 Protocol on Arduino IDE

Introduction

In this post, we will see how is the RS-485 protocol, the master-slave relation and we will deepen in how to configure this communication in the best possible way.


RS-485

RS-485 is a standard defining the electrical characteristics of drivers and recievers for use in serial communication systems. According to electrical signaling, this is balanced and all kind of multipoint systems are supported. Implementing the standard, digital communications networks can be used effectively over long ranged distances and in electronically noisy environments too. Several receivers can be connected to a network in a multidrop, linear bus.

This protocol is common in industrial control systems and this type of applications. It is used in PLC (Programmable Logic Controllers) and factory floors among other applications. It is applied as the physical layer underlying many standard and proprietary autmation protocols used to implement industrial control systems such as, for example, Modbus.

Here are some references to other posts of this blog about RS-485 and Modbus:

-Post: RS485 Troubleshooting Guide


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 centrally located along the set of RS-485 wires and two slaves are located at the physical end of the wires, making the terminations.

There are three types of relationships 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 an unidirectional transmission type which the Rx can not respond to the Rx.

  • The Half Duplex allows the transmission in both directions, although Tx and Rx share the same frequency, Tx can only occurs in one way simultaneosuly.

  • The Full Duplex allows the transmission in both directions and for the same channel simultaneosuly, using two different frequences; one for Tx and another for Rx.



Configuration

To explain the RS-485 configuration we are gonna take an example; the Sinamics V20 Converter from Siemens.

A master PLC can connect a maximum of 31 converters (slaves) through a serial interface and control them with the serial bus protocol USS. A slave cannot transmit if the master did not initialize it before, therefore it is not possible the direct information transmission between the different slaves.


In the Half-Duplex communication case, the messages are always sent in the same format:


And, in the Arduino IDE configuration for Serial we can see that we have to follow the next Syntax and Parameters (the configuration has to be set in the setup section and you can initialize so many serial ports as your working device has) : 


If we put in common both images we can see that, first of all, we have to set the Baudrate which is the communication speed of the port and it has to be equal 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 do you need 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 configurate this right cause the parity bit is a binary bit that indicates if the number of bits with the value of 1 in a group of bits is even, odd or nothing. Is an important error detection method (it compares the 1's with the bit of parity to check if the transmission is successfull.

  • 1/2 Bits of Stop: it is important to define 1 or 2 bits of stop. It is important to know that with 1 bit, efficiency is 80%, and with 2 bits 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 we don't set the configuration parameters of the Serial a part from the baud rate speed which is always necessary, 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 baud rate, it is important to always adjust the right speed according to our working device (we 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:
}

If you want to have a look at the Serial section of the official Arduino page, website where we based part of this post, to check more information, you can click on the link below:

- Arduino page: Serial Configuration



Do you want more information?

Open Source technology allows you to develop your installations.

Just fill the form and we will contact you as soon as we can.

Send