工具与软件:
尊敬的 TI 技术团队:
我使用的是 CC3220MODASF launchpad、并且尝试使用 AT 命令与 GSM 调制解调器进行通信。 我的问题是、当 GSM 调制解调器的响应是一个大字符串时、如何初始化 UART 并处理读取?
下面是针对 GSM 的 UART 初始化
uart_init();
UART_Params_init (&uartParams);
uartParams.writeDataMode = UART_DATA_BINARY;
uartParams.readDataMode = UART_DATA_BINARY;
uartParams.readReturnMode = UART_RETURN_FULL;
uartParams.readEcho = uart_echo_off;
uartParams.baudrate = 115200;
uartParams.readTimeout = UART_WAIT_FOREVER;// 500
GSM_UART= UART_OPEN (CONFIG_UART_1、&uartParams);
if (GSM_UART => NULL){
// uart_open()失败
while (1);
}
UART 读取句柄
void printBuffer() { buffer[buffer_index] = '\0'; // Null-terminate the buffer //Display_printf(display, 0, 0, "%s", buffer); // Print the buffer as a string strcat(UART_READ, buffer); buffer_index = 0; // Reset buffer index for next data } int readGSMUART(char *output, size_t max_length, int timeout_ms) { unsigned long start_time = Timer_getMillis(); while (Timer_getMillis() - start_time < timeout_ms) { if (UART_read(Gsm_uart, &input, 1) > 0) { if (input >= 32 && input <= 126) { buffer[buffer_index++] = input; // Store the character in the buffer if (buffer_index >= MAX_BUFFER_SIZE - 1) { printBuffer(); } } else if (input == '\n' || input == '\r') { printBuffer(); // Handle newline or carriage return if (strlen(UART_READ) > 0) { // Check if the response is complete (usually by checking for specific patterns or length) if (strstr(UART_READ, "OK") || strstr(UART_READ, "ERROR") || strstr(UART_READ, ">") || (strlen(UART_READ) >= max_length - 1)) { strncpy(output, UART_READ, max_length - 1); // Copy to output output[max_length - 1] = '\0'; // Ensure null-terminated string memset(UART_READ, 0, MAX_BUFFER_SIZE); return 1; // Successfully read response } } } } } if (strlen(UART_READ) > 0) { removeOKFromResponse(UART_READ); strncpy(output, UART_READ, max_length - 1); // Copy to output output[max_length - 1] = '\0'; // Ensure null-terminated string memset(UART_READ, 0, MAX_BUFFER_SIZE); return 1; // Successfully read response } output[0] = '\0'; // In case of timeout or failure Display_printf(display, 0, 0, "UART_READ TIMEOUT or FAILURE\n"); return 0; }