参考: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. Android中文TTS

    如今在Android中开发中文语音播报有各式各样的云服务.SDK.API等云云,但是大部分服务是需要联网支持来进行语音合成的,在中文语音合成方面,科大讯飞无疑是佼佼者,而且它也提供了离线语音合成包(需 ...

  2. weex 小结--内建模块

    使用以下模块时,需要导入依赖:var *** = require('@weex-module/****'); 1. navigator --像浏览器一样切换页面 2. webview(module) ...

  3. xml报文解析和组装

    package com.xjts.cipher.util;import java.io.File;import java.io.FileWriter;import java.io.IOExceptio ...

  4. Android实现帧动画,以及出场时的动画

    最近有个小需求,在数据上传的时候加一个上传的动画,然后就寻思着自己写一个帧动画 上传开始的时候调用动画,上传结束通知容器将其删除(这个方法应该不会太耗内存),然后吐槽下gif图片还是我自己一帧一帧从p ...

  5. ngui中 代码调用按钮事件(后来改成了按钮绑定键盘..)

    ngui中 代码调用按钮事件 好烦人啊这个问题, 我弄完发上来 这个问题解决了一半 发现可以用 按钮绑定来解决这个问题,并且更安全方便快速 直接在按钮上添加一个 key binding 指定按键 搞定 ...

  6. 日期时间组件 - layui.laydate

    全部参数 一.核心方法:laydate(options); options是一个对象,它包含了以下key: '默认值' { elem: '#id', //需显示日期的元素选择器 event: 'cli ...

  7. ExtJS自制表格Grid分页条

    试过Grid自带的load和分页功能,没有成功,干脆就自己写了...... 主要是查询条件比较复杂...... 希望哪位大神能有更好的意见. Ext.define('MyApp.ux.Paginati ...

  8. 关于codeblock中一些常用的快捷键(搬运)

    关于codeblock中一些常用的快捷键(搬运) codeblock作为一个常用的C/C++编译器,是我最常用的一款编译器,但也因为常用,所以有时为了更加快速的操作难免会用到一些快捷键,但是因为我本身 ...

  9. call 和 apply使用

      call 和 apply 都是为了改变某个函数运行时的 context 即上下文而存在的,换句话说,就是为了改变函数体内部 this 的指向.因为 JavaScript 的函数存在「定义时上下文」 ...

  10. thinkPHP 标题

    在控制器中 $this->meta_title='标题'; 在对应模板中 {$meta_title} 可以这样对模板中的变量直接进行赋值