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)
Requirements
Ethernet or 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 PLC | RS232 Device |
| RX | TX |
| TX | RX |
| GND | GND |
Â
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);
}
}

How to use RS-232 on Industrial Arduino based PLC