私のソースコードで非常に奇妙なことが起こります。次の関数はうまく機能し、パスワードが正しい場合は 'y' を出力し、正しくない場合は 'n' を出力します。しかし、いくつかの UART1_Write および Delay 関数を else ステートメントに追加すると、バグが出てきて、パスワードが "zxc" (正しい) であっても、常に else ステートメントに入ります。私は PIC v6.0.0 用に MikroC PRO を使用しています。ロボット システムは PIC18F452 とそれに接続された RN-42 Bluetooth モジュールで構成されています。BluetoothとTeraTermを備えたラップトップでテストしています。
詳細情報: http://instagram.com/p/pLnU9eDL8z/#
これはうまく機能するルーチンです。
void authenticate() {
char *input = "";
char *password = "zxc\0";
unsigned char ready = 0;
while (connected && !ready) {
if (UART1_Data_Ready()) {
UART1_Read_Text(input, "|", 17);
strcat(input, "\0");
if (strcmp(input, password) == 0) {
UART1_Write('y');
ready = 1;
} else {
UART1_Write('n');
ready = 1;
}
}
}
}
このバージョンのルーチンは、常に strcmp(input, password) == 0 の部分の ELSE ステートメントに入ります。
void authenticate() {
char *input = "";
char *password = "zxc\0";
unsigned char ready = 0;
while (connected && !ready) {
if (UART1_Data_Ready()) {
UART1_Read_Text(input, "|", 17);
strcat(input, "\0");
if (strcmp(input, password) == 0) {
UART1_Write('y');
ready = 1;
} else {
UART1_Write('n');
Delay_ms(100);
UART1_Write('$');
Delay_ms(100);
UART1_Write('$');
Delay_ms(100);
UART1_Write('$');
Delay_ms(100);
UART1_Write('K');
Delay_ms(100);
UART1_Write(',');
Delay_ms(100);
UART1_Write('-');
Delay_ms(100);
UART1_Write('-');
Delay_ms(100);
UART1_Write('-');
Delay_ms(100);
UART1_Write('\n');
ready = 1;
}
}
}
}
RN-42 をコマンド モードにして、パスワードが間違っている場合にユーザーを切断するには、これらすべての追加シンボルを送信することが重要です。問題を解決するのを手伝ってください。どんなアイデアでも大歓迎です!