Read and Write From Serial Port With Raspberry Pi
Read and Write From Serial Port With Raspberry Pi
Table of Contents
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.
Last line indicates that the console is enabled on the serial port ttyAMA0, so we disable it
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
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
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.
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
http://www.instructables.com/id/Read-and-write-from-serial-port-with-Raspberry-Pi/