【问题标题】:Building String from UART Characters - Home Learning C从 UART 字符构建字符串 - 家庭学习 C
【发布时间】:2020-03-27 16:31:52
【问题描述】:

我正在使用 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.

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

【问题讨论】:

    标签: string concatenation pic uart


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2015-02-06
      • 2014-02-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-01
      • 1970-01-01
      • 2013-12-11
      • 2017-11-30
      相关资源
      最近更新 更多