How to use RS232 on Industrial Arduino Based PLC

How to use RS232 on Industrial Arduino Based PLC

On this post it’s showed how to use RS232 on an Arduino based PLC of Industrial Shields. To use RS232 is required the next points:

  • Industrial Shields equipment (not any Ardbox HF version)
  • Industrial Shields boards installed, follow this older post to install it.

Hardware Configuration

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

Industrial Shields PCL RS232 Device
RX TX
TX RX
GND GND

 

Also it’s important to have the proper configuration of the switch’s. If the equipment is an M-Duino PLUS you must have the switch’s RX1/I1.6 and TX1/I1.5 on ON position to enable the communication hardware.

*Remember that I1.6 and I1.5 will be disabled, so if you use RS232 on M-Duino 42 or M-Duino 58 you will use two interrupt inputs. 

If the equipment is an Ardbox V7 version (analog or relay) you must place the switch on ON position. 

*Remember that the related I/Os with this pins will be disabled, so if you use RS232 on Ardbox V7 version you will use some I/Os. 

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

Write example:

// 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 example:

// 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);
  }
}

 

See Also:

HOW TO USE THE MAPPING PINS OF INDUSTRIAL SHIELDS BOARDS

RS-485 COMMUNICATION EXAMPLE USING OUR LIBRARIES

Comments are closed.