请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。
器件型号:CC2340R5 工具与软件:
我修改了 Data Stream Project 才能启用 外设+中心角色 并将其刷写到 CC2340R5器件中。 另一个 CC2340R5器件以外设角色运行未修改的数据流项目。 这两个器件都成功建立了 BLE 连接。
- 115200波特率下的 UART 通信 :通过 UART (中央到外设)在设备之间传输和接收数据时运行完美。
- 9600波特率下的 UART 通信 :经过几秒钟的连续数据传输和接收后 中央设备挂起 .
连接到 BLE 模块的终端设备仅支持 9600波特率 、使通信在该波特率下无缝工作至关重要。
中央设备设置和代码:
我附上了屏幕截图、其中显示:
- 的设置 中央设备 .
- 针对数据流服务特性的代码处理数据读取/写入操作。
需要紧急支持:
- 潜在的问题 :在高频数据传输过程中,中心设备的 UART 接口或缓冲区处理可能无法有效地处理较慢的波特率(9600)。
- 征求建议 :请建议修改代码、UART 配置或流控制机制以解决此问题。
// Writing UART data to the peripheral from central device void sendUartToDataIn() { bStatus_t status = SUCCESS; uint16_t offset = 0; uint16_t mtuSize = 200; uint16_t headerSize = 3; // Approximate GATT header size uint16_t maxChunkSize = mtuSize - headerSize; uint16_t dataLength = BufferSize;//strlen((char*)uartReadBuffer); // Total data length attWriteReq_t req; char buffer[40]; // Loop until all data is sent while (offset < dataLength) { // Determine the chunk size for this iteration uint16_t chunkSize = (dataLength - offset > maxChunkSize) ? maxChunkSize : (dataLength - offset); bool chunkSent = false; // Retry sending the current chunk until successful while (!chunkSent) { // Allocate memory for the write request req.pValue = GATT_bm_alloc(0x0, ATT_WRITE_REQ, chunkSize, NULL); if (req.pValue != NULL) { // Set up the request details req.handle = 0x25; // Set handle for DataIn characteristic req.len = chunkSize; req.sig = FALSE; req.cmd = TRUE; // Using Write Command (Write without Response) // Copy data to the request buffer memcpy(req.pValue, uartReadBuffer + offset, chunkSize); // Attempt to send the data chunk // vTaskDelay(pdMS_TO_TICKS(5)); status = GATT_WriteNoRsp(0x0, &req); // vTaskDelay(pdMS_TO_TICKS(10)); if (status == SUCCESS) { chunkSent = true; // Exit retry loop on success offset += chunkSize; // Move to the next chunk // Only clear sent chunk data in `uartReadBuffer` if desired, or keep it until the entire buffer is read } else { // Free the allocated buffer on failure GATT_bm_free((gattMsg_t *)&req, ATT_WRITE_REQ); vTaskDelay(pdMS_TO_TICKS(10)); } } else { // Memory allocation failed; log error and retry after delay UART2_write(uart, "Memory allocation failed\n", strlen("Memory allocation failed\n"), NULL); vTaskDelay(pdMS_TO_TICKS(50)); // Delay before retrying allocation } } } // Reset UART read buffer for new data memset(uartReadBuffer, 0, UART_MAX_READ_SIZE); UART2_read(uart, uartReadBuffer, UART_MAX_READ_SIZE, NULL); // Set up UART for the next data reception } // Reading data at central form peripheral and writing it to UART case ATT_HANDLE_VALUE_NOTI: { // Handle incoming notification data uint16_t handle = gattMsg->msg.handleValueNoti.handle; uint8_t *pValue = gattMsg->msg.handleValueNoti.pValue; uint16_t len = gattMsg->msg.handleValueNoti.len; uint16_t bytesWritten = 0; uint16_t chunkSize = 200; // Define the max UART write size (200 bytes in this case) // snprintf(buffer, sizeof(buffer), "Size : %d\n", len); // UART2_write(uart, buffer, strlen(buffer), NULL); // Loop to write data to UART in manageable chunks while (bytesWritten < len) { uint16_t bytesToWrite = (len - bytesWritten) > chunkSize ? chunkSize : (len - bytesWritten); // Write a chunk to UART UART2_write(uart, pValue + bytesWritten, bytesToWrite, NULL); // vTaskDelay(pdMS_TO_TICKS(5)); // Update the bytes written bytesWritten += bytesToWrite; GATT_bm_free(&gattMsg->msg,gattMsg->method); }
感谢您的支持。