Easy-Python  1.00.00Change to: 中文
UART

Functions

 UART (id, baudrate)
 Create an UART object of the given id: More...
 
 any ()
 Return an integer counting the number of characters. More...
 
 read (nbytes)
 Read characters. More...
 
 readinto (buf)
 Read bytes into the buf. More...
 
 readline ()
 Read a line, ending in a newline character. More...
 
 write (buf)
 Write the buffer of bytes to the bus. More...
 

Detailed Description

UART implements the standard UART/USART duplex serial communications protocol. At the physical level it consists of 2 lines: RX and TX. The unit of communication is a character (not to be confused with a string character) which can be 8 bits wide.

UART Example of Micropython
Click here
Uart Function of usage:
uart = UART(1,115200) # create an uart object of id=1 baudrate=115200
uart.read(10) # read 10 characters, returns a bytes object
uart.read() # 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

Function Documentation

UART ( id  ,
baudrate   
)

Create an UART object of the given id:

Parameters
[in]idis the UART number
  • 0 : selsct UART0
  • 1 : selsct UART1
[in]baudrateis the clock rate
  • 9600 : baudrate 9600
  • 14400 : baudrate 14400
  • 19200 : baudrate 19200
  • 38400 : baudrate 38400
  • 57600 : baudrate 57600
  • 115200 : baudrate 115200
  • 230400 : baudrate 230400
  • 460800 : baudrate 460800
Returns
an UART object
Example of usage:
from machine import UART
ser=UART(1,115200)
any ( )

Return an integer counting the number of characters.

UART.any()

Parameters
no
Returns
Returns an integer counting the number of characters that can be read without blocking.
It will return 0 if there are no characters available and a positive number if there are characters
Example of usage:
from machine import UART
ser=UART(1,115200)
ser.any()
read ( nbytes  )

Read characters.

UART.read([nbytes])

Parameters
[in]nbytesis specified then read at most that many bytes, otherwise read as much data as possible.
Returns
a bytes object
Example of usage:
from machine import UART
uartStr=b''
ser=UART(1,115200)
uartStr=ser.read()
readinto ( buf  )

Read bytes into the buf.

UART.readinto(buf[, nbytes])

Parameters
[in]buf: the given buffer is stored data
[in]nbytes: read nbytes to the given buffer
Returns
If nbytes is specified then read at most that many bytes. Otherwise, read at most len(buf) bytes.
Example of usage:
from machine import UART
uartStr=b''
ser=UART(1,115200)
uartStr=ser.readinto(buf)
readline ( )

Read a line, ending in a newline character.

UART.readline()

Parameters
no
Returns
Return value: the line read or None on timeout.
Example of usage:
from machine import UART
uartStr=b''
ser=UART(1,115200)
uartStr=ser.readline()
write ( buf  )

Write the buffer of bytes to the bus.

UART.write(buf)

Parameters
[in]buf: write data of buffer to UART
Returns
(no)
Example of usage:
from machine import UART
ser=UART(1,115200)
ser.write('abc')