同步操作将从 OpenHarmony/docs 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
Figure 1 shows the process of using a UART device.
Figure 1 Process of using a UART device
Before performing UART communication, call UartOpen to obtain a UART device handle. This function returns the pointer to the UART device handle with a specified port number.
struct DevHandle *UartOpen(uint32_t port);
Table 1 Description of UartOpen
The following example shows how to obtain a UART device handle based on the assumption that the UART port number is 3:
struct DevHandle *handle = NULL; /* Pointer to the UART device handle */
uint32_t port = 3; /* UART port number */
handle = UartOpen(port);
if (handle == NULL) {
HDF_LOGE("UartOpen: failed!\n");
return;
}
After obtaining the UART device handle, set the UART baud rate by calling the following function:
int32_t UartSetBaud(struct DevHandle *handle, uint32_t baudRate);
Table 2 Description of UartSetBaud
The following example shows how to set the UART baud rate to 9600:
int32_t ret;
/* Set the UART baud rate to 9600. */
ret = UartSetBaud(handle, 9600);
if (ret != 0) {
HDF_LOGE("UartSetBaud: failed, ret %d\n", ret);
}
After setting the UART baud rate, obtain the current baud rate by calling the following function:
int32_t UartGetBaud(struct DevHandle *handle, uint32_t *baudRate);
Table 3 Description of UartGetBaud
The following example shows how to obtain the UART baud rate:
int32_t ret;
uint32_t baudRate;
/* Obtain the UART baud rate. */
ret = UartGetBaud(handle, &baudRate);
if (ret != 0) {
HDF_LOGE("UartGetBaud: failed, ret %d\n", ret);
}
Before performing UART communication, set the UART device attributes by calling the following function:
int32_t UartSetAttribute(struct DevHandle *handle, struct UartAttribute *attribute);
Table 4 Description of UartSetAttribute
The following example shows how to set the UART device attributes:
int32_t ret;
struct UartAttribute attribute;
attribute.dataBits = UART_ATTR_DATABIT_7; /* Set the number of data bits to 7. */
attribute.parity = UART_ATTR_PARITY_NONE; /* Set the parity bit to no parity. */
attribute.stopBits = UART_ATTR_STOPBIT_1; /* Set the stop bit to 1. */
attribute.rts = UART_ATTR_RTS_DIS; /* Disable the RTS signal. */
attribute.cts = UART_ATTR_CTS_DIS; /* Disable the CTS signal. */
attribute.fifoRxEn = UART_ATTR_RX_FIFO_EN; /* Enable RX FIFO. */
attribute.fifoTxEn = UART_ATTR_TX_FIFO_EN; /* Enable TX FIFO. */
/* Set the UART device attributes. */
ret = UartSetAttribute(handle, &attribute);
if (ret != 0) {
HDF_LOGE("UartSetAttribute: failed, ret %d\n", ret);
}
After setting the UART device attributes, obtain the current device attributes by calling the following function:
int32_t UartGetAttribute(struct DevHandle *handle, struct UartAttribute *attribute);
Table 5 Description of UartGetAttribute
The following example shows how to obtain the UART device attributes:
int32_t ret;
struct UartAttribute attribute;
/* Obtain the UART attributes. */
ret = UartGetAttribute(handle, &attribute);
if (ret != 0) {
HDF_LOGE("UartGetAttribute: failed, ret %d\n", ret);
}
Before performing UART communication, set the UART transmission mode by calling the following function:
int32_t UartSetTransMode(struct DevHandle *handle, enum UartTransMode mode);
Table 6 Description of UartSetTransMode
The following example shows how to set the transmission mode to UART_MODE_RD_BLOCK:
int32_t ret;
/* Set the UART transmission mode. */
ret = UartSetTransMode(handle, UART_MODE_RD_BLOCK);
if (ret != 0) {
HDF_LOGE("UartSetTransMode: failed, ret %d\n", ret);
}
To write data into a UART device, call the following function:
int32_t UartWrite(struct DevHandle *handle, uint8_t *data, uint32_t size);
Table 7 Description of UartWrite
The following example shows how to write data of a specified length into the UART device:
int32_t ret;
uint8_t wbuff[5] = {1, 2, 3, 4, 5};
/* Write 5-byte data into the UART device. */
ret = UartWrite(handle, wbuff, 5);
if (ret != 0) {
HDF_LOGE("UartWrite: failed, ret %d\n", ret);
}
To read data from a UART device, call the following function:
int32_t UartRead(struct DevHandle *handle, uint8_t *data, uint32_t size);
Table 8 Description of UartRead
The following example shows how to read data of a specified length from the UART device:
int32_t ret;
uint8_t rbuff[5] = {0};
/* Read 5-byte data from the UART device. */
ret = UartRead(handle, rbuff, 5);
if (ret < 0) {
HDF_LOGE("UartRead: failed, ret %d\n", ret);
}
CAUTION: Data is successfully read from the UART device if a non-negative value is returned. If the return value is 0, no valid data can be read from the UART device. If the return value is greater than 0, the return value is the length of the data actually read from the UART device. The length is less than or equal to the value of size and does not exceed the maximum length of data to read at a time specified by the UART controller in use.
After the UART communication, destroy the UART device handle by calling the following function:
void UartClose(struct DevHandle *handle);
This function will release the resources previously obtained.
Table 9 Description of UartClose
The following example shows how to destroy the UART device handle:
UartClose(handle); /* Destroy the UART device handle. */
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。