Pyboard basic functions --- UART

UART

UART (Universal Asynchronous Receiver Transmitter) is referred to in the microcontroller and embedded systems, serial port (UART) has always been very important peripherals. Although the serial port speed is not fast, but because it uses simple (serial data may be transmitted between two chips easiest way), occupy less hardware and software resources, communication, control, many aspects of data transmission, simulation and debugging there are very widely used. Many devices or modules will even provide a dedicated serial interfaces for communication and control, such as a GPRS module, a Bluetooth / WiFi pass-through module. Make use of a transmitting GPIO UART, a GPIO received do not separate clock signal. Send and receive both need a good agreement the same baud rate, data bits, parity, stop bits and other parameters for communication, it is also known as asynchronous serial bus.

1. UART acquisition method of the class

>>> from pyb import UART
>>> help(pyb.UART)
object <class 'UART'> is of type type
  init -- <function>
  deinit -- <function>
  any -- <function>
  read -- <function>
  readline -- <function>
  readinto -- <function>
  write -- <function>
  irq -- <function>
  writechar -- <function>
  readchar -- <function>
  sendbreak -- <function>
  RTS -- 256
  CTS -- 512
  IRQ_RXIDLE -- 16
>>>

 2. In MicroPython, the serial operation and easy to use as GPIO. We take a look at common functions serial port, from where you can see the basics of using the serial port.

The basic use of the serial port, the serial port is first defined, and then set the serial port parameters (the most important is to set the baud rate), and then () function or string through the transmit buffer write. Or use any () function to determine whether the received data, then the read () function reads the data.

UART function is as follows:

(1)class pyb.UART(bus,...)

bus: 1-6, or 'XA', 'XB', 'YA', 'YB'.

On PYB V10, the corresponding GPIO port is:

 

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

Serial port initialization.

◆ baudrate: Baud Rate

◆ bits: data bits can be 7/8/9

◆ parity: parity bit can be set to None, 0 (even) or 1 (odd), no parity default

◆ stop: stop bit, 1/2

◆ flow: flow control, can be None, UART.RTS, UART.CTS or UART.RTS | UART.CTS, the default None (no flow control)

◆ timeout: read a byte timeout time (ms)

◆ timeout_char: read or write latency between the two bytes

◆ read_buf_len: read buffer length, 0 disables buffer

(3) uart.deinit ()

Close the serial port.

(4)uart.any()

Returns the number of data buffer, the received data is greater than 0 for.

(5)uart.writechar(char)

Writes a byte.

(6)uart.read([nbytes])

Nbytes bytes read up. If the data bit is 9 bits, so a data occupies two bytes, and nbytes must be even.

(7)uart.readall()

Read all the data.

(8)uart.readchar()

Read a byte.

(9)uart.readinto(buf[,nbytes])

buf: data buffer;

nbytes: The maximum number of reads.

(10)uart.readline()

Reads one line.

(11)uart.write(buf)

Write buffer. In 9bits mode, a two-byte count data.

(12) uart.sendbreak ()

Transmission suspension state to the bus, the bus 13bits down time.

 Note:

When setting the baud rate, the baud rate if the error exceeds 5%, will cause an exception.

When no parity, 8 data bits, or can be set to 9; and when using parity, data bits can only be set to 7 or 8 bits.

In nine bit mode data, whether read or write, a data occupies 2 bytes.

Currently in MicroPython, the serial port does not support callback function (interrupt), only to determine whether the received data by means of queries (any () function). Considering the serial communication speed is not too fast, in most cases in this way is able to meet the performance requirements.

On PYB V10, 2 and 3 only serial port supports flow control.

 

Guess you like

Origin www.cnblogs.com/iBoundary/p/11516976.html