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

 

转载于:https://www.cnblogs.com/liuyang92/p/7482499.html

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值