从UART字符构建字符串-家庭学习C

问题描述 投票:0回答:1

我正在与PIC一起工作,并且已经通过UART成功接收到一个字符,但是现在我需要捕获传入的字符序列,构建一个字符串,并在收到返回角数后执行操作。我以前有过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.

[花了几个小时尝试解决这个问题之后,我一无所获,希望您能对您有所帮助。

string concatenation pic uart
1个回答
0
投票

[如果有人感兴趣,问题是因为我收到的字符只是...字符,而不是通常在结尾处以\ 0结尾的字符串,因此strcat(包含字符串)功能不起作用。

作为修复,我只是在InChar的末尾添加\ 0,然后strcat起作用。

© www.soinside.com 2019 - 2024. All rights reserved.