`
xusaomaiss
  • 浏览: 615597 次
  • 性别: Icon_minigender_1
  • 来自: 厦门
社区版块
存档分类
最新评论

串口通讯的python模块——pySerial

阅读更多

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
<!-- /* GeSHi (c) Nigel McNie 2004 (http://qbnz.com/highlighter) */ .text .imp {font-weight: bold; color: red;} -->
>>> 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
<!-- /* GeSHi (c) Nigel McNie 2004 (http://qbnz.com/highlighter) */ .text .imp {font-weight: bold; color: red;} -->
>>> 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
<!-- /* GeSHi (c) Nigel McNie 2004 (http://qbnz.com/highlighter) */ .text .imp {font-weight: bold; color: red;} -->
>>> 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
<!-- /* GeSHi (c) Nigel McNie 2004 (http://qbnz.com/highlighter) */ .text .imp {font-weight: bold; color: red;} -->
>>> 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
Be carefully when using "readline". Do specify a timeout when opening the serial port otherwise it could block forever if no newline character is received. Also note that "readlines" only works with a timeout. "readlines" depends on having a timeout and interprets that as EOF (end of file). It raises an exception if the port is not opened correctly.
Do also have a look at the example files in the examples directory in the source distribution or online.

Examples

Please look in the SVN Repository. There is an example directory where you can find a simple terminal and more.
http://pyserial.svn.sourceforge.net/viewvc/pyserial/trunk/pyserial/examples/

Parameters for the Serial class

<!-- /* GeSHi (c) Nigel McNie 2004 (http://qbnz.com/highlighter) */ .text .imp {font-weight: bold; color: red;} -->
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
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:
<!-- /* GeSHi (c) Nigel McNie 2004 (http://qbnz.com/highlighter) */ .text .imp {font-weight: bold; color: red;} -->
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

<!-- /* GeSHi (c) Nigel McNie 2004 (http://qbnz.com/highlighter) */ .text .imp {font-weight: bold; color: red;} -->
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:
<!-- /* GeSHi (c) Nigel McNie 2004 (http://qbnz.com/highlighter) */ .text .imp {font-weight: bold; color: red;} -->
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:

<!-- /* GeSHi (c) Nigel McNie 2004 (http://qbnz.com/highlighter) */ .text .imp {font-weight: bold; color: red;} -->
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

<!-- /* GeSHi (c) Nigel McNie 2004 (http://qbnz.com/highlighter) */ .text .imp {font-weight: bold; color: red;} -->
serial.SerialException

Constants

parity:
<!-- /* GeSHi (c) Nigel McNie 2004 (http://qbnz.com/highlighter) */ .text .imp {font-weight: bold; color: red;} -->
    serial.PARITY_NONE
serial.PARITY_EVEN
serial.PARITY_ODD
stopbits:
<!-- /* GeSHi (c) Nigel McNie 2004 (http://qbnz.com/highlighter) */ .text .imp {font-weight: bold; color: red;} -->
    serial.STOPBITS_ONE
serial.STOPBITS_TWO
bytesize:
<!-- /* GeSHi (c) Nigel McNie 2004 (http://qbnz.com/highlighter) */ .text .imp {font-weight: bold; color: red;} -->
    serial.FIVEBITS
serial.SIXBITS
serial.SEVENBITS
serial.EIGHTBITS
分享到:
评论

相关推荐

    python串口模块 python开发 pyserial-2.5.win32.zip

    Python串口通信是Python编程中一个重要的组成部分,特别...这个库对于进行Python的串口开发来说是不可或缺的工具,尤其对于那些需要在Windows系统上进行串口通信的项目,Pyserial-2.5.win32版本是一个可靠的解决方案。

    python串口通信模块包--pyserial-3.4.tar.gz

    安装pyserial-3.4.tar.gz步骤:下载pyserial-3.4.tar.gz并解压,在安装python并将python添加到环境变量中后,打开命令提示符窗口切换到解压pyserial-3.4.tar.gz的目录路径,然后输入python setup.py install,即可...

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

    pySerial是一个强大的库,它为Python提供了跨平台的串口通信功能。本文将详细介绍如何使用pip来安装pySerial模块,以及pySerial的主要特性和使用方法。 首先,让我们来看看如何使用pip安装pySerial。pip是Python的...

    python开发的基于串口通讯的上位机应用

    本文将详细解析使用Python进行串口通信的上位机应用开发,结合给定的标签"python pyside2 pyserial",我们可以看到这个项目涉及到的技术栈。 首先,`Python`是一种高级编程语言,以其简洁的语法和丰富的库资源深受...

    python serial模块.zip

    Python的`serial`模块,通常称为`pyserial`,是一个非常重要的工具,它允许开发者在Python程序中与串行端口进行通信。这个模块适用于各种应用场景,包括嵌入式设备控制、硬件测试、数据采集等。`pyserial`库不仅支持...

    dw_pyserial_work.zip_python 硬件_python串口调试_python数据分析_上位机 python

    1. Python串口调试(pyserial库): Python的pyserial库是一个强大的工具,用于与各种串行设备进行通信。它允许开发者打开、配置和读写串口,适合于硬件调试。在"dw_pyserial_work.py"中,开发者可能利用pyserial的...

    python3+Tkinter+GUI界面+pyserial+串口+串口调试助手

    在这个项目中,我们将关注Python3在创建图形用户界面(GUI)和串口通信方面的应用。 Tkinter是Python的标准GUI库,它是与Tcl/Tk编程语言绑定的,为Python提供了一种简单且跨平台的方式来创建窗口应用。虽然Tkinter...

    pyserial-2.5.win32 python 串口模块

    本文将深入探讨pyserial-2.5.win32版本,这个专门为Windows系统设计的Python串口模块。 首先,让我们明确什么是串口通信。串口通信,也称为串行通信,是一种数据传输方式,通过单个数据线逐位传输信息。它常用于...

    由pyserial改编的VB6串口通讯类

    《VB6串口通讯类:基于pyserial与win32api的实现》 在信息技术领域,串口通信是一种常见的设备间数据传输方式,尤其在嵌入式系统和工业自动化中广泛应用。本文将深入探讨如何在VB6(Visual Basic 6)环境中,利用...

    python 实现RFID串口数据读取(csdn)————程序.pdf

    RFID技术是一种非接触式的自动识别技术,通过射频信号自动识别目标对象并获取相关数据,而Python中的pyserial模块则提供了方便的接口来操作串口通信。 **pyserial模块详解:** pyserial是Python的一个第三方库,...

    pyserial模块

    PySerial是Python编程语言中用于串行通信的一个重要模块,它使得在Python中控制串行端口变得极其简单。这个模块在Python 3.0及更高版本上运行良好,并且在Windows操作系统下,由于包含了`serialwin32.py`,我们无需...

    python的串口操作(pyserial)源代码

    python串口操作,使用python的pyserial实现,可进行hex转换发送,hex接收显示,如有不理解的地方,请参见博客:https://blog.csdn.net/absinjun/article/details/81407790

    Python使用pyserial进行串口通信的实例

    ### Python使用pyserial进行串口通信的实例 在本文中,我们将详细介绍如何使用Python与`pyserial`库实现串口通信。此实例不仅适合初学者快速上手,也能为有一定基础的开发人员提供深入理解的机会。 #### 一、...

    python3+qt4+pyserial 串口调试助手 串口波形显示

    总的来说,这个"python3+qt4+pyserial 串口调试助手 串口波形显示"项目结合了Python3的编程能力,Qt4的界面设计,以及pyserial的串口通信功能,提供了一个实用的工具,帮助开发者和工程师更高效地进行串口设备的调试...

    Serial2CSV_python字节数据_csv_串口_python_

    总的来说,“Serial2CSV_python字节数据_csv_串口_python_”这个项目展示了如何利用Python结合`pyserial`和`csv`库,实现串口数据的实时监控和存储。对于需要处理类似数据流的开发者,理解并学习这个脚本将大有裨益...

    python编写的串口调试工具

    在本文中,我们将深入探讨Python的串口通信库PySerial以及如何使用它来创建串口调试工具。 PySerial是Python的一个扩展模块,专门用于处理串行通信。它提供了丰富的功能,如打开和关闭串口、发送和接收数据、设置...

    陀螺仪角度输出——串口通讯

    对于Python,可以使用`pyserial`库。代码中还需包含读取和解析陀螺仪数据的逻辑。 4. **数据接收与解析**:当串口接收到数据时,根据JY-61的数据格式(例如ASCII或二进制)解码角度值。这可能涉及将接收到的字节...

    python2-pyserial-3.1.1-7.el7.noarch.rpm

    官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装

Global site tag (gtag.js) - Google Analytics