简体   繁体   English

如何从 setup() 中的 Serial 读取字符数组并等待足够长的时间让用户点击发送?

[英]How do I read a char array from the Serial in setup() and wait long enough for the user to hit send?

I am using an Arduino Mega, and it will be bluetooth controlled with a Bluetooth Terminal app for Arduino.我正在使用 Arduino Mega,它将通过 Arduino 的蓝牙终端应用程序进行蓝牙控制。 What I want it to do is, when it starts up, at setup- I want it to prompt the user for a 4 digit PIN, and once the user is finished entering the pin and hits send the program should proceed further and not before that.我想要它做的是,当它启动时,在设置时 - 我希望它提示用户输入 4 位 PIN,一旦用户完成输入 PIN 并点击发送,程序应该继续进行,而不是在此之前. This is what I tried这是我试过的

unsigned char buff[4];

void getPassword() {

  int j = 0;

  Serial1.println("Enter the PIN: ");

  for (;;) {
    while (Serial1.available() > 0) {
      buff[j] = Serial1.read();
      j++;
    }

    if (j >= 3) {

      break;
    }
  }
}

void setup() {

getPassword();
}

It doesn't seem to work at all.它似乎根本不起作用。 How do I achieve this?我如何实现这一目标?

I notice that your password buffer is 4 chars long.我注意到您的密码缓冲区有 4 个字符长。 But you are only reading 3 characters from Serial.但是您只能从 Serial 中读取 3 个字符。 IS the password actually 3 or 4 characters long?密码实际上是 3 或 4 个字符长吗? If it is 4 characters long, then when j is 3 it isn't time to exit that loop yet.如果它是 4 个字符长,那么当 j 是 3 时还不是退出该循环的时候。 When j is 3 you are waiting for the fourth character.当 j 为 3 时,您正在等待第四个字符。 You want to exit AFTER you read the fourth character and j gets incremented again and becomes 4.您想在读取第四个字符后退出,并且 j 再次递增并变为 4。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将从串行读取的数据存储到arduino中的数组中? - How to store data read from serial into an array in arduino? 在发送数据之前,如何强制串口写入方法等待线路清除? - How do I force a serial port write method to wait for the line to clear before sending its data? 我如何通过设置蓝牙连接发送信息 - How do i send information via a setup bluetooth connection 如何向Bluez发送长通知,例如? - How do I send a long notification with Bluez, example? 如何从Android蓝牙向串行设备发送扩展ASCII AT命令(CCh)? - How do you send an extended-ascii AT-command (CCh) from Android bluetooth to a serial device? Arduino-如何从串行端口读取字符串 - Arduino - How to read a string from the Serial Port 如何从串行端口获取“总线报告的设备描述”和“已连接”? - How do I get "Bus Reported Device Description" and "is connected" from a serial port? 如何通过Android蓝牙串口RFCOMM将串行通信转储为文件 - How do I dump serial communication as file through Android bluetooth serial RFCOMM 如何将元数据从 ExoPlayer 发送到蓝牙? - How do I send metadata from ExoPlayer to bluetooth? 如何使用 sys.argv 和串行在以下 python 脚本中指定串行端口? - How do I specify a serial port in the following python script using sys.argv and serial?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM