首页 > 解决方案 > 从 UART 字符构建字符串 - 家庭学习 C

问题描述

我正在使用 PIC 并成功通过 UART 接收到单个字符,但是我现在需要捕获传入的字符序列,建立一个字符串并在接收到 carrage return 后执行一个操作。我以前有一些 PHP 经验,并认为事情会比原来容易得多。

我已经使用工作 UART 函数修改了我的简单代码,以尝试如下连接接收到的字符并构建一个字符串:

#include "mcc_generated_files/mcc.h"
#include "string.h"

unsigned char InChar;
char Temp[5];
char Command[32];

void UART_Demo_Command_INT(void) 
{    
    if(eusartRxCount!=0) 
    {   
        InChar=EUSART_Read();  // read a byte for RX
        strcat(Command,InChar); //concat Command with InChar, result in Command
        printf = printf("Command String: %s \n", Command);
    }  

}

我收到许多错误:

UART_Demo.c:115:24: warning: incompatible integer to pointer conversion passing 'unsigned char' to parameter of type 'const char *' [-Wint-conversion]
        strcat(Command,InChar); //concat Command with InChar, result in Command
                       ^~~~~~
C:\Program Files (x86)\Microchip\xc8\v2.10\pic\include\c99\string.h:36:55: note: passing argument to parameter here
char *strcat (char *__restrict, const char *__restrict);
                                                      ^
UART_Demo.c:116:16: error: non-object type 'int (const char *restrict, ...)' is not assignable
        printf = printf("Command String: %s \n", Command);
        ~~~~~~ ^
1 warning and 1 error generated.

在花了几个小时试图解决这个问题并且无处可去之后,我希望你们中的一些人能提供帮助。

标签: stringconcatenationpicuart

解决方案


如果有人感兴趣,问题是因为我收到的字符只是...字符,而不是通常在末尾有终止 \0 字符的字符串,因此 strcat (连接字符串)函数不起作用。

作为修复,我只是将 \0 添加到 InChar 的末尾,然后 strcat 工作。


推荐阅读