树莓派串口的使用(pyserial库)

树莓派串口的使用(pyserial库)

@(树莓派学习笔记)

安装pyserial库(不是serial)
pip install pyserial
库的文档:https://pyserial.readthedocs.io/en/latest/pyserial_api.html

pyserial库的简单介绍

class serial.Serial __init__(port=None, baudrate=9600, bytesize=EIGHTBITS, parity=PARITY_NONE, stopbits=STOPBITS_ONE, timeout=None, xonxoff=False, rtscts=False, write_timeout=None, dsrdtr=False, inter_byte_timeout=None, exclusive=None)
Parameters:
port–设备名称或无。
baudrate(int)–波特率,例如9600或115200等。
bytesize –数据位数。 可能的值:五位,六位,七位,八位
parity–奇偶校验。 可能的值:PARITY_NONE,PARITY_EVEN,PARITY_ODD,PARITY_MARK,PARITY_SPACE
stopbits –停止位的数量。 可能的值:STOPBITS_ONE,STOPBITS_ONE_POINT_FIVE,STOPBITS_TWO
timeout(浮动)–设置读取超时值。
xonxoff(布尔)–启用软件流控制。
rtscts(布尔)–启用硬件(RTS / CTS)流控制。
dsrdtr(布尔)–启用硬件(DSR / DTR)流控制。
write_timeout(浮点型)–设置写超时值。
inter_byte_timeout(浮点型)–字符间超时,无则禁用(默认)。
exclusive(布尔)–设置独占访问模式(仅POSIX)。 如果端口已经以独占访问模式打开,则不能以独占访问模式打开端口。
给定端口后,将在创建对象时立即打开该端口。 当端口为None且需要连续调用open()时,不会打开它。

port是设备名称,取决于操作系统。 例如 在GNU / Linux上为/ dev / ttyUSB0,在Windows上为COM3
timeout/write_timeout:
timeout=None:阻塞模式,永远等待/直到收到请求的字节数
timeout= 0:非阻塞模式,在任何情况下立即返回,返回零个或多个,直到请求的字节数
timeout= x:将timeout设置为x秒(允许浮点数),当请求的字节数可用时立即返回,否则请等待直到超时到期并返回直到那时为止接收到的所有字节。

open():打开端口。

close():立即关闭端口。

read(size=1)读取数据
Parameters: size – Number of bytes to read.
Returns: Bytes read from the port.
Return type: bytes
从串行端口读取大小字节。如果设置了超时,则返回的字符数可能少于请求的字符数。如果没有超时,它将阻塞,直到读取请求的字节数。

read_until(expected=LF, size=None):读取直到找到预期的序列(默认情况下为“\n”),超出大小或直到发生超时。如果设置了超时,则返回的字符数可能少于请求的字符数。如果没有超时,它将阻塞,直到读取请求的字节数。
Parameters:
expected – The byte string to search for.
size – Number of bytes to read.
Returns:
Bytes read from the port.

Return type:
bytes

write(data)
Parameters: data – Data to send.
Returns: Number of bytes written.
Return type: int
Raises: SerialTimeoutException – In case a write timeout is configured for the port and the time is exceeded.

Write the bytes data to the port. This should be of type (or compatible such as or ). Unicode strings must be encoded (e.g. .bytes/bytearray/memoryview 'hello'.encode('utf-8'))

flush():刷新文件,如对象。在这种情况下,请等到写入所有数据

in_waiting
Getter: Get the number of bytes in the input buffer
Type: int
Return the number of bytes in the receive buffer.

out_waiting
Getter: Get the number of bytes in the output buffer
Type: int
Platform: Posix
Platform: Windows

reset_input_buffer():刷新输入缓冲区,丢弃其所有内容。

reset_output_buffer():清除输出缓冲区,中止当前输出并丢弃缓冲区中的所有内容。

请注意,对于某些 USB 串行适配器,这可能仅刷新操作系统的缓冲区,而不是 USB 部件中可能存在的所有数据。

send_break(duration=0.25)
Parameters: duration (float) – Time in seconds, to activate the BREAK condition.
Send break condition. Timed, returns to idle state(空闲状态) after given duration.

break_condition
Getter: Get the current BREAK state
Setter: Control the BREAK state
Type: bool
When set to activate BREAK condition, else disable. Controls TXD. When active, no transmitting is possible.

is_open:获取串口的状态,无论它是否打开。返回True或False

参考文档:https://blog.csdn.net/Electrical_IT/article/details/107201561

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值