Raspberry Pi Tutorial Series: Serial

From Waveshare Wiki
Jump to: navigation, search

In this section, you will learn how to use the serial port of your Raspberry Pi.

The serial port of Raspberry Pi is often used to access the shell. However, in some condition you just wanna use it to communicate with UART peripherals. You can disable shell and kernel messages on the serial connection via Raspberry Pi configuration tool:

sudo raspi-config

Select Advanced Options -> Serial -> <NO> to disable shell and kernel messages on the serial connection. Then edit the /boot/config.txt file and append:

enable_uart=1

Use Minicom for Serial debugging

minicom

We supposed that you have a USB to serial module (also is embedded in Pioneer600) and attach it to your PC for serial communication.

1. Minicom is a tool for serial debugging over Linux environment.

sudo apt-get install minicom

2. Start the Minicom

minicom -D /dev/ttyAMA0
  • -D Specify the device, overriding the value given in the configuration file. The followed device, /dev/ttyAMA0, is the serial device specified by -D
  • The baud rate of serial is set to 115200 by default, which can be changed by -b 9600. You can read the detailed manual with the command man minicom.

Note: ttyAMA0 is used to communicate with the on-board Bluetooth device on Raspberry Pi 3, in this case, the serial port must be changed to ttyS0. ttyAMA0 also should be changed to ttyS0 in the following program as long as using a Pi 3.

Whilst, connect the TX and RX pin to the USB to serial module attached to the PC. Then open a serial monitor tool on the PC (Arduino software comes with a Serial Monitor, you can use it). Select a proper COM and baud rate, 115200.

3. When you input something to the minicom console, the serial monitor on PC shows the input information accordingly. Similarly, something sent by serial monitor will be shown on the minicom console. If local echo for minicom is on (toggle on/off: press Ctrl + A then E), the shell will echo what you typed. Last, you can quit from minicom with Ctrl + A then Q.

Serial programming

There are many libraries for Raspberry Pi Serial programming. Here some examples given:

C Program Based on WiringPi Library

#include <stdio.h>
#include <wiringPi.h>
#include <wiringSerial.h>
 
int main()
{
    int fd;
    if(wiringPiSetup() < 0)return 1;
    if((fd = serialOpen("/dev/ttyAMA0",115200)) < 0)return 1;
    printf("serial test start ...\n");
    serialPrintf(fd,"Hello World!!!\n");
    while(1)
    {  
        serialPutchar(fd,serialGetchar(fd));
    }  
    serialClose(fd);
    return 0;
}

You can compile the C program with:

gcc -Wall uart.c -o uart -lwiringPi

and run with:

sudo ./uart

A message sent to Windows, i.e. "Hello Wrold !!!", will be printed on the serial monitor. And messages sent from the monitor will be printed on the monitor again.

Python Program Based on Serial Library

Before using this program, you should install the serial library for Python. Get it by apt-get command:

sudo apt-get install python-serial

Here is the python program.

#!/usr/bin/python
# -*- coding:utf-8 -*-
import serial
 
ser = serial.Serial("/dev/ttyAMA0",115200)
 
print('serial test start ...')
ser.write("Hello Wrold !!!\n")
try:
    while True:
        ser.write(ser.read())
except KeyboardInterrupt:
    if ser != None:
        ser.close()

Save the file as "uart.py" and run with:

sudo python uart.py

The expected result is the same as the C program. For more details about python-serial, see: https://pythonhosted.org/pyserial/pyserial_api.html Tips: both WiringPi and Python control serial port via reading and writing /dev/ttyAMA0, and we can also control serial port via sysfs.