python serial读串口

来源:互联网 发布:租号平台源码 编辑:程序博客网 时间:2024/05/11 03:31

由于项目需求,需要从传感器节点和GPS中读取数据,所以学习了如何用python读串口。

python提供了pySerial,Linux和windows下有相应的模块

linux版本: pyserial (http://pyserial.wiki.sourceforge.net/pySerial)   (在我的虚拟机中Ubuntu 12.04 LTS 版本中好像已经自带)

windows版本: pywin32 (http://sourceforge.net/projects/pywin32/) 下载直接安装

先看一个例子:

import serialser = serial.Serial('/dev/ttyTX0', 9600, timeout=1)while True:    data = ser.read(100)    print repr(data)


1、读串口步骤:

实例化 --> 设置 (当然可以一步完成)

2、serial详解

2.1 serial类原型
ser = serial.Serial(
port=None,              # number of device, numbering starts at
# zero. if everything fails, the user
# can specify a device string, note
# that this isn't portable anymore
# if no port is specified an unconfigured
# an closed serial port object is created
baudrate=9600,          # baud rate
bytesize=EIGHTBITS,     # number of databits
parity=PARITY_NONE,     # enable parity checking
stopbits=STOPBITS_ONE,  # number of stopbits
timeout=None,           # set a timeout value, None for waiting forever
                              #zero, non-blocking mode, return immediately on read 
xonxoff=0,              # enable software flow control
rtscts=0,               # enable RTS/CTS flow control
interCharTimeout=None   # Inter-character timeout, None to disable
)
注;Port可以用数字表示。COM1为 0,COM2为1 .......
也可以实用绝对路径:ser.port = '/dev/ttySAc2' 
timeout说明(None和 0不同):
timeout = None: 长时间等待
timeout = 0: 不阻塞形式 (读完之后就返回)
timeout = x: x秒后超时 (float allowed)


2.2 Serial方法:
open()                  # open port
close()                 # close port immediately
setBaudrate(baudrate)   # change baud rate on an open port
inWaiting()             # return the number of chars in the receive buffer
read(size=1)            # read "size" characters
write(s)                # write the string s to the port
flushInput()            # flush input buffer, discarding all it's contents
flushOutput()           # flush output buffer, abort output
sendBreak()             # send break condition
setRTS(level=1)         # set RTS line to specified logic level
setDTR(level=1)         # set DTR line to specified logic level
getCTS()                # return the state of the CTS line
getDSR()                # return the state of the DSR line
getRI()                 # return the state of the RI line
getCD()                 # return the state of the CD line

2.3 Serial 实例属性
portstr                 # device name
BAUDRATES               # list of valid baudrates
BYTESIZES               # list of valid byte sizes
PARITIES                # list of valid parities
STOPBITS                # list of valid stop bit widths
以下属性可以改变,即使在端口打开时也可以重新赋值
port                    # port name/number as set by the user
baudrate                # current baud rate setting
bytesize                # byte size in bits
parity                  # parity setting
stopbits                # stop bit with (1,2)
timeout                 # timeout setting
xonxoff                 # if Xon/Xoff flow control is enabled
rtscts                  # if hardware flow control is enabled

3.简单实例

import serialser = serial.Serial()def hexShow(argv):    result = ''    hLen = len(argv)    for i in xrange(hLen):        hvol = ord(argv[i])        hhex = '%02x'%hvol        result += hhex+' '        print 'hexShow:',resultser.baudrate = 57600ser.port = '/dev/ttySAC2'ser.open()readstr = ser.read(20)print ser.portstrhexShow(readstr)

执行结果如图:


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 利仁分体电饼铛做饼熟的慢怎么办 闲鱼买的东西自提有问题怎么办 老板油烟机的表层的膜掉了怎么办 给老板打工老板跑路了怎么办 公司要业务员承担客户的欠款怎么办 从自己公司出去抢自己客户怎么办? 在苏宁买东西买贵了怎么办 苏宁购物小票不见了未提货怎么办 结痂的地方扣掉了颜色不一样怎么办 实体店买手机不给发票怎么办 小孩学英语字母怎么都记不住怎么办 聊客老是提示网络连接失败怎么办? 天猫购物漏发客服不理怎么办 京东二维码被骗怎么办联系客服号码 京东客服打电话来让自己退款怎么办 淘宝上买到的衣服是坏的怎么办 淘宝上买到的产品是坏的怎么办? 在淘宝买到东西是坏的怎么办 冰箱冷冻室门生锈变形关不严怎么办 诲信电冰箱电脑板坏了怎么办 冰箱里放了热水后就不制冷了怎么办 双温冰柜冷藏矿泉水不冰怎么办 美的电饭煲e一传感器也没坏怎么办 美的电饭煲不工作显示C3怎么办 美的电饭煲啪一声响不工作了怎么办 误给宝宝吃了坏的饭怎么办 鼠标没反应键盘指示灯不亮怎么办 新买变频冰箱风机声音大怎么办 三星手机玩王者荣耀一直闪退怎么办 刚申请的阿里大宝卡不想要了怎么办 国美刚买不到一个月电视坏了怎么办 交保险后保险公司不给开收据怎么办 收据白联作废红联丢失怎么办 收据作废客户联给客户了怎么办 宜家买的床和床垫搬家了怎么办 科龙空调开机后自己关机怎么办 以旧换新旧的没给商家 报案怎么办 想换新手机但是旧的没坏怎么办 从苏宁易购买的电视坏了怎么办 苏宁易购服务站买的电视坏了怎么办 用微信登陆京东账号退不出来怎么办