pySerial
Overview
This module encapsulates the access for the serial port. It provides backends for Python running on Windows, Linux, BSD (possibly any POSIX compliant system), Jython and IronPython (.NET and Mono). The module named “serial” automatically selects the appropriate backend.

It is released under a free software license, see LICENSE.txt for more details.
(C) 2001-2008 Chris Liechti cliechti@gmx.net

The project page on SourceForge and here is the SVN repository and the Download Page .
The homepage is on http://pyserial.sf.net/

Features
same class based interface on all supported platforms
access to the port settings through Python 2.2+ properties
port numbering starts at zero, no need to know the port name in the user program
port string (device name) can be specified if access through numbering is inappropriate
support for different bytesizes, stopbits, parity and flow control with RTS/CTS and/or Xon/Xoff
working with or without receive timeout
file like API with “read” and “write” (“readline” etc. also supported)
The files in this package are 100% pure Python. They depend on non standard but common packages on Windows (pywin32) and Jython (JavaComm). POSIX (Linux, BSD) uses only modules from the standard Python distribution)
The port is set up for binary transmission. No NULL byte stripping, CR-LF translation etc. (which are many times enabled for POSIX.) This makes this module universally useful.

Requirements
Python 2.2 or newer
pywin32 extensions on Windows
“Java Communications” (JavaComm) or compatible extension for Java/Jython

Installation

from source
Extract files from the archive, open a shell/console in that directory and let Distutils do the rest:
python setup.py install

The files get installed in the “Lib/site-packages” directory.

easy_install
An EGG is available from the Python Package Index: http://pypi.python.org/pypi/pyserial
easy_install pyserial

windows installer
There is also a Windows installer for end users. It is located in the Download Page
Developers may be interested to get the source archive, because it contains examples and the readme.

Short introduction
Open port 0 at “9600,8,N,1”, no timeout

.text .imp { font-weight: bold; color: red; }
[text] view plain copy

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
.text .imp { font-weight: bold; color: red; }
[text] view plain copy
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
.text .imp { font-weight: bold; color: red; }
[text] view plain copy
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
.text .imp { font-weight: bold; color: red; }
[text] view plain copy
ser = serial.Serial()
ser.baudrate = 19200
ser.port = 0
ser
Serial

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
xonxoff=0, # enable software flow control
rtscts=0, # enable RTS/CTS flow control
interCharTimeout=None # Inter-character timeout, None to disable
)
The port is immediately opened on object creation, if a port is given. It is not opened if port is None.
Options for read timeout:
.text .imp { font-weight: bold; color: red; }
[text] view plain copy
timeout=None # wait forever
timeout=0 # non-blocking mode (return immediately on read)
timeout=x # set timeout to x seconds (float allowed)

Methods of Serial instances
.text .imp { font-weight: bold; color: red; }
[text] view plain copy
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

Attributes of Serial instances
Read Only:
.text .imp { font-weight: bold; color: red; }
[text] view plain copy
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
New values can be assigned to the following attributes, the port will be reconfigured, even if it’s opened at that time:

.text .imp { font-weight: bold; color: red; }
[text] view plain copy
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

Exceptions
.text .imp { font-weight: bold; color: red; }
[text] view plain copy
serial.SerialException

Constants
parity:
.text .imp { font-weight: bold; color: red; }
[text] view plain copy
serial.PARITY_NONE
serial.PARITY_EVEN
serial.PARITY_ODD
stopbits:
.text .imp { font-weight: bold; color: red; }
[text] view plain copy
serial.STOPBITS_ONE
al.STOPBITS_TWO
bytesize:
.text .imp { font-weight: bold; color: red; }
[text] view plain copy
serial.FIVEBITS
serial.SIXBITS
serial.SEVENBITS
serial.EIGHTBITS

pySerial -- Python的串口通讯模块相关推荐

  1. python串口模块_Python使用pip安装pySerial串口通讯模块

    pySerial封装了对串口的访问,供大家参考,具体内容如下 特性 在支持的平台上有统一的接口. 通过python属性访问串口设置. 支持不同的字节大小.停止位.校验位和流控设置. 可以有或者没有接收 ...

  2. 使用pip安装pySerial串口通讯模块

    提示错误: Pyserial is not installed for D:\Professional_Software\Python27\python.exe. Check the README f ...

  3. Linux下的Ubuntu16.04系统配置并使用USB转串口(串口转USB),最终使用python的serial和pyserial包实现串口的打开并读取数据

    1. USB转串口的配置 1.1 首先使用 lsmod | grep usbserial 指令查看系统是否包含USB转串口驱动,如果没有信息输出不代表没有驱动,我这边就是没有信息输出,且看后面分析: ...

  4. python 用pyserial模块通过串口发送数据的注意点

    最近开始在工作中开始边学边用python,其中需要用python实现串口读写,在编程调试过程中发现通过pyserial模块的write方法发送字符串时,数据并没有被发送去到串口设备中,而是停留在了输出 ...

  5. Python的串口通信(pyserial)

    串口通信是指外设和计算机间,通过数据信号线 .地线.控制线等,按位进行传输数据的一种通讯方式.这种通信方式使用的数据线少,在远距离通信中可以节约通信成本,但其传输速度比并行传输低.串口是计算机上一种非 ...

  6. 使用 pip 安装serial 串口通讯模块

    pySerial封装了对串口的访问. 特性 在支持的平台上有统一的接口. 通过python属性访问串口设置. 支持不同的字节大小.停止位.校验位和流控设置. 可以有或者没有接收超时. 类似文件的API ...

  7. python 串口_如何使用Python开发串口通讯上位机(一)

    用Python开发串口通讯型上位机,其实并非最优解,本系列更新只为个人学习与总结.如果有C语言底子,嵌入式层面的上位机开发,C++ Builder,C#才是更加好用的利器. 1 什么是上位机 从事过嵌 ...

  8. python树莓派串口通信实例_树莓派通过串口发送数据

    可以通过串口登陆树莓派,也可以通过串口向其他主机发送数据.树莓派的串口接受数据科技爱好者博客已经写过,可以参考这篇文章:树莓派与arduino串口通信实践.这篇文章教大家如果通过树莓派的串口发送数据. ...

  9. 【开源】一款PyQT+Pyserial开发的串口调试工具

    [开源]PyQT+Pyserial开发的串口调试工具 串口调试工具是我们做嵌入式开发常用的工具,市面上已经有很多串口调试工具了,博主写这款串口调试工具一方面是为了学习Python PyQT Pyser ...

最新文章

  1. Debian 系统初体验
  2. (六)Javascript 规范
  3. [深度学习] FM FFM 算法基本原理
  4. css里面的位置属性,CSS定位属性Position详解
  5. Python中 模块、包、库
  6. 实时获取浏览器滚动条高度(兼容写法)
  7. kops_使用KOPS的Kubernetes群集中SQL Server
  8. ueditor滚动条
  9. python右对齐函数_Python这68个内置函数,建议你吃透!
  10. 首都师范大学计算机技术复试分数线,2020年首都师范大学计算机应用技术考研分析...
  11. 嗅探工具java_权宜之计 让迅雷5支持傲游2嗅探器来下载视频
  12. python爬网页html乱码问题
  13. TSNE 高维数据可视化
  14. 家有千金之冰糖绿豆汤
  15. LINUX IIO子系统分析之二 IIO子系统相关数据结构分析
  16. 如何统计excel中的sheet数量
  17. 网络爬虫--记一次有趣的侦察型反爬手段(ebay)
  18. excel输入公式不计算结果
  19. java匹配字段返回成功,Java MongoTemplate查询返回指定字段及指定数量的数据
  20. WPF(七) Prism框架基本特性

热门文章

  1. python ftp 设置代理_用Python搭建一个简单的代理池
  2. 仿个人税务 app html5_【注意】你下载的可能是个假的个税App
  3. python alter table_python(pymysql)之mysql简单操作
  4. linux 版本_Linux动态库版本号作用机制
  5. arcgis字段计算器利用python按不同两列数据进行编号
  6. 神经网络与深度学习——TensorFlow2.0实战(笔记)(五)(NumPy科学计算库<1>python)
  7. Sharepoint学习笔记 –架构系列—11 Sharepoint的服务器端对象模型(Server Object Model) 3.服务层次结构
  8. java中的jgroup_JGroups实现聊天小程序
  9. Spark-自定义累加器-进行字符串拼接(代码及详细实现步骤)
  10. 50个最有用的Matplotlib数据分析与可视化图