Raspberry Pi UART with PySerial
参考:http://programmingadvent.blogspot.hk/2012/12/raspberry-pi-uart-with-pyserial.html
Raspberry Pi UART with PySerial
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的更多相关文章
- Raspberry pi 使用python+pySerial实现串口通信(转)
		
Raspberry pi 使用python+pySerial实现串口通信 转:http://blog.csdn.net/homeway999/article/details/8642353 目录( ...
 - RASPBERRY PI 外设学习资源
		
参考: http://www.siongboon.com/projects/2013-07-08_raspberry_pi/index.html Raspberry Pi Get st ...
 - Raspberry Pi Resources-Using the UART
		
参考:RPi Serial Connection 本文来自:http://www.raspberry-projects.com/pi/programming-in-c/uart-serial-port ...
 - Raspberry Pi上手
		
2013-05-21 买的树莓派终于到手了,嘿嘿.我在官方代理ICKEY买的,是英国版,B型. 上手教程可以根据Getting Started with Raspberry Pi(网上有电子版免费下载 ...
 - Raspberry Pi GPIO Protection
		
After damaging the GPIO port on our raspberry pi while designing a new solar monitoring system we de ...
 - 让Mono 4在Raspberry Pi上飞
		
最近公司有项目想要在树莓派上做,代替原来的工控机(我们是把工控主机当作小的主机用,一台小的工控主机最少也要600左右,而树莓派只要200多).于是,公司买了一个Raspberry Pi B+和一个Ra ...
 - Kali v2.1.2 for Raspberry Pi 3B
		
最新的下载地址是: https://www.offensive-security.com/kali-linux-arm-images/ 按照官网的说法是找不到树莓派版本的SHA1SUM和SHA1SUM ...
 - A new comer playing with Raspberry Pi 3B
		
there are some things to do for raspberry pi 3b for the first time: 1, connect pi with monitor/KB/mo ...
 - Windows Iot:让Raspberry Pi跑起来(1)
		
首先请大家原谅我的"不务正业",放着RabbitHub不写,各种系列的文章不写搞什么Iot,哈哈,最近心血来潮想搞个速度极快的遥控车玩,望着在角落的Raspberry Pi恶狠狠的 ...
 
随机推荐
- Linux与user和group相关文件分析
			
/etc/passwd LOGNAME:PASSWORD::UID:GID:USERINFO:HOME:SHELL 注册名:口令:用户标识号:组标识号:用户名:用户主目录:命令解释程序 ()注册名(l ...
 - 被滥用的for in循环
			
众所周知,javascript中有两种for循环,一种是: var a=['this','is','a','article'], i, len; for( i = 0,len = a.length;i ...
 - 锁相关知识 & mutex怎么实现的 & spinlock怎么用的 & 怎样避免死锁 & 内核同步机制 & 读写锁
			
spinlock在上一篇文章有提到:http://www.cnblogs.com/charlesblc/p/6254437.html 通过锁数据总线来实现. 而看了这篇文章说明:mutex内部也用到 ...
 - Wiki设置
			
在Wiki安装完成后,就wiki会提示下载LocalSettings.php文件,这是wiki的设置文件,当我们要对wiki进行设置的时候,就需要用到这个文件. 下面对常用的操作设置做简要讲解: —— ...
 - PHPStorm技巧篇 -- 观感优化
			
(1)设置默认显示行号 (2)设置自动换行 (3)去除代码下划线(拼写检测) 优化说明:自动换行和显示行号字面意思很好理解,下划线说明一下,phpstorm默认对代码进行拼写校验,即对于不符合英文单词 ...
 - LinkedBlockingQueueE(示例,出错代码)
			
java.util.concurrent 类 LinkedBlockingQueue<E> java.lang.Object java.util.AbstractCollection< ...
 - linux建立一个快捷方式,连接到另一个目录
			
sudo ln -s 源目录 目标快捷方式比如你要在/etc下面建立一个叫LXBC553的快捷方式,指向/home/LXBC,那就是sudo ln -s /home/LXBC /etc/LXBC553
 - 阿里im即时通讯 h5 demo
			
适合不想装后台环境的同学,用nodejs搭建服务器. 以下是官网提供的node 请求示例: 找到了一个ali-top-sdk 代替topClient 于是请求示例代码如下: TopClient = r ...
 - 使用automake等命令自动生成Makefile文件   (转载)
			
使用automake等命令自动生成Makefile文件 Linux下编程时,为了方便编译,往往使用Makefile文件自动完成编译,但是Makefile文件本身的书写十分复杂,规则很多.好在Lin ...
 - android 单元测试
			
android studio 2.0创建一个空android moudle后, 会出现两个test目录,其中一个是Instrument Test, 另一个则是Unit Test Unit Test 和 ...