gpt4 book ai didi

c - UART1_Read_Text() 问题

转载 作者:太空宇宙 更新时间:2023-11-04 05:57:32 26 4
gpt4 key购买 nike

我正在使用 PICKit3、MikroC PRO for PIC v6.0.0、PIC18F45 @ 8MHz 和 RN-42 蓝牙模块。我正在从笔记本电脑和我的 Android 应用程序连接到 RN-42 模块,我正在发送密码。奇怪的是PIC有时说密码是正确的,但有时却不正确。我正在发送相同的字符串。测试时,碰巧只有第一次没有接受通过,所有其他尝试都被接受。 PIC 和 RN-42 上的 UART 波特率都设置为 9600。 RN-42 上的 RTS 和 CTS 未连接。

我也试过:

char password[] = "abc";
char password[4] = "abc";
char password[5] = "abc\0";
char *password = "abc\0";

strcmp(input, "\0"); after the UART1_Read_Text(input, "|", MAX_UART_RX_CHARACTERS);

但它是一样的...可能是什么问题,是在我的代码中还是可能在 MikroC 函数内部发出?

void authenticate() {
char input[MAX_UART_RX_CHARACTERS + 1] = "";
char password[] = "abc\0";
unsigned char ready = 0;

Delay_ms(500);
UART1_Write_Text("TRobot\n");

while (connected && !ready) {
if (UART1_Data_Ready()) {
TMR0ON_bit = 0;
time_out = 0;
UART1_Read_Text(input, "|", MAX_UART_RX_CHARACTERS);

if (strcmp(input, password) == 0) {
UART1_Write('y');
serve();
} else {
UART1_Write('n');
disconnect();
}

ready = 1;
}

if (time_out) {
disconnect();
}
}
}

UARTx_Read_Text() 原型(prototype):

void UARTx_Read_Text(char *Output, char *Delimiter, char Attempts);

UARTx_Read_Text() 说明:

读取通过 UART 接收的字符,直到检测到定界符序列。读取序列存储在参数输出中;定界符序列存储在参数定界符中。

这是一个阻塞调用:需要定界符序列,否则过程退出(如果未找到定界符)。

参数:

输出:收到的文本定界符:标识接收到的字符串结尾的字符序列尝试:定义接收到的字符数,其中需要分隔符序列。如果 Attempts 设置为 255,则此例程将不断尝试检测 Delimiter 序列。

例子:

读取文本直到收到序列“OK”,然后发回收到的内容:

UART1_Init(4800);                         // initialize UART1 module
Delay_ms(100);

while (1) {
if (UART1_Data_Ready() == 1) { // if data is received
UART1_Read_Text(output, "OK", 10); // reads text until 'OK' is found
UART1_Write_Text(output); // sends back text
}
}

最佳答案

可能 UART1_Read_Text() 没有按预期运行。我查找了该(非标准)函数的文档/源代码,但没有找到。

确保它在用户按下回车键时使用 '\0' 字符正确终止 input

关于c - UART1_Read_Text() 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24524930/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com