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

Using the SoftwareSerial library in Arduino Based PLCs

 

Introduction


There are situations in which, we want to use the serial ports of the equipment but these are already occupied, or, as is the case of the Ardbox family, that the default serial port of the Arduino Leonardo incorporated inside is occupied by the RS-232  or RS-485 protocols.

It is then, when using Hardware we can not use the physical serial ports of the equipment and there are two possible solutions:

The first and most economically costly is to use an RS-232 to TTL converter (We lose the use of the RS-232 protocol).

The second is the one that we explain in this post and consists in taking advantage of the SoftwareSerial library to simulate a serial port through Software. 


Requirements


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

SoftwareSerial Library:              SoftwareSerial Library

Configuration

For Ardbox Family (20 IOs PLCs):

    For the Ardbox family (Arduino Leonardo incorporated) all pins can be used for Tx while for Rx the following can be used:

    pin 14 of Leonardo (SO), pin 15 of Leonardo (SCK) or pin 16 of Leonardo(SI). 

    We recommend using one of the Rx-enabled pins for Tx.


Software

This sketch is very simple and shows how the library works. First, the Tx and Rx pins of the equipment for the Serial Software must be defined (we are trying it on an Ardbox Family model) :
SoftwareSerial mySerial(14, 15); // RX, TX

Rx is digital pin 14 (SO), connected to TX of the other device.
Tx is digital pin 15 (SI), connected to RX of the other device.

Receives from the serial hardware, sends to software serial, receives from software serial, sends to hardware serial
.



#include <SoftwareSerial.h>

SoftwareSerial mySerial(14, 15); // RX (MISO), TX (SCK)

void setup()  
{
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }


  Serial.println("Goodnight moon!");

  // set the data rate for the SoftwareSerial port
mySerial.begin(9600); mySerial.println("Hello, world?"); } void loop() // run over and over
{ if (mySerial.available())
Serial.write(mySerial.read());
if (Serial.available())
mySerial.write(Serial.read()); }

This other sketch sends instructions through the software serial: 

 

#include <SoftwareSerial.h>


SoftwareSerial mySerial(14, 15);  // Rx (MISO, Tx (MOSI)

void setup() {

  // put your setup code here, to run once:

mySerial.begin(9600);

Serial.begin(9600);


}

void loop() {

  // put your main code here, to run repeatedly:


delay(500);

mySerial.println("Instruction 1"); 

Serial.println("1st instruction sended");

delay(500); 

mySerial.println("Instruction 2"); 

Serial.println("2nd instruction sended");

}