Pyserial操作串口
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/
Pyserial操作串口的更多相关文章
- C#操作串口总结
Technorati 标签: C#,SerialPort,ReadTo,ReadTimeout 最近几天一直在调一个要长时间连续不断的操作串口,并且是多线程运行,不允许中断的服务.后来服 ...
- MSComm控件与Win32 API操作串口有何区别?
MSComm控件与Win32 API操作串口有何区别? [问题点数:50分,结帖人shell_shell] 收藏帖子 回复 我是一个小兵,在战场上拼命! 结帖率 83.33% 我以前用MSCo ...
- Raspberry pi 使用python+pySerial实现串口通信(转)
Raspberry pi 使用python+pySerial实现串口通信 转:http://blog.csdn.net/homeway999/article/details/8642353 目录( ...
- android开发(37) android使用android_serialport_api 操作串口,解决权限问题
最近有个项目,要使用android设备操作串口的 斑马GK888T打印机,使用打印机打印二维码. 硬件设备连接方式: 安卓设备 通过 串口RS232 连接 斑马打印机的串口 那么就要解决:使用安卓设备 ...
- pyserial库-串口通讯模块
pySerial 封装了串口通讯模块,支持Linux.Windows.BSD(可能支持所有支持POSIX的操作系统),支持Jython(Java)和IconPython(.NET and Mono). ...
- CE 操作串口
WinCE里面都是通过标准的系统API对串口进行操作的,但是串口不同于其它文件,它是是独占式地操作的. 下面是一个操作的串口类: #pragma once typedef void (*LPDataA ...
- 通过shell操作串口
1. 通过stty工具设置串口参数,例如 stty -F /dev/ttyUSB0 raw speed 9600 -echo min 0 time 10 上例设置了ttyUSB0设备的数据流格式为ra ...
- python操作串口
import serial test = serial.Serial("COM1",115200)#这里就已经打开了串口 print(test.portstr) test.writ ...
- C# 如何操作串口
1.首先要引用 System.IO.Ports using System; using System.Collections.Generic; using System.ComponentModel ...
随机推荐
- C#Linq技术中SelectMany(...)的内部实现推测
对于声明为:public static IEnumerable<TResult> SelectMany<TSource, TResult>(this IEnumerable&l ...
- 完全卸载AndroidStudio
一:卸载Android Studio 由于从1.5正式版直接升级到2.1的版本,整个项目构建都变得异常的慢,所以决定卸载重新安装2.0的正式版.但是Mac下使用dmg安装的app很多都是不能使用拖拽的 ...
- Oracle 之SQL_Loader
SQL*LOADER是ORACLE的数据加载工具,通常用来将操作系统文件迁移到ORACLE数据库中.SQL*LOADER是大型数据仓库选择使用的加载方法,因为它提供了最快速的途径(DIRECT,PAR ...
- CTSC是啥
洛谷看到一题的难度NOI/NOI+/CTSC 百度一下 CTSC (China Team Selection Competition)为国际信息学奥林匹克竞赛(International Olympi ...
- 2003服务器搭建vpn
先说下为什么会有本文,公司的git突然被防火墙屏蔽了,有些同事无奈用4g网去提交,我比较穷,1g的小水管hold不住,于是我想着用vpn.国内封杀的比较严重,免费的更是少,找朋友要了一个3小时试用的, ...
- Jetpack 由 WordPress.com 出品
官网:https://jetpack.com/ Jetpack 由 WordPress.com 出品. Jetpack 通过为您提供访客统计数据和安全服务.加速图像传输以及帮您获得更多浏览量,可以简化 ...
- [leetcode] 提醒整理之进制
12. Integer to Roman Given an integer, convert it to a roman numeral. Input is guaranteed to be with ...
- CentOS安装JDK和安装Glassfish
1.首先下载对应CentOS版本的jdk:下载:jdk-7u75-linux-x64.tar.gz 2.下载该jdk到本地,并上传到CentOS系统的opt临时目录下 3.在安装自己下载的jdk之前, ...
- AJAX 同步异步笔记
就在刚才,做一个很简单的Demo, 预览MP4视频文件 这就是一个video标签嘛,然后再动态的给src赋值嘛.这还不是so easy? 好,说做就做.先简单的测试一下.先给src赋值一下. 嗯,可以 ...
- gradle下载地址
gradle下载:http://services.gradle.org/distributions