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/

时间: 2024-07-31 09:21:18

Pyserial操作串口的相关文章

转:Python通过pyserial控制串口操作

https://blog.csdn.net/lovelyaiq/article/details/48101487 你想通过串行端口读写数据,典型场景就是和一些硬件设备打交道(比如一个机器人或传感器).尽管你可以通过使用Python内置的I/O模块来完成这个任务,但对于串行通信最好的选择是使用 pySerial包 . 这个包的使用非常简单,先安装pySerial,使用类似下面这样的代码就能很容易的打开一个串行端口: 一.用python操作串口,首先需要下载相关模块: pyserial (http:

python3操作串口

通过引用serial模块包,来操作串口. 1.查看串口名称 在Linux和Windows中,串口的名字规则不太一样.需要事先查看. Linux下的查看串口命令 [email protected]:~# ls -l /dev/ttyS* crw-rw---- 1 root dialout 4, 64 Dec 26 06:53 /dev/ttyS0 crw-rw---- 1 root dialout 4, 65 Dec 26 06:41 /dev/ttyS1 crw--w---- 1 root tt

Raspberry pi 使用python+pySerial实现串口通信(转)

Raspberry pi 使用python+pySerial实现串口通信 转:http://blog.csdn.net/homeway999/article/details/8642353 目录(?)[+] Raspberry pi 使用pythonpySerial实现串口通信 Raspberry pi端安装pyserial 方法1source安装 方法2pip安装 Raspberry pi端连接串口 Windows端连接串口 Raspberry pi 使用python+pySerial实现串口

android开发(37) android使用android_serialport_api 操作串口,解决权限问题

最近有个项目,要使用android设备操作串口的 斑马GK888T打印机,使用打印机打印二维码. 硬件设备连接方式: 安卓设备 通过 串口RS232 连接 斑马打印机的串口 那么就要解决:使用安卓设备操作串口的问题. 我找到一个框架:android_serialport_api,这个框架被托管在: https://code.google.com/p/android-serialport-api/    谷歌的代码库,无奈国内无法下载 https://github.com/cepr/android

WindowsAPI操作串口

#include <windows.h> #include <stdio.h> int main() { //1.打开串口 HANDLE hCom; hCom = CreateFile("COM1", GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL); if (hCom ==(HANDLE)-1 ) printf("打开串口失败!\n"); else printf(&qu

Pyserial 实现串口 base on python3

应用Pyserial 包实现串口通信类: 其主要接口如下: 1. Serial_Create  创建和使能串口 2. Serial_WriteAndReadBin 写读二进制数据流 3. Serial_WriteAndReadString 写读字符串 4. Serial_WriteString 写字符串 5. Serial_WriteBin 写二进制数据 6. Serial_Read 读字符串数据 #coding:utf-8 #!/usr/bin/python3 import serial im

c#操作串口类

先放上串口的一个类,自己编写的,觉得这样好用些. using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.IO.Ports;using System.Collections;us

Android 操作串口 (android serial port api)

前几天公司有通过搭载Android系统的开发板来使用打卡机统计数据的需求,对于攻城狮来说就需要在Android平台上读写打卡机的串口,在网上搜索一些东西之后发现了在google code 上的android serial port api可以用,墙了之后拿到源码发现还有demo,不错不错,我这个帖子就通过serial port api的源码简单得实现一个读写串口,这个当然是在native写的,如果还有哪些童鞋不清楚android上使用jni和native的话可以跳转到我的上篇帖子 点我点我 在A

Python 操作串口

import serial导入模块 然后就可以用了 ser = serial.Serial(0) 是打开第一个串口 print ser.portstr 能看到第一个串口的标识,windows下是COM1 ser.write("hello") 就是往串口里面写数据 ser.close() 就是关闭ser表示的串口 ser.open() 会打开这个串口 ser = serial.Serial('COM1', 115200) 来设置波特率,当然还有专门的函数 data = ser.read(