How to work with RS485 with a Raspberry PLC

Learning how to work with the RS485 communication of the Raspberry Pi PLC
March 8, 2021 by
How to work with RS485 with a Raspberry PLC
Boot & Work Corp. S.L., Marti Guillem Cura

Introduction

RS-485, also known as EIA-485, is defined as a differential multi-point bus system; it is perfect to transmit data with high speed at ranges up to 12m. One of its most important features is that the twisted pair of wires reduces induced noise on the transmission line. Multiple receivers can 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.

Our PLCs for industrial automation have the SC16IS752 integrated circuit inside. This is a low-power transceiver used for RS-485 communication that operates with a single power supply of 5V and the average current is 300μA. Using half-duplex communication to convert the TTL level to RS-485 level, it can reach the maximum transmission speed of 2.5Mbps.

Odoo text and image block

In this post, you will learn how to do the basics to work with the RS485 of Industrial Shields Raspberry Pi programmable logic controllers. By reading this post, you will be able to understand how to connect and configure your industrial Raspberry Pi based PLC controller.

Previous readings

We recommend you to read the following posts in order to understand the program of this blog. We have used the following blog posts before making this example:

  • How to access the Raspberry Pi PLC Read >>
  • How to change the IP in Windows and Linux Read >>
  • Basics about digital inputs of a Raspberry PLC Read >>

Requirements

In order to follow the blog and do all the corresponding tests we will need the following devices:

  • 12/24 Vdc Power Supply Read >>
  • Any device of the Raspberry PLC Family Read >>
  • Any device of the M-Duino / Ardbox Family Read >>
  • Some wires
   

Hardware

An industrial Raspberry PLC includes two RS485 ports half-duplex . To make it work as a full-duplex communication, you should use both at the same time, one to send data and the other to receive. 

First of all, make sure that the PLC is powered with 12-24 Vdc power voltage. In order to work with the RS48 we must connect the pins A1+ and B1- with the corresponding A+ and B-. 





Software

NOTE: For RPI PLC Version 4 you must use ttySC2 instead of ttySC0 and ttySC3 instead of ttySC1. 

Once the hardware configuration is done, let's configure the software. The industrial Raspberry Pi PLC is configured by default to work with the RS485 communication. If you are using a different image than the default, please follow the instructions of the Section 15.2 (32-bit version) or 15.3 (64-bit version) of the Raspberry PLC Family User Manual.

 

The Raspberry Pi industrial PLC uses the serial ports ttySC0 and ttySC1 to communicate. In this post, you will see how to communicate with an M-Duino Family PLC in order to send and receive data. First of all, you will do an initial test to check the RS485 in your Programmable Logical Controller.


Verification Test

In the following test, you are going to use the two RS485 channels to communicate with each other, connecting A+ pin with the other A+ and B- to the B- pin of the same PLC. After connecting the wires, you will create a new bash script.

nano Test_RS485.sh 


First of all, you will initialize both serial ports to the same baud rate of 115200. 

#!/bin/bash

stty 115200 -F /dev/ttySC0 raw -echo #will be used to send data
stty 115200 -F /dev/ttySC1 raw -echo #will be used to receive data


All data received from the /dev/ttySC1 will be stored in the temporary file rs485.txt. You will send an "OK" message from the /dev/ttySC0, if everything is connected properly, it will be sent to the serial /dev/ttySC1 and written at rs485.txt. The resulting message will be stored in the RESULT variable for future verification. Be sure to kill the cat process, otherwise, it will run in the background.

cat /dev/ttySC1 > /tmp/rs485.txt &
sleep 1
echo "OK" > /dev/ttySC0
RESULT=$(cat /tmp/rs485.txt)
sudo killall cat > /dev/null


Finally, you will check if there is any data in the variable RESULT. If the communication has been successful, you will print on the terminal the received message "OK". Otherwise, a message error will be displayed.

if [ -n "${RESULT}" ] ; then
        echo rs485 true "${RESULT}"
else
        echo rs485 false "RS-485 cannot read from /dev/ttySC1"
fi


M-Duino connection test

After checking that both RS485 ports are working correctly, you will connect your Raspberry PLC to one of the M-Duino PLCs. In this case, we are using an M-Duino 54 ARA+. You will program your PLCs to send information from one to another and return a confirmation message. For the hardware connection, you must wire the A+ pin of the Arduino PLC to the A+ pin of the Raspberry PLC and also the B- with the B-.

On both Arduino programs you will do the same setup:
// Include Industrial Shields libraries
#include <RS485.h>
//// IMPORTANT: check switches configuration
////////////////////////////////////////////////////////////////////////////////////////////////////
void setup() {
  // Begin serial port
  Serial.begin(9600);
  // Begin RS485 port
  RS485.begin(115200);
}
////////////////////////////////////////////////////////////////////////////////////////////////////

The first program will send data from the Arduino PLC to the Raspberry PLC. You will be waiting for the data from the serial port and, when you get it, you will send it via RS485. Meanwhile, the Raspberry PLC will be waiting for the information for 5 seconds and, once received, it will show it on the terminal.  You must set up the following code on the loop function of the Arduino PLC:

////////////////////////////////////////////////////////////////////////////////////////////////////
void loop() {
  // Wait bytes in the serial port
  if (Serial.available()) {
    byte tx = Serial.read();

    // Echo the byte to the serial port again
    Serial.write(tx);

    // And send it to the RS-485 port
    RS485.write(tx);
  }
}
////////////////////////////////////////////////////////////////////////////////////////////////////

As for the Raspberry side, you will have to create a new bash script and paste the following code:

#!/bin/bash

stty 115200 -F /dev/ttySC0 raw -echo #will be used to send data
end=$((SECONDS+5))

while [ $SECONDS -lt $end ]; do
	cat /dev/ttySC0 > /tmp/rs485.txt &
	sleep 1
	RESULT=$(cat /tmp/rs485.txt)
	if [ -n "${RESULT}" ] ; then
		echo "${RESULT}"
		break
	fi
done

sudo killall cat > /dev/null

The second program will send data from the Raspberry PLC to the Arduino PLC. You will send a "Hello World!" message from the serial ttySC0 port of the Raspberry PLC and, when you get it, you will display it on the serial monitor of the Arduino IDE.

Add the following lines before the setup:
  int Period = 1000; //Periode d'enviament de dades en ms
  int unsigned long Time;
  String rcv_message;

You must set up the following code on the loop function of the Arduino PLC:

////////////////////////////////////////////////////////////////////////////////////////////////////
void loop() {
  //Print received byte when available
  if (RS485.available()) {
    rcv_message = RS485.readString();
    Serial.println(rcv_message);
  }
}
////////////////////////////////////////////////////////////////////////////////////////////////////

As for the Raspberry side, you will have to create a new bash script and paste the following code:

#!/bin/bash

stty 115200 -F /dev/ttySC0 raw -echo #will be used to send data
echo "Hello World!" > /dev/ttySC0

Want to know more about Industrial Shields Raspberry Pi PLC?

Discover the features of this open source based controller.


​Search in our Blog

How to work with RS485 with a Raspberry PLC
Boot & Work Corp. S.L., Marti Guillem Cura March 8, 2021

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