4 May 2015

Read and write from serial port with Raspberry Pi

Submitted by Max

In this tutorial we will see how to use the serial port on Raspberry Pi. We will use the serial port available on Raspberry with a RS232/TTL 3-5,5V adapter and a USB-serial adapter.
By default the Raspberry Pi’s serial port is configured to be used for console input/output. This can help to fix problems during boot, or to log in to the Pi if the video and network are not available. 

To be able to use the serial port to connect and talk to other devices (e.g. a modem a printer.. ), the serial port console login needs to be disabled.

Here we use Raspberry Pi 2, and we connect a RS232/TTL 3-5,5V adapter to pins 4 (5V), 6 (GND) ,8 (TX),10 (RX) of Raspberry, obviously connect tx with rx and vice versa.

Emmeshop tutorial Raspberry serial connection

To search for available serial ports we use the command

dmesg | grep tty

The output is something like this

pi@raspberrypi ~ $ dmesg | grep tty
[    0.000000] Kernel command line: dma.dmachans=0x7f35 bcm2708_fb.fbwidth=656 bcm2708_fb.fbheight=416 bcm2709.boardrev=0xa01041 bcm2709.serial=0x93f9c7f9 smsc95xx.macaddr=B8:27:EB:F9:C7:F9 bcm2708_fb.fbswap=1 bcm2709.disk_led_gpio=47 bcm2709.disk_led_active_low=0 sdhci-bcm2708.emmc_clock_freq=250000000 vc_mem.mem_base=0x3dc00000 vc_mem.mem_size=0x3f000000  dwc_otg.lpm_enable=0 console=tty1 console=ttyAMA0,115200 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait
[    0.001774] console [tty1] enabled
[    0.749509] dev:f1: ttyAMA0 at MMIO 0x3f201000 (irq = 83, base_baud = 0) is a PL011 rev3
[    1.268971] console [ttyAMA0] enabled
pi@raspberrypi ~ $

Last line indicates that the console is enabled on the serial port ttyAMA0, so we disable it

Run the configuration command and follow the instructions below

sudo raspi-config

Emmeshop tutorial Raspberry serial

Emmeshop tutorial Raspberry serial

Emmeshop tutorial Raspberry serial

Emmeshop tutorial Raspberry serial

Emmeshop tutorial Raspberry serial

Reboot and try with

dmesg | grep tty

output now is

pi@raspberrypi ~ $ dmesg | grep tty
[    0.000000] Kernel command line: dma.dmachans=0x7f35 bcm2708_fb.fbwidth=656 bcm2708_fb.fbheight=416 bcm2709.boardrev=0xa01041 bcm2709.serial=0x93f9c7f9 smsc95xx.macaddr=B8:27:EB:F9:C7:F9 bcm2708_fb.fbswap=1 bcm2709.disk_led_gpio=47 bcm2709.disk_led_active_low=0 sdhci-bcm2708.emmc_clock_freq=250000000 vc_mem.mem_base=0x3dc00000 vc_mem.mem_size=0x3f000000  dwc_otg.lpm_enable=0 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait
[    0.001769] console [tty1] enabled
[    0.749438] dev:f1: ttyAMA0 at MMIO 0x3f201000 (irq = 83, base_baud = 0) is a PL011 rev3
pi@raspberrypi ~ $

Now we can use the serial ttyAMA0.
We connect an adapter usb / serial, then we will try to establish a communication between the two serial ports; obviously in a practical application to every serial we could connect a device, for example a modem, a printer a RFID reader etc.

Emmeshop tutorial Raspberry two serial port communication

After connecting the second serial port we launch the command to find the name that Raspberry gives him

dmesg | grep tty

The output is something like this

pi@raspberrypi ~ $ dmesg | grep tty
[    0.000000] Kernel command line: dma.dmachans=0x7f35 bcm2708_fb.fbwidth=656 bcm2708_fb.fbheight=416 bcm2709.boardrev=0xa01041 bcm2709.serial=0x93f9c7f9 smsc95xx.macaddr=B8:27:EB:F9:C7:F9 bcm2708_fb.fbswap=1 bcm2709.disk_led_gpio=47 bcm2709.disk_led_active_low=0 sdhci-bcm2708.emmc_clock_freq=250000000 vc_mem.mem_base=0x3dc00000 vc_mem.mem_size=0x3f000000  dwc_otg.lpm_enable=0 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait
[    0.001769] console [tty1] enabled
[    0.749438] dev:f1: ttyAMA0 at MMIO 0x3f201000 (irq = 83, base_baud = 0) is a PL011 rev3
[  971.919417] usb 1-1.2: pl2303 converter now attached to ttyUSB0
pi@raspberrypi ~ $

Ok, now we create two files, one who writes something on the ttyAMA0 port and the other that reads on the ttyUSB0 port.

serial_write.py

    #!/usr/bin/env python
    
    import time
    import serial
    
    ser = serial.Serial(
        port='/dev/ttyAMA0',
        baudrate = 9600,
        parity=serial.PARITY_NONE,
        stopbits=serial.STOPBITS_ONE,
        bytesize=serial.EIGHTBITS,
        timeout=1
    )
    counter=0
    
    while 1:
         ser.write('Write counter: %d \n'%(counter))
         time.sleep(1)
         counter += 1

serial_read.py

    #!/usr/bin/env python
    
    import time
    import serial
    
    ser = serial.Serial(
        port='/dev/ttyUSB0',
        baudrate = 9600,
        parity=serial.PARITY_NONE,
        stopbits=serial.STOPBITS_ONE,
        bytesize=serial.EIGHTBITS,
        timeout=1
    )
    counter=0
    
    while 1:
         x=ser.readline()
         print x

If we run both files, serial_read.py will read what serial_write.py writes

Emmeshop tutorial Raspberry serial write and read

This is just a small example but it can serve as a starting point to send a print to an old printer or read data from a router or a gps.

Emmeshop tutorial Raspberry serial connection