Python 串口通讯
摘要:
pyserial module: https://github.com/tbusf/pyserial
Python使用pyserial进行串口通信:http://blog.csdn.net/log1100/article/details/54380325
串口通讯的python模块——pySerial :http://blog.csdn.net/dainiao01/article/details/5885122
Parameters for the Serial class
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
)
Methods of Serial instances
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
readonly
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:(即使是打开的情况下也会重新配置???liub)
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
居然还有这么多好东西,看看下面:
TCP/IP – serial bridge
This program opens a TCP/IP port. When a connection is made to that port (e.g. with telnet) it forwards all data to the serial port and vice versa.
This example only exports a raw socket connection. The next example below gives the client much more control over the remote serial port.
- The serial port settings are set on the command line when starting the program.
- There is no possibility to change settings from remote.
- All data is passed through as-is.
打开一个python shell
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()可以读一个字符
data = ser.read(20) 是读20个字符
data = ser.readline() 是读一行,以/n结束,要是没有/n就一直读,阻塞。
data = ser.readlines()和ser.xreadlines()都需要设置超时时间
ser.baudrate = 9600 设置波特率
ser 来查看当前串口的状态
ser.isOpen() 看看这个串口是否已经被打开
import pyserial
t=serial.Serial()
t.port = 3
t.baudrate = 115200
t.open()
t.write(chr(0x03)) #向串口输入ctrl+c
Python 串口通讯的更多相关文章
- python实现串口通讯小程序(GUI界面)
python实现串口通讯小程序(GUI界面) 使用python实现串口通讯需要使用python的pyserial库来实现,这个库在安装python的时候没有自动进行安装,需要自己进行安装. 1.安装p ...
- C#串口通讯实例
本文参考<C#网络通信程序设计>(张晓明 编著) 程序界面如下图: 参数设置界面代码如下: using System; using System.Collections.Generic; ...
- delphi之动态库调用和串口通讯
串口通讯: Spcomm 控件属性: CommName :表示COM1,COM2等串口的名字: BaudRate:设定波特率9600,4800等 StartComm StopComm 函数Write ...
- 用SPCOMM 在 Delphi中实现串口通讯 转
用Delphi 实现串口通讯,常用的几种方法为:使用控件如MSCOMM和SPCOMM,使用API函数或者在Delphi 中调用其它串口通讯程序.利用API编写串口通信程序较为复杂,需要掌握大量通信 ...
- 西门子plc串口通讯方式
西门子plc串口通讯的三种方式 时间:2015-10-25 14:31:55编辑:电工栏目:西门子plc 导读:西门子plc串口通讯的三种方式,分为RS485 串口通信.PPI 通信.MPI 通信,自 ...
- 教程-Delphi MSComm 实时串口通讯
Delphi MSComm 实时串口通讯 MSComm控件具有丰富的与串口通信密切相关的属性,提供了对串口进行的多种操作,进而使串行通信变得十分简便.MSComm的控件属性较多,常用的属性如下:1) ...
- 浅析PC机串口通讯流控制
转自浅析PC机串口通讯流控制 我们在串行通讯处理中,常常看到RTS/CTS和XON/XOFF这两个选项,这就是两个流控制的选项,目前流控制主要应用于调制解调器的数据通讯中,但对普通RS232编程,了解 ...
- STM32F10x 学习笔记6(USART实现串口通讯 2)
这次讲讲利用串口收发中断来进行串口通讯.STM32 上为每个串口分配了一个中断.也就是说无论是发送完成还是收到数据或是数据溢出都产生同一个中断.程序需在中断处理函数中读取状态寄存器(USART_SR) ...
- STM32F10x 学习笔记5(USART实现串口通讯 1)
STM32F10x 系列单片机中都包含了USART 模块,所谓USART,就是通用同步异步收发器.通用同步异步收发器(USART)提供了一种灵活的方法与使用工业标准NRZ异步串行数据格式的外部设备之间 ...
随机推荐
- Centos7--从最小化系统发开发环境
Centos7--从最小化系统发开发环境 程序员总是离不开"环境"的困扰,从进入新手村的那一天就开始手动搞各种环境.虽然阿里云学生服务很方便,但是想弄集群真的买不起.正好实验室有 ...
- Redis笔记一
REmote DIctionary Server 是一个开源.内存存储的数据结构服务器,可以用作数据库来存储key-value数据,支持字符串,哈希表,列表,集合,位图,地理空间信息等数据类型,同时也 ...
- 转载:PHP扩展函数库-文件系统、进程与网络
PHP的扩展函数库十分庞大,官方的非官方的,在这里只记录一些目前比较常用的扩展,对于这一部分,也只是记录其中一些核心的函数,不是一个全面记录.对于详细的扩展函数说明,需要在使用中参考PHP的用户手册. ...
- 9.EL表达式 和 JSTL核心标签库
EL表达式 1./*获取数据*/ (某个web域中的对象,访问javabean的属性.访问List集合.访问Map集合.访问数组) <html> <head> <titl ...
- 运行时异常与受检异常有何异同、error和exception有什么区别
1.运行时异常与受检异常有何异同? 异常表示程序运行过程中可能出现的非正常状态,运行时异常表示虚拟机的通常操作中可能遇到的异常,是一种常见运行错误,只要程序设计得没有问题通常就不会发生.受检异常跟程序 ...
- 【Struts2】文件上传与下载
一.上传 1.1 Struts2实现步骤 浏览器端 服务器端 1.2 关于Struts2中文件上传细节: 1.3 示例 jsp文件 Action类 struts.xml文件配置 二.下载 2.1 文件 ...
- Failed to parse multipart servlet request; nested exception is java.io.IOException: The temporary upload location [/tmp/tomcat.1428942566812653608
这个问题也是某天做一个上传文件功能发生的.然后在网上查找的资料,整理了这几个解决方案. 1.在application.yml文件中设置multipart location ,并重启项目 spring: ...
- 《设计模式之美》 <02>评判代码质量好坏的维度
如何评价代码质量的高低? 实际上,咱们平时嘴中常说的“好”和“烂”,是对代码质量的一种描述.“好”笼统地表示代码质量高,“烂”笼统地表示代码质量低.对于代码质量的描述,除了“好”“烂”这样比较简单粗暴 ...
- 修改IDEA关闭标签页的快捷键
IDEA原先关闭标签页的快捷键是Ctrl + F4 ,操作起来很不方便,而我们通常习惯于用 Ctrl + W 关闭浏览器的标签页,所以,也可以将关闭IDEA标签页的快捷键修改为Ctrl + W,具体步 ...
- unomp 矿池运行问题随记
经过大量的实践,遇到的问题或经验如下: 1.单机运行多矿池时,单机CPU核心数 成为性能瓶颈,运行两个月后,有部分用户反映 矿机速率只有以前的一半. 2.Dash 等可以自行报块的矿池,每个块的股份比 ...