问题遇到的现象和发生背景
再MSP430F5529单片机与OPENMV的串口通信上,出现了不接收或接收乱码的错误,中断处理函数如下:
#pragma vector = USCI_A0_VECTOR
__interrupt void USCI_A_ISR() {
GPIO_toggleOutputOnPin (GPIO_PORT_P4, GPIO_PIN7);
static char data[MAX_BUFFER_SIZE] = {0}; // 定义足够大的缓冲区
static int rx_index = 0; // 接收索引
// 接收数据并存储到缓冲区
data[rx_index++] = USCI_A_UART_receiveData(USCI_A0_BASE);
int dataLength = strlen(data);
while(i=1,1 < dataLength,i++)
{
UART_printf(USCI_A1_BASE, "%s",data[i]);
}
static int data_index = 0; // 数据索引
static int coordinate_index = 0; // 坐标索引
if (state == WAIT_HEADER)
{
// 构建消息头
for (i = 0; i < dataLength; i++)
{
current_header[data_index++] = data[i];
// 检查是否完整接收到消息头
if (strstr(current_header, "max_rect is :")!= NULL) {
state = RECEIVING_DATA;
coordinate_index = 0;
data_index = 0;
} else if (strstr(current_header, "rect_zx is :")!= NULL) {
state = RECEIVING_DATA;
coordinate_index = 0;
data_index = 0;
} else if (strstr(current_header, "max_blob is :")!= NULL) {
state = RECEIVING_DATA;
coordinate_index = 0;
data_index = 0;
} else if (data_index >= 20) { // 防止消息头过长
data_index = 0;
}
}
} else if (state == RECEIVING_DATA) {
for (i = 0; i < dataLength; i++) {
if (data[i] == ',') { // 遇到逗号,切换坐标维度
coordinate_index++;
if (coordinate_index > 1) { // 超出坐标维度,完成一组坐标接收
coordinate_index = 0;
if (strstr(current_header, "max_rect is :")!= NULL && data_index < 4) { // 存储 max_rect 的坐标
received_max_rect[data_index][0] = atoi(current_header);
received_max_rect[data_index][1] = atoi(current_header + data_index * 2);
data_index++;
} else if (strstr(current_header, "rect_zx is :")!= NULL) { // 存储 rect_zx 的坐标
received_rect_zx[0] = atoi(current_header);
received_rect_zx[1] = atoi(current_header + 2);
state = WAIT_HEADER; // 完成 rect_zx 坐标接收,回到等待消息头状态
} else if (strstr(current_header, "max_blob is :")!= NULL) { // 存储 max_blob 的坐标
received_max_blob[0] = atoi(current_header);
received_max_blob[1] = atoi(current_header + 2);
state = WAIT_HEADER; // 完成 max_blob 坐标接收,回到等待消息头状态
}
}
} else if (data[i] == ')') { // 遇到右括号,完成一组坐标接收
coordinate_index = 0;
if (strstr(current_header, "max_rect is :")!= NULL && data_index < 4) { // 存储 max_rect 的坐标
received_max_rect[data_index][0] = atoi(current_header);
received_max_rect[data_index][1] = atoi(current_header + data_index * 2);
data_index++;
if (data_index == 4) { // 完成所有 max_rect 坐标接收
state = WAIT_HEADER;
}
} else if (strstr(current_header, "rect_zx is :")!= NULL) { // 存储 rect_zx 的坐标
received_rect_zx[0] = atoi(current_header);
received_rect_zx[1] = atoi(current_header + 2);
state = WAIT_HEADER; // 完成 rect_zx 坐标接收,回到等待消息头状态
} else if (strstr(current_header, "max_blob is :")!= NULL) { // 存储 max_blob 的坐标
received_max_blob[0] = atoi(current_header);
received_max_blob[1] = atoi(current_header + 2);
state = WAIT_HEADER; // 完成 max_blob 坐标接收,回到等待消息头状态
}
} else {
current_header[data_index++] = data[i];
if (data_index >= 20) { // 防止数据过长
data_index = 0;
state = WAIT_HEADER;
}
}
}
}
// 清除接收中断标志位
UCA0IFG &= ~UCRXIFG;
// 处理接收完成的情况
if (rx_index >= MAX_BUFFER_SIZE || data[rx_index - 1] == '\0') {
rx_index = 0;
}
}
操作环境、软件版本等信息
软件为CCS(Code Composer Studio),用的单片机为MSP430F5529
尝试过的解决方法
之前想的是用字符串整体收入,然后再通过条件语句分类接收
我想要达到的结果
将接收到的数据(坐标)分别存储到各个数组里面