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

How to use RS-232 on Industrial Arduino based PLC

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:      Ethernet PLC     20 I/Os PLC    

Industrial Shields boards:   Industrial Shields Boards            


Implementation

- Hardware configuration

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

Industrial Shields PCLRS232 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:          Documentation 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 on 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 than Serial class. Take a look on 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 it’s necessary to know to use RS232 on Industrial Shields equipment’s. 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);
  }
}

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