Raspberry Pi UART with PySerial

参考: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 is available on the GPIO pins is located at:

/dev/ttyAMA0

Configure the operating system:

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

Below is a summary of This Post

First backup the two files you are 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 two settings from /boot/cmdline.txt:

console=ttyAMA0,115200 kgdboc=ttyAMA0,115200

Then comment out 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 not come standard with Occidentalis. You can download it at pyserial-2.6.tar.gz. To extract the file and install use 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 is a simple script that tests the serial connection in loopback. Note that you need to wait for a bit in between sending characters and receiving them.  This is because the command to send serial characters uses interrupts and does not wait for the output to be put on the bus before it returns.  If you print the input and output strings you will see that the last character gets dropped once the output sting is longer than can be sent in the given delay.  On mine it fails when writing strings of characters longer than 46 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()

Raspberry Pi UART with PySerial

时间: 2024-10-16 04:54:28

Raspberry Pi UART with PySerial的相关文章

Raspberry pi 使用python+pySerial实现串口通信(转)

Raspberry pi 使用python+pySerial实现串口通信 转:http://blog.csdn.net/homeway999/article/details/8642353 目录(?)[+] Raspberry pi 使用pythonpySerial实现串口通信 Raspberry pi端安装pyserial 方法1source安装 方法2pip安装 Raspberry pi端连接串口 Windows端连接串口 Raspberry pi 使用python+pySerial实现串口

RASPBERRY PI 外设学习资源

参考: http://www.siongboon.com/projects/2013-07-08_raspberry_pi/index.html Raspberry Pi         Get started with Raspberry Pi (RPi), a step by step approach to get your Raspberry Pi with low level electronics hardware control. Make simple, step by step

Raspberry Pi Resources-Using the UART

参考:RPi Serial Connection 本文来自:http://www.raspberry-projects.com/pi/programming-in-c/uart-serial-port/using-the-uart Using the UART If you are running Raspbian or similar then the UART will be used as a serial console.  Using a suitable cable, such as

Raspberry Pi 3 with Openwrt

https://wiki.openwrt.org/toh/raspberry_pi_foundation/raspberry_pi#boot_log_raspberry_pi_3 Table of Contents Supported Versions Hardware Specifications Raspberry Pi SoCs Raspberry Pi Models Using I2C and SPI Power Notes Serial Boot Logs Boot Log (Rasp

Raspberry Pi GPIO Protection

After damaging the GPIO port on our raspberry pi while designing a new solar monitoring system we decided that it might be a good idea to build a buffer/level translator to protect the GPIO pins from over voltage and ESD spikes.  The board would need

Raspberry PI硬件

Raspberry Pi硬件 1.目前树莓派的型号(根据处理器分) 第一代 Model A Model B Model A+ Model B+ ZERO 第二代 Model B 第三代 Model B 还有一个Raspberry Pi Compute Module 第二代是不是叫Raspberry Pi 2B啊 图片 Raspberry Pi 1 Model A+ Raspberry Pi 1 Model B v1.0 正面 Raspberry Pi 1 Model B v1.0 背面 Rasp

VNC connect to raspberry pi under ubuntu desktop environment

1 使用Remmina Remote Desktop ubuntu 14.04自带一款远程桌面叫作 Remmina Remote Desktop 利用它即可方便打开已经开启VNC server的raspberry pi 如图,已经新建好了一个raspberry的链接. 新建服务链接 填写链接name 选择链接所使用protocol,注意要选VNC 填写server的地址 填写登录user name以及password 如图: 2 使用SSL/SSH VNC Viewer 下载vnc viewer

Adding an On/Off switch to your Raspberry Pi

http://www.raspberry-pi-geek.com/Archive/2013/01/Adding-an-On-Off-switch-to-your-Raspberry-Pi#article_f5 Which Switch? Aaron Shaw Pulling the plug on your Pi without an orderly shutdown can corrupt the SD card. Also, many users prefer a convenient sw

Raspberry pi,一个好玩的派:第五季 无线网卡

Raspberry pi的板子由于成本原因是没有加无线模块的,不想被网线束缚的我们,需要自备USB无线模块.在购买板子时,看见官方推荐EDUP无线网卡,价格还算合适,就直接入手了. 采用REALTEK8188芯片,802.11n,传输速度150Mbps,适用范围130平方米. 将其插到任一U口即可,如下图: 由于外壳阻碍了电源插孔,所以只好先裸着了,图中已经加电,HDMI的另一头是电视机. 接下来的任务就是如何让这个无线网卡工作,连接到我已经开启的无线路由器. 一.wpa_gui 在进入Rasp