class UART – 双工串行通信总线(串口)

Usage

UART对象实现了标准的UART/USART双工串口通信协议. 在物理层面上需要两根接线RXTX

(当然还有一个GND, 作为参考电平). 串口通信的单元是字节(不要跟字符串的字符搞混).

字节宽度为8比特(bits)或者9比特.

UART创建跟初始化

from pyb import UART

uart = UART(3, 9600)                         # init with given baudrate
                                             # 根据给定波特率(9600)进行初始化
uart.init(9600, bits=8, parity=None, stop=1) # init with given parameters
参数 取值范围 含义
bits 7, 8, 9 DATA比特数
parity None, 0, 1 校验位
stop 1, 2 停止位

随机parity的不同, bits取值范围也不同

parity 校验位 bits取值范围
None (无校验位) 8, 9
0(even 偶校验), 1(odd 奇校验) 7, 8

因为要增加校验位, 所以可以发送的bits数就见少了一位.

A UART object acts like a stream object and reading and writing is done using the standard stream methods:

UART对象操作起来类似stream(数据流)对象, 读写操作如下所示

uart.read(10)       # read 10 characters, returns a bytes object
                    # 读取10个字符, 返回一个bytes对象
uart.readall()      # read all available characters
                    # 读取所有
uart.readline()     # read a line
                    # 读取一行数据
uart.readinto(buf)  # read and store into the given buffer
                    # 将数据读入缓存池
uart.write('abc')   # write the 3 characters
                    # 向串口写入3个字符.

单个字符可以使用下列函数读/写

uart.readchar()     # read 1 character and returns it as an integer
                    # 读取一个字符, 返回整数(ASCII码)
uart.writechar(42)  # write 1 character
                    # 写入一个字符

判断串口是否有数据, 返回所有等待的字符个数.

uart.any()          # returns the number of characters waiting

Constructors

pyb.UART

pyb.UART(bus, ...)

给定总线构造UART对象.

bus bus取值范围 1, 3

OpenMV3 R2的串口资源

UART ID (bus) TX RX
1 P1 = PB14 P0 = PB15
3 P4 = PB10 P5 = PB11

如果只有bus给出, 没有其他参数, 则UART只创建, 但是不初始化.

Methods

uart.init

uart.init(baudrate, bits=8, parity=None, stop=1, *, timeout=1000, flow=0, timeout_char=0, read_buf_len=64)

串口初始化.

Usage部分的串口创建及初始化

TODO 翻译

Initialize the UART bus with the given parameters:baudrate is the clock rate.bits is the number of bits per character, 7, 8 or 9.parity is the parity, None, 0 (even) or 1 (odd).stop is the number of stop bits, 1 or 2.flow sets the flow control type. Can be 0, UART.RTS, UART.CTS or UART.RTS | UART.CTS.timeout is the timeout in milliseconds to wait for the first character.timeout_char is the timeout in milliseconds to wait between characters.read_buf_len is the character length of the read buffer (0 to disable).This method will raise an exception if the baudrate could not be set within 5% of the desired value. The minimum baudrate is dictated by the frequency of the bus that the UART is on; UART(1) is on APB2, UART(3) is on APB1. The default bus frequencies give a minimum baudrate of 1300 for UART(1) and 650 for the others. Use pyb.freq to reduce the bus frequencies to get lower baudrates.Note: with parity=None, only 8 and 9 bits are supported. With parity enabled, only 7 and 8 bits are supported.

uart.deinit

uart.deinit()

关闭UART总线.

uart.any

uart.any()

返回串口等待被接受的字符串个数. 如果是0, 就代表没有

Returns the number of bytes waiting (may be 0).

uart.writechar

uart.writechar(char)

写入单个字符

@Param char 类型:整数 要写入的字符.

uart.read

uart.read([nbytes])

@Param nbytes 最多读入的字节数(可能串口缓冲区的字节数小于nbytes) 一个字符占两个字节(单个字节9-bits), 所以nbytes必须是偶数. 读入的字符的个数= nbytes / 2

uart.readall

uart.readall()

读入尽可能多的数据.

timeout事件过去之后返回数值. 返回数据有两种, 一种是bytes, 一种是None 当超时timeout发生时, 阻止任何的读事件, 此时返回的为None.

uart.readchar

uart.readchar()

在总线上读取单个字符, 返回整数. 如果timeout返回-1

uart.readinto

uart.readinto(buf[, nbytes])

将字节数据读入buf. 如果制定了nbytes, 则最多读入最多nbytes个数据. 否则,读入最多buf长度的字节.

返回数据, 存储在buf的字节数. 如果timeout 返回None.

uart.readline

uart.readline()

读入一行数据, 在换行符处结束\n 如果存在一行数据, 立刻返回, 如果timeout过后, 所有的有效的数据都被返回, 而不管是否有\n存在.

如果没有数据至timeout则 返回None

uart.write

uart.write(buf)

buf中存储的字节, 写入到总线. 如果字符是7/8 bits宽, 则一个字节(byte) , 代表一个字符 如果字符是9-bts宽, 则使用两个字节(byte), 代表一个字符

buf必须包含偶数个字节(bytes) 返回 写入的字节数. 如果timeout出现且无bytes写入, 则返回None

uart.sendbreak

uart.sendbreak()

向总线发送中断条件. 总线将在此之后13bits的数据发送低位. 返回None

Constants

UART.``RTS 数据流控制类型 RTS

UART.``CTS 数据流控制类型 CTS

TODO 翻译 数据流控制

Flow Control

UART(3) support RTS/CTS hardware flow control using the following pins:

  • UART(3) is on :(TX, RX, nRTS, nCTS) = (P4, P5, P1, P2) = (PB10, PB11, PB14, PB13)

In the following paragraphs the term “target” refers to the device connected to the UART.

When the UART’s init() method is called with flow set to one or both of UART.RTS and UART.CTS the relevant flow control pins are configured. nRTS is an active low output, nCTS is an active low input with pullup enabled. To achieve flow control the OpenMV Cam’s nCTS signal should be connected to the target’s nRTS and the OpenMV Cam’s nRTS to the target’s nCTS.

CTS: target controls OpenMV Cam transmitter

If CTS flow control is enabled the write behavior is as follows:

If the OpenMV Cam’s uart.write(buf) method is called, transmission will stall for any periods when nCTS is False. This will result in a timeout if the entire buffer was not transmitted in the timeout period. The method returns the number of bytes written, enabling the user to write the remainder of the data if required. In the event of a timeout, a character will remain in the UARTpending nCTS. The number of bytes composing this character will be included in the return value.

If uart.writechar() is called when nCTS is False the method will time out unless the target asserts nCTS in time. If it times out OSError 116 will be raised. The character will be transmitted as soon as the target asserts nCTS.

RTS: OpenMV Cam controls target’s transmitter

If RTS flow control is enabled, behavior is as follows:

If buffered input is used (read_buf_len > 0), incoming characters are buffered. If the buffer becomes full, the next character to arrive will cause nRTS to go False: the target should cease transmission. nRTS will go True when characters are read from the buffer.

Note that the any() method returns the number of bytes in the buffer. Assume a buffer length of Nbytes. If the buffer becomes full, and another character arrives, nRTS will be set False, and any()will return the count N. When characters are read the additional character will be placed in the buffer and will be included in the result of a subsequent any() call.

If buffered input is not used (read_buf_len == 0) the arrival of a character will cause nRTS to go False until the character is read.

Copyright 杭州云江科技有限公司 2017 all right reserved,powered by Gitbook该文件修订时间: 2018-04-02 09:53:12

results matching ""

    No results matching ""