pySerial -- Python的串口通讯模块
来源:互联网 发布:叮叮当软件下载 编辑:程序博客网 时间:2024/05/17 23:06
pySerial -- Python的串口通讯模块
介绍
pySerial
封装了串口通讯模块,支持Linux、Windows、BSD(可能支持所有支持POSIX的操作系统),支持Jython(Java)和IconPython(.NET and Mono).
首页 http://pyserial.sf.net/
特性
- 所有平台使用同样的类接口
- 端口号默认从0开始,程序中不需要知道端口名称
- 像文件读写一样的API,
read
、write
(readline
等也受支持) - 所有程序全由Python完成,除了标准库外不依赖其他包,除了pywin32(windows)、JavaComm(Jython). POSIX(Linux, BSD) 只依赖Python标准库。
依赖环境
- Python2.2或更新版本
- windows 上的 pywin32扩展
- Java/Jython上的 "Java Communications" (JavaComm)或者兼容包
安装
pip/easy_install
pip install pyserial easy_install pyserial
windows
下载地址 : http://sourceforge.net/project/showfiles.php?group_id=46487
快速上手
Open port 0 at "9600,8,N,1", no timeout
>>> import serial >>> ser = serial.Serial(0) # open first serial port >>> print ser.portstr # check which port was really used >>> ser.write("hello") # write a string >>> ser.close() # close port
Open named port at "19200,8,N,1", 1s timeout
>>> ser = serial.Serial('/dev/ttyS1', 19200, timeout=1) >>> x = ser.read() # read one byte >>> s = ser.read(10) # read up to ten bytes (timeout) >>> line = ser.readline() # read a '/n' terminated line >>> ser.close()
Open second port at "38400,8,E,1", non blocking HW handshaking
>>> ser = serial.Serial(1, 38400, timeout=0, ... parity=serial.PARITY_EVEN, rtscts=1) >>> s = ser.read(100) # read up to one hundred bytes ... # or as much is in the buffer
Get a Serial instance and configure/open it later
>>> ser = serial.Serial() >>> ser.baudrate = 19200 >>> ser.port = 0 >>> ser Serial<id=0xa81c10, open=False>(port='COM1', baudrate=19200, bytesize=8, parity='N', stopbits=1, timeout=None, xonxoff=0, rtscts=0) >>> ser.open() >>> ser.isOpen() True >>> ser.close() >>> ser.isOpen() False
如果给定端口,端口将在创建对象之后立即打开,如果没有给定端口,可选timeout
参数
timeout=None # wait forever timeout=0 # non-blocking mode (return immediately on read) timeout=x # set timeout to x seconds (float allowed)
Serial实例的可用方法
open() # 打开端口close() # 立即关闭端口 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) # 把字符串s写到该端口 flushInput() # 清除输入缓存区,放弃所有内容flushOutput() # 清除输出缓冲区,放弃输出 sendBreak() # 发送中断条件 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
Serial实例的属性
只读
portstr # 设备名称 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
异常
serial.SerialException
常量
parity:
serial.PARITY_NONE serial.PARITY_EVEN serial.PARITY_ODD
stopbits
serial.STOPBITS_ONE al.STOPBITS_TWO
bytesize:
serial.FIVEBITS serial.SIXBITS serial.SEVENBITS serial.EIGHTBITS
翻译(有删减)仅供参考
原文地址:http://blog.csdn.net/dainiao01/article/details/5885122 官方文档:http://pyserial.sf.net/
0 0
- pySerial -- Python的串口通讯模块
- pySerial -- Python的串口通讯模块
- 串口通讯的python模块——pySerial
- 串口通讯的python模块——pySerial
- 串口通讯的python模块——pySerial
- python串口通讯模块pySerial使用随笔
- Python串口模块pySerial
- Python串口模块pySerial
- 【Python入门】 使用 pip 安装 pySerial 串口通讯模块
- python串口通信模块——pySerial
- python pyserial 串口
- Python3.2----串口模块Pyserial
- python串口通信,pyserial安装
- python中pyserial模块使用方法
- 树莓派+Python+pyserial 2.7实现串口通信
- Python通过pyserial控制串口操作
- Python使用pyserial进行串口通信
- 树莓派+Python+pyserial 2.7实现串口通信
- DBUtils使用详解二
- Java安全通信:HTTPS与SSL
- 函数指针的使用
- PHP实现简单的万年历(实例)
- Live555学习笔记(一)—— live555概述
- pySerial -- Python的串口通讯模块
- BUtils使用详解一
- 利用正则表达查找文件
- 关于acm专题的相关资料下载
- 深度学习(三十九)可视化理解卷积神经网络(2.0)
- PAT 1007. Maximum Subsequence Sum (25)
- 你可能不了解java接口
- java安全框架-Shiro学习笔记(二)-身份认证
- 复印curl学习记录