我正在手机上的终端和连接到微 Controller 的蓝牙模块之间进行传输和接收(pic18f)。
我设置了一个 4 个字符的数组,char input[4] = ""
来存储来自终端的用户输入。函数 getsUSART(input,4)
将输入放在那里。
问题是,当我检查输入内容时,当我在终端中输入 ABC 时,我发现这是“ABC/UN”。/UN 是转义序列吗?如果是的话,它的用途是什么?
另外,如果我想将输入与字符串进行比较,假设该字符串是“hey”
我会先执行 char str1[4] = "hey/UN"
然后执行 strcmp(input, str1) == 0
吗?
编辑:
所以编译器库(USART)可以在这里访问,第66页我相信:http://ww1.microchip.com/downloads/en/DeviceDoc/MPLAB_C18_Libraries_51297f.pdf
这是相关代码,仅用于获取输入并放置 null:
char input[4] = "";
while(BusyUSART());
while (!DataRdyUSART());
getsUSART(input, 4); //Get the input.
// NULL terminate the string for putsUSART call.
input[4] = '/0';
putrsUSART("Input:");
putsUSART(input);
当我执行 input[4] = '/0' 时,我试图以 null 终止它,如果它不是 null 终止,我在这里做错了什么吗?
最佳答案
你忘记了 C 从 0 开始计数。
所以在你的例子中:
input[0] = 'A'
input[1] = 'B'
input[2] = 'C'
input[3] = some old byte of data
input[4] = '\0'
关于c - 在c程序中接收后字符串以/UN结尾?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49981926/