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 ...
随机推荐
- 51nod1183(Edit Distance)
题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1183 题意:中文题啦- 思路:dp 用dp[i][j]表示从 ...
- __clone()方法和传址区别
示例: <?php class Computer{ public $name = '联想'; public function _run(){ return '运行中'; } } $comp1 = ...
- Swift -运算符和循环结构
p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px Menlo; color: #4dbf56 } p.p2 { margin: 0.0px 0. ...
- Xcode 突然有部分文件不显示
今天突然Xcode抽风了,突然我的项目中的文件好多不显示出来,本地文件夹都有,见鬼了..... 真心坑死了,于是乎就找度娘了,果然被我发现了 看见这个蓝色小时钟没呀,就是他在捣鬼,我点击一下变成灰色就 ...
- asp.net(C#)读取文件夹和子文件夹下所有文件,绑定到GRIDVIEW并排序 .
Asp部分: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="MyFiles ...
- Maven依赖包下载慢--阿里云让你飞
当用maven下载依赖包的时候,用官方的镜像库,那慢的真是要死要死的.后来在网上搜到英国的库(也是慢的不行),国内的oschina更是直接没法下载呀.不过还好突然发现阿里云也有镜像库,尝试了以下,速度 ...
- ajax返回json数据,对其中日期的解析
JS 对其格式化 方法如下 function ChangeDateFormat(d){ //将时间戳转为int类型,构造Date类型 var date = new Date(parseInt(d.ti ...
- css箭头
.aui-list-item-arrow:before { content: ''; width: 0.4rem; height: 0.4rem; position: absolute; top: 5 ...
- EasyUi
<base href="<%=basePath %>"> -- (不推荐使用)--导入文件路径 ${pageContent.request.contextP ...
- Android -- PopupWindow(其中嵌套ListView 可以被点击)
1. 效果图