//初始处理接收的数据 void processReceivedData(void) {// 刷新 RX 缓冲区状态size_t bytesRead;size_t bytesWritten = 0;bytesRead = 0;uint32_t status = UART2_STATUS_SUCCESS;// 读取接收到的数据status = UART2_read(uartHandle, rxBuffer, 5, &bytesRead);if (status != UART2_STATUS_SUCCESS){/* UART2_read() failed */switch (status) {case UART2_STATUS_EINUSE:// System_printf("UART read: The UART is currently in use.\n");sendData("UART read: The UART is currently in use.\n", strlen("UART read: The UART is currently in use.\n"));break;default:System_printf("UART read: Unknown error (0x%x)\n", status);break;}}else {bytesRead = status;}if (numBytesRead > 0) {numBytesRead=0;// 处理接收到的数据rxBuffer[0]=numBytesRead;// 清除接收缓冲区memset(rxBuffer, 0, RX_BUFFER_SIZE);} } //callback void callbackFxn(UART2_Handle handle, void *buffer, size_t count, void *userArg, int_fast16_t status) {if (status != UART2_STATUS_SUCCESS){/* RX error occured in UART2_read() *///while (1) {}} } //initial uartUART2_Params uartParams;UART2_HWAttrs const *hwAttrs; UART2_Params_init(&uartParams);uartParams.readMode= UART2_Mode_CALLBACK;uartParams.readCallback = callbackFxn;uartParams.baudRate = 115200;uartParams.dataLength = UART2_DataLen_8;uartParams.stopBits = UART2_StopBits_1;uartParams.parityType = UART2_Parity_NONE;uartHandle = UART2_open(CONFIG_UART2_0, &uartParams);if (uartHandle == NULL) {//Log_info0("UART open failed");//while (1) {}uint8_t test_msg1[50] = "UART open failed";UART2_write(uartHandle, test_msg1, sizeof(test_msg1), NULL);}else{uint8_t test_msg1[50] = "UART open success";UART2_write(uartHandle, test_msg1, sizeof(test_msg1), NULL);}//main han'shuwhile (1){// 主循环,处理其他任务Task_sleep(1000); // 休眠一段时间processReceivedData();Task_sleep(1000); // 休眠一段时间size_t bytesWritten;UART2_write(uartHandle, sendString, strlen(sendString), &bytesWritten);
代码如下 运行后
数据可以一直发送 但uart_read 一直显示串口在使用
Hello, UART!
UART read: The UART is currently in use.
Alice:
您好,
UART2.h File Reference (ti.com)
试一下 UART2_readCancel()。
,
yu cui:
问题已解决 感谢