Tuesday 13 November 2018

[Technique] Three most common communication protocols: (1) UART

Universal Asynchronous Receiver/Transmitter (UART) communication protocol

1. Brief introduction

A universal asynchronous receiver-transmitter (UART /ˈjuːɑːrt/) is a computer hardware device for asynchronous serial communication in which the data format and transmission speeds are configurable. UART communication protocol is only used bwteen MCUs or MCUs and computers.
More information refers to http://www.circuitbasics.com/basics-uart-communication/

2. How many serial ports in the Arduino boards?

Different Arduino boards are different from each other.
  • For Uno, there is only one hardware serial port which is used for USB communication. On the board, there is another pair of pins for serial communication. 0 (RX) and 1 (TX) are used to receive (RX) and transmit (TX) TTL serial data. These pins are connected to the corresponding pins of the ATmega8U2 USB-to-TTL Serial chip. So it is not the hardware serial port.
  • For Leonardo, there are two classes of hardware serial ports. The Serial class refers to USB communication; another hardware serial port--Serial1, 0 (RX) and 1 (TX), is used to receive (RX) and transmit (TX) TTL serial data using theATmega32U4 hardware serial capability.
  • For Mega, there are three hardware serial ports: Serial 1: 19 (RX) and 18 (TX); Serial 2: 17 (RX) and 16 (TX); Serial 3: 15 (RX) and 14 (TX). Besides, on the board, the Serial, 0 (RX) and 1 (TX), is also connected to the corresponding pins of the ATmega16U2 USB-to-TTL Serial chip. These four serials are used to receive (RX) and transmit (TX) TTL serial data.  
  • In addition: All Arduino boards support software serial when there is no available serial ports.
  • Supplement: This method of serial communication is sometimes referred to as TTL serial (transistor-transistor logic). Serial communication at a TTL level will always remain between the limits of 0V and Vcc, which is often 5V or 3.3V. A logic high ('1') is represented by Vcc, while a logic low ('0') is 0V.  USB-to-TTL means the conversion from USB to TTL.

3. The difference between parallel communication and Serial communication.

  • Parallel interfaces transfer multiple bits at the same time. They usually require buses of data - transmitting across eight, sixteen, or more wires. Data is transferred in huge, crashing waves of 1’s and 0’s.




  • Serial interfaces stream their data, one single bit at a time. These interfaces can operate on as little as one wire, usually never more than four.

4. When communicating between the Arduino and the monitor, there are four formats for the output.

  • No line ending: means there is no modifier following your input. input=output
  • newLine: means there is a modifier '/n' following your input. output=input+'/n'  (n--newLine)
  • Carriage return: means there is a modifier '/r' following your input. output=input+'/r'  (r-return)
  • Both NL&CR: means there is a modifier '/n/r' following your input. output=input+'/n/r'
  • For example: if you input 'e', the output are 101, 101 10, 101 13, 101, 13, 10 respectively.  

5. Description of serial functions

6. SoftwareSerial Library

7. Demonstration

  • Serial communication between two Uno boards.
In this case, we take one as the sender and the other as the receiver. Different codes are uploaded to them. For the Uno board, there is only one hardware serial port. When we use Pin0 and Pin1 to communicate, the serial class is still Serial. The Uno will automatically detect the status of connection of Pin 0 and Pin 1 and send (Serial.write()) or receive (Serial.read()) the content if Pin 0 and Pin 1 are connected. At the same time, we can use Serial.print() to print the content on the computer monitor. These three different functions, Serial.write(),Serial.read() and Serial.print(), are very important in serial communication. The source code refers to https://iotguider.in/arduino/serial-communication-between-two-arduino-boards/.
  • Software serial communication between three Uno boards.
There would be a situation where Pin 0 and Pin 1 are being occupied and you still need to use the serial connection with other devices like another Uno board (shown in the above picture). How to deal with this conflict? As mentioned above, all Arduino boards support Software serial communication. Therefore, in this demo, I will show how to use Software serial communication. The jumper wires connection is:
In this figure, Pin 10 and Pin 11 are the software serial ports used for connecting the right Uno Board. Here are some source codes (copy-> paste->upload):
  • the left Uno
char mystr[5] = "Hello"; //String data
void setup() {
  // Begin the Serial at 9600 Baud
  Serial.begin(9600);
}
void loop() {
  Serial.write(s);
  delay(1000);
}
  • the middle Uno

#include <SoftwareSerial.h>
// software serial #1: RX = digital pin 10, TX = digital pin 11
SoftwareSerial portOne(10, 11);

char mystr[10]; //Initialized variable to store recieved data
char c;

void setup() {
  // Begin the Serial at 9600 Baud
  Serial.begin(9600);
 portOne.begin(9600);
}

void loop() {
  Serial.readBytes(mystr,5); //Read the serial data and store in var
  char c = portOne.read();
  Serial.println(mystr); //Print data on Serial Monitor
  Serial.println(c); //Print data on Serial Monitor
  delay(1000);
}
  • the right Uno

char s = 'B'; 
void setup() {
  // Begin the Serial at 9600 Baud
  Serial.begin(9600);
}
void loop() {
  Serial.write(s);//Write the serial data
  delay(1000);
}
  • The output
  • The usage of Software serial port
Generally, we can use Software serial ports whenever we want. But it would be best to use it when Pin 0 and Pin 1 are already used.

No comments:

Post a Comment

[Research] Recurrent Neural Network (RNN)

1. Introduction As we all know, forward neural networks (FNN) have no connection between neurons in the same layer or in cross layers. Th...