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

How to configure the Serial Port on industrial Arduino IDE

Example using RS-485 Serial Port in industrial automation environtment

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

RS-485 is a standard that defines the electrical characteristics of drivers and receivers 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 aldo in electronically noisy environments. Several receivers can be connected to a network on a multidrop, linear bus.

This protocol is common in industrial control systems and in this type of applications. 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.

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

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 located in the centre along 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 simultaneosuly.

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


The Simplex, the Full Duplex and the Half Duplex.


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 initilised by the master before, so direct transmission of information between the different slaves is not possible.

Configuration

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

Half duplex communication

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) : 

Syntax and parameters

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 successfull.

  • 1/2 Bits of Stop: it is important to set 1 or 2 bits of 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 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:
}


Take a look at the Serial Section of the official Arduino page for further information. 

 

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