# `UART` Module API Manual

## Overview

The K230 integrates five UART (Universal Asynchronous Receiver/Transmitter) hardware modules internally. UART0 is occupied by the small core SH, UART3 is occupied by the large core SH, and the remaining UART1, UART2, and UART4 are available for user use. For UART I/O configuration, please refer to the IOMUX module.

## API Introduction

The UART class is located in the `machine` module.

### Example Code

```python
from machine import UART

# Configure UART1: baud rate 115200, 8 data bits, no parity, 1 stop bit
u1 = UART(UART.UART1, baudrate=115200, bits=UART.EIGHTBITS, parity=UART.PARITY_NONE, stop=UART.STOPBITS_ONE)

# Write data to UART
u1.write("UART1 test")

# Read data from UART
r = u1.read()

# Read a line of data
r = u1.readline()

# Read data into a byte buffer
b = bytearray(8)
r = u1.readinto(b)

# Release UART resources
u1.deinit()
```

### Constructor

```python
uart = UART(id, baudrate=115200, bits=UART.EIGHTBITS, parity=UART.PARITY_NONE, stop=UART.STOPBITS_ONE, timeout = 0)
```

**Parameters**

- `id`: UART module number, valid values are `UART1`, `UART2`, `UART4`.
- `baudrate`: UART baud rate, optional parameter, default value is 115200.
- `bits`: Number of data bits per character, valid values are `FIVEBITS`, `SIXBITS`, `SEVENBITS`, `EIGHTBITS`, optional parameter, default value is `EIGHTBITS`.
- `parity`: Parity check, valid values are `PARITY_NONE`, `PARITY_ODD`, `PARITY_EVEN`, optional parameter, default value is `PARITY_NONE`.
- `stop`: Number of stop bits, valid values are `STOPBITS_ONE`, `STOPBITS_TWO`, optional parameter, default value is `STOPBITS_ONE`.
- `timeout`: Read data timeout, unit is `ms`.

### `init` Method

```python
UART.init(baudrate=115200, bits=UART.EIGHTBITS, parity=UART.PARITY_NONE, stop=UART.STOPBITS_ONE)
```

Configure UART parameters.

**Parameters**

Refer to the constructor.

**Return Value**

None

### `read` Method

```python
UART.read([nbytes])
```

Read characters. If `nbytes` is specified, at most that many bytes will be read; otherwise, as much data as possible will be read.

**Parameters**

- `nbytes`: Maximum number of bytes to read, optional parameter.

**Return Value**

Returns a bytes object containing the read bytes.

### `readline` Method

```python
UART.readline()
```

Read a line of data, terminated by a newline character.

**Parameters**

None

**Return Value**

Returns a bytes object containing the read bytes.

### `readinto` Method

```python
UART.readinto(buf[, nbytes])
```

Read bytes into `buf`. If `nbytes` is specified, at most that many bytes will be read; otherwise, at most `len(buf)` bytes will be read.

**Parameters**

- `buf`: A buffer object.
- `nbytes`: Maximum number of bytes to read, optional parameter.

**Return Value**

Returns the number of bytes read and stored in `buf`.

### `write` Method

```python
UART.write(buf)
```

Write the byte buffer to UART.

**Parameters**

- `buf`: A buffer object.

**Return Value**

Returns the number of bytes written.

### `deinit` Method

```python
UART.deinit()
```

Release UART resources.

**Parameters**

None

**Return Value**

None
