Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ Arduino只读取一个字符。_C++_Arduino - Fatal编程技术网

C++ Arduino只读取一个字符。

C++ Arduino只读取一个字符。,c++,arduino,C++,Arduino,该程序用于读取输入,并将其写入Arduino的串行监视器。问题是它只在串行监视器中写入一个字符 void setup() { Serial.begin(9600); //Set the serial monitor. lcd.begin(16, 2); //Set the LCD } // Alignment variables boolean left = true; //Set boolean left to true to begin to display text in middle o

该程序用于读取输入,并将其写入Arduino的串行监视器。问题是它只在串行监视器中写入一个字符

void setup()
{
Serial.begin(9600); //Set the serial monitor.
lcd.begin(16, 2); //Set the LCD
}
// Alignment variables
boolean left = true; //Set boolean left to true to begin to display text in middle of screen.
boolean right = false; //Other possible align booleans set to false
boolean select = false;
//Text show/hide variables
boolean show1 = true;  //Both values set to true to display on start up.
boolean show2 = true;
//Serial input
char serialinput [4] = {0}; //For 3 value input, and null character to end.
char line1;
void loop()
 {

 if (Serial.available() > 0) { //If the serial monitor is open it will read a value.
    line1 = Serial.read();
    Serial.print(line1);
    memmove (serialinput, &serialinput[1], 3); //copy the value to memory
    serialinput [2] = Serial.read(); //value is read.
    //if statements for each possible input.

}

将一个字节读到第1行,然后打印该字节。一行应该打印成循环或类似的形式。此外,该评论:

if (Serial.available() > 0) { 
//If the serial monitor is open it will read a value.

事实并非如此。Serial.available返回缓冲区中当前的字节数。因此,这一行:serialinput[2]=Serial.read;正在调用read wihtout,但正在验证是否存在要读取的内容。显然,每个可能的输入都有//if语句。在循环结束时,它们做什么?我怀疑没有逻辑来断言serialinput[]中某个特定字节的结束位置。在此处发布时向代码发送长度超过2字节的消息将覆盖这些值

Serial.read不会等待字符串结束。如果您试图读取[4]中暗示的几个字符,则只需在serial.read之后包含一个短延迟,以便缓冲区有足够的时间完全转储字符串

if (Serial.available() > 0) { //If the serial monitor is open it will read a value.
 line1 = Serial.read();
 delay(100);
 Serial.print(line1);
由于line1是一个字符,您希望它如何处理多个字符?