How to configure the Serial Port on industrial Arduino IDE

Example using the Serial Port in industrial automation environment
July 22, 2019 by
How to configure the Serial Port on industrial Arduino IDE
Boot & Work Corp. S.L., Quesada Dani Salvans

Index

1. Introduction: Serial Communication
2. Configuration
3. Code example


1.-Serial Port

Serial Communication is one of the existing methods that allows two devices the possibility to transfer data between them, sending the bits of information one after another over a communication channel or a physical link. It is often applied in a short distance communication applications between devices and it uses two wires in a bidirectional structure; one for transmit the data and the other for receive it. 

In the context of our Arduino based devices, the serial communication has the following features:

  • Asynchronous Communication: the sender and receiver PLCs are configured with the same baudrate (bits per second) for communication. Despite of this, they do not share a clock signal but, apart from that, both devices generate clocks in an independent way based on the previously configured baudrate.
  • Start and Stop Bits: every bite of information sent in asynchronous serial communication is framed by a start bit at the beginning and one or several stop bits at the end. 
  • Half-Duplex and Full-Duplex: in Half-Duplex communication the device cannot transmit and receive simmultaneously, it takes turns for one and the other action. On the other hand, in Full-Duplex communication, the devices can transmit and receive at the same time. 
  • ASCII Encoding: the data sent over Serial Port is usually encoded with ASCII standard.
  • Software Serial: apart from the common Serial communication, you can also use the Software Serial option when the first one cannot be applied. 


2.-Configuration

For exemaple, in the case of Half-Duplex communication, messages are always sent in the same format:

Half duplex communication

And, in the configuration of Arduino IDE for Serial you can see that you have to follow the next Syntax and Parameters (the configuration has to be set in the setup section and you can initialize as many serial ports as your working device has) : 

Syntax and parameters

If you put both images together, first of all, you can see that you have to set the Baudrate which is the communication speed of the port and it has to be the same in the Tx and Rx if they have to communicate through the same frequency channel. After that, we have to set the configuration parameters:

  • 8 Data Bits: here you have to set how many data bits you have to work with.
  • 1 Bit of Parity: here you have to write e (for even), o (for odd), or n (for nothing). It is important to set this correctly because the parity bit is a binary bit that indicates whether the number of bits with the value  1 in a group of bits is even, odd, or none. This is an important error detection method (it compares the 1's with the bit of parity to check if the transmission is successful).
  • 1/2 Bits of Stop: it is important to set 1 or 2 bits of the stop. It is important to know that with 1 bit, efficiency is 80%, and with 2 bits it drops to 72.7% but, sometimes, this extra bit can be a useful way to add a little extra time, especially at high baud rates and/or using soft UART, where time is required to process the received byte.


If you do not set the Serial configuration parameters apart from the baud rate speed that is always required, it will set up the default parameters configuration, that is SERIAL_8N1 (8 -> bits of data, N -> none, without parity, 1 -> bit of stop). Regarding the baud rate, it is important to always adjust the right speed according to your working device (you can consult the supported baud rates for every shield or device in their own datasheets). 


3.-Code Example

Here we can see an example which uses the Serial Port 1 (or 0 or 2). We have to take into account that our main family devices have the following Serial Ports available: 

  • M-Duino Family: Serial 0 (be careful, it is the same used to upload the code, and you cannot use it simmultaneously while uploading the code or using the Serial monitor) and Serial1.
  • ESP32 PLC Family: Serial1 and Serial2. 
  • Ardbox Family: they do not have Hardware Serial ports, but you can use Software Serial as you can see in the previous mentioned post. 

The Sender code:

/*
   Copyright (c) 2019 Boot&Work Corp., S.L. All rights reserved
   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU Lesser General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU Lesser General Public License for more details.
   You should have received a copy of the GNU Lesser General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/


void setup() {
  Serial1.begin(9600,SERIAL_8N1); //the default configuration
  //Serial.begin(38400,SERIAL_5E1); //even parity
  //Serial2.begin(19200, SERIAL_5O1); //odd parity


  Serial1.println("This is Serial1");
  //Serial.println("This is Serial 0");
  //Serial2.println("This is Serial 2");
}


void loop() {
  // put your main code here, to run repeatedly:

  byte tx = 0x5;

  // Echo the byte to the serial port again
  Serial1.write(tx);
}


The Receiver code:

/*
   Copyright (c) 2019 Boot&Work Corp., S.L. All rights reserved
   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU Lesser General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU Lesser General Public License for more details.
   You should have received a copy of the GNU Lesser General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/


void setup() {
  Serial1.begin(9600,SERIAL_8N1); //the default configuration
  //Serial.begin(38400,SERIAL_5E1); //even parity
  //Serial2.begin(19200, SERIAL_5O1); //odd parity


  Serial1.println("This is Serial 1");
  //Serial.println("This is Serial 0");
  //Serial2.println("This is Serial 2");
}


void loop() {

  // Print received byte when available
  if (Serial1.available()) {
    byte rx = Serial1.read();

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

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

Take a look at the Serial Section of the official Arduino page for further information. 

 

​Search in our Blog

How to configure the Serial Port on industrial Arduino IDE
Boot & Work Corp. S.L., Quesada Dani Salvans July 22, 2019
Share this post

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