Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C STM32-Strstr函数未按预期工作(UART)_C_Stm32_Uart_Strstr - Fatal编程技术网

C STM32-Strstr函数未按预期工作(UART)

C STM32-Strstr函数未按预期工作(UART),c,stm32,uart,strstr,C,Stm32,Uart,Strstr,我试图在通过UART接收的另一个字符串中找到特定的字符串。然而,我的函数返回0,尽管字符串不在uart接收的字符串内。以下是我的功能: bool GetCommand(UART_HandleTypeDef *huart, char *command, char *getCommand, uint8_t size) { char *ptr; if (HAL_UART_Receive_IT(huart,command,size) == HAL_OK) { ptr =

我试图在通过UART接收的另一个字符串中找到特定的字符串。然而,我的函数返回0,尽管字符串不在uart接收的字符串内。以下是我的功能:

bool GetCommand(UART_HandleTypeDef *huart, char *command, char *getCommand, uint8_t size) {
    char *ptr;
    if (HAL_UART_Receive_IT(huart,command,size) == HAL_OK) {
        ptr = strstr(command,getCommand);
    }
    if (ptr) {
        return 1;
    } else {
        return 0;
    }
}
这个程序可以和gcc一起工作,但当我和Keil一起尝试时,它并没有像我预期的那样工作。你能帮我解决这个问题吗?

试试这个: 选择目标中的microlib选项

看这张照片

试试这个: 选择目标中的microlib选项

看这张照片


您的问题不在于函数
strstr()

这是您收集命令的方式

if(HAL\u UART\u接收它(huart,命令,大小)=HAL\u确定){
ptr=strstrstr(command,getCommand);
}
HAL\u UART\u Receive\u它是一个非阻塞函数,因此它在配置USART后直接返回。命令数组中的此字符串目前尚未定义


使用
HAL\u UART\u Receive()
HAL\u UART\u RxCpltCallback()
您的问题不在于函数
strstr()

这是您收集命令的方式

if(HAL\u UART\u接收它(huart,命令,大小)=HAL\u确定){
ptr=strstrstr(command,getCommand);
}
HAL\u UART\u Receive\u它是一个非阻塞函数,因此它在配置USART后直接返回。命令数组中的此字符串目前尚未定义


使用
HAL\u UART\u Receive()
HAL\u UART\u RxCpltCallback()

等待UART完成,然后再使用内存。不要使用未初始化的变量。除了另一个答案,您还可以轮询HAL_UART_状态,直到外围设备停止接收

bool GetCommand(UART_HandleTypeDef *huart, char *command, char *getCommand, uint8_t size) {
    if (HAL_UART_Receive_IT(huart,command,size) != HAL_OK) {
        // handle error
        return 0;
    }

    // wait until UART stops receiving
    while ((HAL_UART_State(huart) & HAL_UART_STATE_BUSY_RX) == HAL_UART_STATE_BUSY_RX) {
         continue;
    }

    return strstr(command, getCommand) != NULL;
}

在使用内存之前,请等待UART完成。不要使用未初始化的变量。除了另一个答案,您还可以轮询HAL_UART_状态,直到外围设备停止接收

bool GetCommand(UART_HandleTypeDef *huart, char *command, char *getCommand, uint8_t size) {
    if (HAL_UART_Receive_IT(huart,command,size) != HAL_OK) {
        // handle error
        return 0;
    }

    // wait until UART stops receiving
    while ((HAL_UART_State(huart) & HAL_UART_STATE_BUSY_RX) == HAL_UART_STATE_BUSY_RX) {
         continue;
    }

    return strstr(command, getCommand) != NULL;
}

谢谢你的回答。但是已经选择了microlib,首先,keil默认编译器是armcc,c-lib是microlib;您可以通过gcc运行这个程序员,所以我认为是c库导致了这个问题,可能是armcc c库中有一个bug。谢谢您的回答。但是已经选择了microlib,首先,keil默认编译器是armcc,c-lib是microlib;您可以通过gcc运行这个程序员,所以我认为c库导致了这个问题,也许armcc c lib中有一个bug这似乎是同一个问题:在使用strstrstr之前,请确保您的字符串是
nul
terminated这似乎是同一个问题:在使用strstrstr之前,请确保您的字符串是
nul
terminated我感谢您的回答。。。它像我以前计划的那样工作。祝你有一个愉快的一天:)我很感谢你的回答。。。它像我以前计划的那样工作。祝你今天愉快:)