Raspberry Pi UART with pyserial

Source: Internet
Author: User

Reference: http://programmingadvent.blogspot.hk/2012/12/raspberry-pi-uart-with-pyserial.html

Raspberry Pi UART with pyserial os:occidentalis v0.2
Hardware:raspberry Pi Revision B with Cobbler
Setup:serial Loopback (Connect RX and TX pins on GPIO pins)

Linux attempts to treat all devices as file system like devices, the UART that's available on the GPIO pins are located at :

/dev/ttyama0

Configure The operating system:

Occidentalis comes pre-configured to allow your to console into the Raspberry Pi using the external UART. If you intend to use the UART for your own software you'll have to disable this functionality.

Below is a summary of this Post

First backup The both files you is going to edit with:

sudo cp/boot/cmdline.txt/boot/cmdline.txt.bak
sudo cp/etc/inittab/etc/inittab.bak

Then use your favorite editor to remove these the settings from/boot/cmdline.txt:

console=ttyama0,115200 kgdboc=ttyama0,115200

Then comment out of the line that mentions TtyAMA0 In/etc/inittab. (Place a # at the start of the line.

#T0:23:respawn:/sbin/getty-l ttyAMA0 11520 vt100

Install pyserial
Reference:http://pyserial.sourceforge.net/index.html

Pyserial is a Python library for interfacing with serial interfaces, it does isn't come standard with occidentalis. You can download it at pyserial-2.6.tar.gz. To extract the file and install the following commands:

mkdir pyserial-2.6
TAR-ZXVF pyserial-2.6.tar.gz pyserial-2.6
CD pyserial-2.6
Python setup.py Install

Using pyserial
Reference:http://pyserial.sourceforge.net/index.html

Here's a simple script, tests the serial connection in loopback.  Note that you need to wait for a bit in between sending characters and receiving them. This was because the command to send serial characters uses interrupts and does don't wait for the output to be put on the BU  s before it returns. If you print the input and output strings, the last character gets dropped once the output sting is Longe  R than can sent in the given delay. On mine it fails when writing strings of characters longer than-with a 0.5 second delay.


From serial import serial
Import time

SerialPort = Serial ("/dev/ttyama0", 9600, timeout=2)
if (serialport.isopen () = = False):
Serialport.open ()

outstr = ' '
InStr = ' '

Serialport.flushinput ()
Serialport.flushoutput ()

For I, a in enumerate (range (33, 126)):
Outstr + = Chr (a)

Serialport.write (OUTSTR)
Time.sleep (0.05)
INSTR = Serialport.read (serialport.inwaiting ())

#print "INSTR =" + inStr
#print "outstr =" + outstr

if (inStr = = outstr):
Print "worked! For length of%d "% (i+1)
Else
Print "Failed"

Serialport.close ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.