How to use RS-232 on Industrial Arduino based PLC

A common standard for industrial communication
December 10, 2018 by
How to use RS-232 on Industrial Arduino based PLC
Alejandro Jabalquinto

Introduction

In this post, it is shown how to use RS232 on an Arduino based PLC of Industrial Shields. This post has been implemented for the M-Duino PLUS version and the Ardbox Relay/Analog HF (HW 232/485)

How to use RS-232 on Industrial Arduino based PLC

Requirements

Ethernet or 20 I/Os PLC:
Ethernet PLC >>>   20 I/Os PLC >>>    

Industrial Shields boards:
 Install the Boards >>>


Implementation

- Hardware configuration

The first step is to configure the hardware. The right connections between the desired devices are shown below:

Industrial Shields PLCRS232 Device
RXTX
TXRX
GNDGND

 

Also, it’s important to have the proper configuration of the switches. If the equipment is an M-Duino PLUS the RS-232 is always enabled. For the Ardbox Family equipments it is required some extra configuration:

Ardbox Analog HF RS-232:
Documentation RS-232 >>>

Ardbox Relay HF RS-232:


 

- Software Configuration

Once the hardware configuration is done it’s possible to proceed with the software part. First of all, it’s necessary to include the RS232.h library:

#include <RS232.h>

Then don’t forget to implement the proper initialization of your communication on the setup() function:

RS232.begin(9600);

*Remember that both devices have to communicate at the same baud rate. 

Now everything is ready to start using the RS232 functions or methods. RS232 is a class that can use the same methods as the Serial class. Take a look at Arduino web site references to see the different available functions. The most used methods that every Industrial Shields developed has to know are:

RS232.begin(baud); //sets the baud rate for RS232 data transmision
RS232.read(); //reads incoming RS232 data
RS232.write(byteToSend); //write binary data to RS232 port
RS232.write(dataToSend, dataLength); //write binary data to RS232 port
RS232.available() //returns the number of bytes available for reading on RS232 port

This is all necessary to know how to use RS232 on Industrial Shields equipments. Finally, it’s showed an example using RS232.


Example

- Write:

// Include Industrial Shields libraries
#include <RS232.h>

//// IMPORTANT: check switches configuration

////////////////////////////////////////////////////////////////////////////////////////////////////
void setup() {
  // Begin serial port
	Serial.begin(9600);

  // Begin RS232 port
  RS232.begin(38400);
}

////////////////////////////////////////////////////////////////////////////////////////////////////
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-232 port
    RS232.write(tx);
  }
}


- Read:

// Include Industrial Shields libraries
#include <RS232.h>

//// IMPORTANT: check switches configuration

////////////////////////////////////////////////////////////////////////////////////////////////////
void setup() {
  // Begin serial port
	Serial.begin(9600);

  // Begin RS232 port
  RS232.begin(38400);
}

////////////////////////////////////////////////////////////////////////////////////////////////////
void loop() {
  // Print received byte when available
  if (RS232.available()) {
    byte rx = RS232.read();

    // Hexadecimal representation
    Serial.print("HEX: ");
    Serial.print(rx, HEX);

    // Decimal representation
    Serial.print(", DEC: ");
    Serial.println(rx, DEC);
  }
}
 
 

Take a look at the GPRS Range >>>

... and discover the products of this family designed for industrial automation solutions using this technology.

Read our Case Studies  Case Studies >>>

You will find more technical articles and several project implementations based on PLC Arduino, Raspberry Pi and ESP32 that will help you with your industry automation.

​Search in our Blog

How to use RS-232 on Industrial Arduino based PLC
Alejandro Jabalquinto December 10, 2018

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