ECruoyan 2024-07-06 16:06 采纳率: 0%
浏览 1

(标签-单片机|关键词-字符串)

问题遇到的现象和发生背景

再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

尝试过的解决方法

之前想的是用字符串整体收入,然后再通过条件语句分类接收

我想要达到的结果

将接收到的数据(坐标)分别存储到各个数组里面

  • 写回答

2条回答 默认 最新

  • 专家-赤兔[在线] 全栈领域优质创作者 2024-07-06 16:06
    关注

    引自免费微信小程序:皆我百晓生

    从您提供的代码来看,您在MSP430F5529单片机与OPENMV的串口通信上遇到了不接收或接收乱码的问题。让我们一步步分析并尝试解决问题。

    首先,关于接收乱码的问题,可能是由于串口接收数据的格式或编码方式不匹配导致的。请确保发送方和接收方的数据格式和编码方式一致。

    接下来,让我们分析您的中断处理函数代码。代码的主要逻辑是根据接收到的数据构建消息头,并根据消息头的内容处理接收到的坐标数据。然而,有几个潜在的问题需要注意:

    1. 在您的代码中,i变量在多个地方被使用,但没有事先声明。请确保在使用i之前进行声明。
    2. 在使用strstr函数时,您比较的是字符串的一部分,而不是整个字符串。这可能会导致不准确的匹配。建议将完整的消息头字符串存储在另一个数组中,并使用完整的消息头字符串进行比较。
    3. 在处理接收到的数据时,您使用了静态变量来存储数据和坐标索引。请确保这些变量的初始值正确设置,以避免潜在的错误。

    针对这些问题,我提供以下修改建议:

    1. 在使用i变量之前进行声明。
    2. 使用完整的消息头字符串进行比较,确保准确的匹配。
    3. 检查并确认静态变量的初始值设置正确。

    此外,为了更好地帮助您解决问题,我建议您检查以下几点:

    1. 确保串口通信的波特率设置正确,并且发送方和接收方的波特率匹配。
    2. 检查发送方的数据格式和编码方式是否正确。
    3. 使用调试工具或串口监视器检查接收到的原始数据,以确定是否接收到乱码或丢失数据。

    如果您能提供更多的关于串口通信的细节,例如发送方的代码、串口通信的协议等,我将更有可能提供更具体的帮助。同时,您可以尝试根据以上分析进行修改和优化代码,然后重新测试以查看是否解决了问题。

    评论 编辑记录

报告相同问题?

问题事件

  • 创建了问题 7月6日

悬赏问题

  • ¥15 安霸cv22 + rtl8211f 千兆,udp传输丢包
  • ¥15 关于区块链和边缘环境搭建的相关问题
  • ¥15 windows远程桌面断卡重连软件卡顿问题
  • ¥30 Unity 实现扫描效果
  • ¥15 HbuilderX检测不到安卓模拟器
  • ¥15 这个main已经在filename.obj中定义是什么错 C语言
  • ¥15 关于#linux#的问题:exsi8.0系统 怎么更改web访问端口,不用80、443
  • ¥15 使用elementor设计样式
  • ¥15 谁能提供一个中文版的推销咨询网站连接?
  • ¥15 springboot项目程序启动报错