string - 从 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.
在花了几个小时试图解决这个问题并且无处可去之后,我希望你们中的一些人能提供帮助。
解决方案
如果有人感兴趣,问题是因为我收到的字符只是...字符,而不是通常在末尾有终止 \0 字符的字符串,因此 strcat (连接字符串)函数不起作用。
作为修复,我只是将 \0 添加到 InChar 的末尾,然后 strcat 工作。
推荐阅读
- ios - 从外部静态函数在 ViewController 上显示警报
- visual-studio-code - Mac OS 10.11 上的 Visual Studio Code Live Share
- python - 二维直方图中颜色条的数量是多少?
- javascript - 从没有 XHR 的 HTML 标记中获取图像的数据 URI
- c# - 表情丰富的成员怪案
- python - 使用按钮重新启动 Tkinter 中的程序
- java - AWS - 带有 $ 的 Elastic Beanstalk 环境属性
- c++ - 为什么条件为真时 x 的值没有变化?
- post - 为什么在使用 Redux Store 的 Fetch 请求中未定义响应
- python - 尝试下载文件时出现“无法获取”错误