UART – Universal Asynchronous Receiver/Transmitter is one of the earliest mode of communication applied to computers. Asynchronous means that data is transferred without support from an external clock signal and universal because it’s parameters: speed, frame size and number of parity and stop bits are not fixed and can be configured to meet the needs of a given communication requirement.
The asynchronous serial protocol has four built-in-rules, mechanisms, which must be the same on both sides: Baud rate, Data bits, Parity bits and Stop bits.
UART parameters are set as: 9600 8N1 which means:
- BaudRate = 9600
- Data bits = 8
- No parity bit
- Stop bits = 1
Disabling console on ttyAMA0
Type dmesg | grep tty into terminal to check if Raspberry is using serial port for console login. If output is like picture below you will need to disable login shell over serial port.
Type in terminal:
Config screen should appear as on picture below. Select Advanced Options.
Next, select Serial and disable it.
Raspberry Pi will be rebooted. Again type dmesg | grep tty into terminal and watch output which should be just like picture below.
Check uart using pyserial
Install pySerial by typing in terminal.
sudo apt-get install python-serial
After installation is complete connect Tx (GPIO14) and Rx (GPIO15) together and write
python –m serial.tools.miniterm –p /dev/ttyAMA0 –e
Start typing and see if your letters double. If they do, then it means that UART is working as each letter we send we also receive. It’s also called Loop-Back test.
Below is example how you can also check UART with simple python script.
from serial import Serial
#Starting serial connection
serialPort = Serial("/dev/ttyAMA0", 9600, timeout=1)
#Check if port failed to open
if (serialPort.isOpen() == False):
#Flush before receiving or sending any data
#String as output
#Check if there is any byte waiting on serial port
if(serialPort.inWaiting() != 0):
#Read 1 byte at a time
input = serialPort.read(1)
#Closing serial port