technology workshop living food play outside
Read and write from serial port with Raspberry Pi
by emmeshop on May 4, 2015
Table of Contents
Read and write from serial port with Raspberry Pi . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Intro: Read and write from serial port with Raspberry Pi . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Related Instructables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Advertisements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
http://www.instructables.com/id/Read-and-write-from-serial-port-with-Raspberry-Pi/
Intro: Read and write from serial port with Raspberry Pi
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.
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 s
[ 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
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 s
[ 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.
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 s
[ 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
http://www.instructables.com/id/Read-and-write-from-serial-port-with-Raspberry-Pi/
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
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.
Follow us on social to stay informed.
http://www.emmeshop.eu
http://www.instructables.com/id/Read-and-write-from-serial-port-with-Raspberry-Pi/
http://www.instructables.com/id/Read-and-write-from-serial-port-with-Raspberry-Pi/
Related Instructables
How to Connect
Python Terminal Use Arduino VBNET control an Electronic Time &
for Cheap Pi and RS232 of ARDUINO Balance or Attendance AVR ISP
Arduino Shield to over TCP RS232 Scale to a PC System with
programmer by
Connection by interface by manu4371 and Read Raspberry and
Integrated UHF Milen
russ_hensel Weight Values Phidgets by
RFID reader Directly into emmeshop
LSID-0702 by Excel by tomlutz
Linksprite
Advertisements
Comments
1 comments Add Comment
tomatoskins says: May 4, 2015. 1:22 PM REPLY
This is so cool! The possiblilities of the Pi are generally limited to ones imagination.
http://www.instructables.com/id/Read-and-write-from-serial-port-with-Raspberry-Pi/