参考: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的更多相关文章

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

    Raspberry pi 使用python+pySerial实现串口通信 转:http://blog.csdn.net/homeway999/article/details/8642353   目录( ...

  2. RASPBERRY PI 外设学习资源

    参考: http://www.siongboon.com/projects/2013-07-08_raspberry_pi/index.html Raspberry Pi         Get st ...

  3. Raspberry Pi Resources-Using the UART

    参考:RPi Serial Connection 本文来自:http://www.raspberry-projects.com/pi/programming-in-c/uart-serial-port ...

  4. Raspberry Pi上手

    2013-05-21 买的树莓派终于到手了,嘿嘿.我在官方代理ICKEY买的,是英国版,B型. 上手教程可以根据Getting Started with Raspberry Pi(网上有电子版免费下载 ...

  5. Raspberry Pi GPIO Protection

    After damaging the GPIO port on our raspberry pi while designing a new solar monitoring system we de ...

  6. 让Mono 4在Raspberry Pi上飞

    最近公司有项目想要在树莓派上做,代替原来的工控机(我们是把工控主机当作小的主机用,一台小的工控主机最少也要600左右,而树莓派只要200多).于是,公司买了一个Raspberry Pi B+和一个Ra ...

  7. Kali v2.1.2 for Raspberry Pi 3B

    最新的下载地址是: https://www.offensive-security.com/kali-linux-arm-images/ 按照官网的说法是找不到树莓派版本的SHA1SUM和SHA1SUM ...

  8. 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 ...

  9. Windows Iot:让Raspberry Pi跑起来(1)

    首先请大家原谅我的"不务正业",放着RabbitHub不写,各种系列的文章不写搞什么Iot,哈哈,最近心血来潮想搞个速度极快的遥控车玩,望着在角落的Raspberry Pi恶狠狠的 ...

随机推荐

  1. python进阶(四)---需要了解的魔法方法

    以下内容,源于个人理解所得,纯属臆测,爱信不信:-D.欢迎大家留言讨论指正. 1.__new__魔法方法: 原型:__new__(cls, *args, **kwargs) 说明:__new__魔法方 ...

  2. css清除浮动float的三种方法总结,为什么清浮动?浮动会有那些影响?一起来$('.float')

    一.抛一块问题砖(display: block)先看现象: 分析HTML代码结构: <div class="outer">     <div class=&quo ...

  3. git备份sublime插件及配置

    github备份sublime配置 sublime使用的时间长了,渐渐的就积累了一些有用甚至离不开的插件.但是有时候系统会出点问题,或者换电脑什么的,这时候要想在找回那个曾经的sublime就不那么容 ...

  4. PHP获取当前时间戳,当前时间、及解决时区问题

    一.获取当前时间戳 方法1:通过time函数 time(); 方法2:通过$_SERVER中的REQUEST_TIME元素 $_SERVER['REQUEST_TIME']; 方法3:通过strtot ...

  5. git hub

    如果希望保留生产服务器上所做的改动,仅仅并入新配置项, 处理方法如下:git stashgit pullgit stash pop然后可以使用git diff -w +文件名 来确认代码自动合并的情况 ...

  6. SAP采购订单历史明细报表源代码(自己收藏)

    SPAN { font-family: "Courier New"; font-size: 10pt; color: #000000; background: #FFFFFF } ...

  7. a.redhat系统如何卸载默认jdk

    Redhat系统安装之后,会默认有openjdk在安装(下图已经是卸载掉了) 这个openjdk下面的需要进行卸载,你可以通过命令进行卸载,例如首先查看JDK,然后默认直接通过rpm命令进行卸载L 卸 ...

  8. 可以结合react的ui组件

    https://ant.design/components/switch-cn/

  9. unity调用摄像头的方法

    http://blog.csdn.net/cocoa_china/article/details/10527995 using UnityEngine; using System.Collection ...

  10. AX2012 XppCompiler create method动态创建方法并运行

    在用友系列的软件中经常可以看到可配置的计算公式,AX中其实也有可配置的公式,如call center呼叫中心的欺诈Fraud rule的配置,AX后台可以根据配置规则,变量,条件来动态产生方法并执行, ...