//初始处理接收的数据 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 uart UART2_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'shu while (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.