pySerial -- Python的串口通訊模塊

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

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/445504.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/445504.shtml
英文地址,請注明出處:http://en.pswp.cn/news/445504.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

串的堆分配實現

今天,線性結構基本就這樣了,以后(至少是最近)就很少寫線性基礎結構的實現了。 串的類型定義 typedef struct {char *str;int length; }HeapString; 初始化串 InitString(HeapString *S) {S->length0;S->str\0; } 長度 …

Numpy 入門

Numpy 入門 Numpy簡介 官網鏈接:http://www.numpy.org/NumPy是Python語言的一個擴充程序庫。支持高級大量的維度數組與矩陣運算,此外也針對數組運算提供大量的數學函數庫 Numpy的基本功能 快速高效的多維數組對象ndarray用于對數組執行元素級計算以…

數據結構課上筆記10

樹 樹的定義:樹(Tree)是 n(n≥0)個結點的有限集。若 n0,稱為空樹;若 n > 0,則它滿足如下兩個條件: (1) 有且僅有一個特定的稱為根 (Root) 的結點; (2) 其余結點可分為 m (m≥0) 個互不相交的有限…

pandasStudyNoteBook

pandas 入門培訓 pandas簡介 - 官網鏈接:http://pandas.pydata.org/ - pandas pannel data data analysis - Pandas是python的一個數據分析包 , Pandas最初被作為金融數據分析工具而開發出來,因此,pandas為時間序列分析提供了很好的支持 …

最大搜索子樹

給定一個二叉樹的頭結點,返回最大搜索子樹的大小。 我們先定義結點: public static class Node {public int value;public Node left;public Node right;public Node(int data) {this.value data;}} 分析: 直接判斷每個節點左邊小右邊大是…

二叉樹最長路徑

分析: 暴力求每一段距離也可。 對于以本節點為根的二叉樹,最遠距離有三種可能: 1)最遠路徑來自左子樹 2 )最遠路徑來自右子樹(圖示與左子樹同理) 3)最遠路徑為左右子樹距離根最遠…

判斷完全二叉樹

完全二叉樹的定義: 一棵二叉樹,除了最后一層之外都是完全填充的,并且最后一層的葉子結點都在左邊。 https://baike.baidu.com/item/%E5%AE%8C%E5%85%A8%E4%BA%8C%E5%8F%89%E6%A0%91/7773232?fraladdin 百度定義 思路:層序遍歷二叉樹 如果…

判斷二叉搜索樹

二叉查找樹(Binary Search Tree),(又:二叉搜索樹,二叉排序樹)它或者是一棵空樹,或者是具有下列性質的二叉樹: 若它的左子樹不空,則左子樹上所有結點的值均小于…

劍指offer_01

文章目錄[toc]第一章 面試流程1.1 面試官談面試1.2 面試3種形式1.3 面試的3個環節第一章 面試流程 1.1 面試官談面試 初級的程序員談算法和數據結構,高級的程序員談項目經驗要對公司近況和項目情況了解不要緊張,不要馬上上手寫代碼 1.2 面試3種形式 …

判斷平衡二叉樹

平衡二叉樹(Balanced Binary Tree)具有以下性質:它是一棵空樹或它的左右兩個子樹的高度差的絕對值不超過1。并且左右兩個子樹都是一棵平衡二叉樹 (不是我們平時意義上的必須為搜索樹) 判斷一棵樹是否為平衡二叉樹&am…

劍指offer_02

文章目錄第二章 面試需要的基礎知識1.1 面試官談基礎知識1.2 編程語言1.3 數據結構1.4 算法和數據操作第二章 面試需要的基礎知識 1.1 面試官談基礎知識 數據結構和算法,編程能力,部分數學能力,問題分析和推理能力編程基礎,計算…

求完全二叉樹的結點個數

第一次見這個題,看時間小于O(N)。。。。。 只能是二分啊。 但是怎么二分,條件是什么,真的想不到。 后來知道了,我們要找最深一層最右邊那個結點。借此確定結點個數。 我們知道,滿二叉樹的結點個數和深度是有公式的&a…

劍指offer_03

文章目錄第三章 高質量代碼1.1 面試官談高質量代碼1.2 代碼的規范性1.3 代碼的完整性1.4 代碼的魯棒性第三章 高質量代碼 1.1 面試官談高質量代碼 代碼應該考慮異常狀況和垃圾回收問題,不能忽視邊界情況變量,函數命名應該要統一,備注要恰到…

劍指offer_04

文章目錄第四章 解決面試題的思路1.1 面試官談面試思路1.2 畫圖讓問題抽象化1.3 舉例讓抽象問題具體化1.4 分解讓復雜問題具體化第四章 解決面試題的思路 1.1 面試官談面試思路 編程前講自己的思路是一項考核指標,不能一開始就變成,面試的時候應該和面…

先序中序后序兩兩結合重建二叉樹

遍歷是對樹的一種最基本的運算,所謂遍歷二叉樹,就是按一定的規則和順序走遍二叉樹的所有結點,使每一個結點都被訪問一次,而且只被訪問一次。由于二叉樹是非線性結構,因此,樹的遍歷實質上是將二叉樹的各個結…

劍指offer_05

文章目錄第五章 優化時間和空間效率1.1 面試官談效率1.2 時間效率1.3 時間效率和空間效率的平衡第五章 優化時間和空間效率 1.1 面試官談效率 1.時間和空間復雜度是寫程序的時候,我們需要分析的,最好每次寫完代碼后自己都可以將程序的時間和空間復雜度…

先序中序數組推后序數組

二叉樹遍歷 所謂遍歷(Traversal)是指沿著某條搜索路線,依次對樹中每個結點均做一次且僅做一次訪問。訪問結點所做的操作依賴于具體的應用問 題。 遍歷是二叉樹上最重要的運算之一,是二叉樹上進行其它運算之基礎。 從二叉樹的遞歸定義可知,一…

劍指offer_06

文章目錄第六章 面試中的各項能力1.1 面試官談能力1.2 溝通能力和學習能力1.3 知識遷移能力1.4 抽象建模能力1.5 發散思維能力第六章 面試中的各項能力 1.1 面試官談能力 1.禮貌平和,不卑不亢的和面試官溝通;邏輯清楚,詳略得到的介紹項目經…

數據結構課上筆記11

滿二叉樹 (Full binary tree) 除最后一層無任何子節點外,每一層上的所有結點都有兩個子結點二叉樹。 國內教程定義:一個二叉樹,如果每一個層的結點數都達到最大值,則這個二叉樹就是滿二叉樹。也就是說,如果一個二叉樹…

數據結構和算法(01)--- 算法復雜度

文章目錄算法時間復雜度算法時間復雜度 要判斷算法的好壞,可以從時間方面進行分析。算法運行的越快,所用的時間越短則算法越好。但是同一個算法在不同的平臺上的運行時間不同。那么又該如何進行評判呢?我們采用時間復雜度進行衡量。 1.算法時…