将单个字符串/字符输入串行监视器

Posted

技术标签:

【中文标题】将单个字符串/字符输入串行监视器【英文标题】:Inputting a single String/Char into Serial Monitor 【发布时间】:2017-08-16 00:09:15 【问题描述】:

我在将字符串/字符输入串行监视器时遇到问题,我是编程初学者,因此不知道如何解决。

我有一个 if/else 语句,它的作用类似于 switch case 语句。我确实从一个 switch case 开始,但我遇到了很多问题,其中一个是输入一个字符串/字符(A、S、M、D、P)来指向每个 case。我知道 switch case 是针对 int 的,所以我放弃了,转而使用多个 if/else 语句。

我已经尝试过(在 void loop() 中),

char theOperator = dataInput();

if (theOperator == 'A') 
  // If input is equal to 1, carry out function 'addition()'
  addition();

但它会显示我在“添加”函数中的错误消息。

这里是使用输入代码的地方(全局定义),

char theOperator;

char dataInput() 
  while (!Serial.available()) 
    // Wait for the user to enter value
  
  return Serial.parseInt();

我尝试过类似 Serial.read() 之类的东西,但这要么循环我的程序的开头并且不允许我输入值,要么根本不接受值。

任何帮助将不胜感激,我将在下面发布我的完整代码,以便您了解我的程序(if/else 等问题在顶部)。

*编辑:忘了说,我可以做类似的事情,

char theOperator = dataInput();

  if (theOperator == 1) 
    // If input is equal to 1, carry out function 'addition()'
    addition();
  

这很有效。但是我希望它取 A、S、M、D 或 P 而不是 1、2、3、4、5。

#define LED_RED 5
#define LED_ORANGE 6
#define LED_YELLOW 9
#define LED_GREEN 10
#define LED_BLUE 11

int potValue;

#define potPin A0

// Limits the range of the numbers entered to between -99 and 999
long firstNumber = constrain(firstNumber, -99, 999);
long secondNumber = constrain(secondNumber, -99, 999);
long value;

char theOperator;

char dataInput() 
  while (!Serial.available()) 
    // Wait for the user to enter value
  
  return Serial.parseInt();


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void setup() 

  Serial.begin(9600);

  pinMode(LED_RED, OUTPUT);
  pinMode(LED_ORANGE, OUTPUT);
  pinMode(LED_YELLOW, OUTPUT);
  pinMode(LED_GREEN, OUTPUT);
  pinMode(LED_BLUE, OUTPUT);

  pinMode(potPin, INPUT);


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void loop() 

  Serial.println ("Enter your chosen operator");
  Serial.println ("A for Addition, S for Subtraction, M for Multiplication,");
  Serial.println ("D for Division, and P for Potentiometer: ");

  // Takes an input from the user
  char theOperator = dataInput();

  if (theOperator == 'A') 
    // If input is equal to 1, carry out function 'addition()'
    addition();
  
  else if (theOperator == 'S') 
    // If input is equal to 2, carry out function 'subtraction()'
    subtraction();
  
  else if (theOperator == 'M') 
    // If input is equal to 3, carry out function 'multiplication()'
    multiplication();
  
  else if (theOperator == 'D') 
    // If input is equal to 4, carry out function 'division()'
    division();
  
  else if (theOperator == 'P') 
    // If input is equal to 5, carry out function 'potentiometer()'
    potentiometer();
  


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void addition() 

  Serial.println ("A");
  Serial.println ("Please enter first number in format nnn: ");
  firstNumber = dataInput(); // Asks the user to input the first set of numbers

  /* Message must be in format cnnnnnn
     therefore first number must be greater than or equal to -99 and less than or equal to 999*/
  if (firstNumber >= -99 && firstNumber <= 999) 
    Serial.println (firstNumber); // Prints the first set of numbers for the user to view
  
  else 
    /* If the data input does not match the format cnnnnnn then this error message will display
       if the input is invalid the red LED will also turn on */
    digitalWrite(LED_RED, HIGH);
    Serial.println ("--------------- ERROR ---------------");
  

  Serial.println ("Please enter second number in format nnn: ");
  secondNumber = dataInput(); // Asks the user to input the second set of numbers

  /* Message must be in format cnnnnnn
     therefore second number must be greater than or equal to -99 and less than or equal to 999*/
  if (secondNumber >= -99 && secondNumber <= 999) 
    // The LED will turn off if it was previously on because this is a valid input
    digitalWrite(LED_RED, LOW);
    Serial.println (secondNumber); // Prints the second set of numbers for the user to view
  
  else 
    digitalWrite(LED_RED, HIGH);
    Serial.println ("--------------- ERROR ---------------");
  

  /* Give time for the red error LED to stay on
     for the user to notice he/she has made an invalid input */
  delay(500);
  digitalWrite(LED_RED, LOW);

  // As this case is for addition, it will add the first and second numbers
  value = (firstNumber + secondNumber);

  Serial.print("Value: ");
  // Prints the value of the two sets of numbers so that the user can see the value of their message
  Serial.println(value);

  /////// The 'sign' flag ///////
  if (value >= 1) 
    // If value is a positive number (excluding 0) turn orange LED on
    digitalWrite(LED_ORANGE, HIGH);
  
  else if (value <= -1) 
    // If value is a negative number, turn orange LED off
    digitalWrite(LED_ORANGE, LOW);
  
  else if (value == 0) 
    // If value is equal to 0, set orange LED at half brightness
    int brightness;
    // Changes the brightness of the LED so that it is half bright
    brightness++;
    brightness %= 128;
    analogWrite(LED_ORANGE, brightness);
  

  /////// The 'even' flag ///////
  if ((value % 2) == 0) 
    // If the modulo is equal to 0, it's an even
    digitalWrite(LED_BLUE, HIGH);
   else 
    // If the modulo is not equal to 0, it's an odd
    digitalWrite(LED_BLUE, LOW);
  


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void subtraction() 

  Serial.println ("S");
  Serial.println ("Please enter the first number in format nnn: ");
  firstNumber = dataInput();

  /* Message must be in format cnnnnnn
     therefore first number must be greater than or equal to -99 and less than or equal to 999*/
  if (firstNumber >= -99 && firstNumber <= 999) 
    Serial.println (firstNumber); // Prints the first set of numbers for the user to view
  
  else 
    /* If the data input does not match the format cnnnnnn then this error message will display
       if the input is invalid the red LED will also turn on */
    digitalWrite(LED_RED, HIGH);
    Serial.println ("--------------- ERROR ---------------");
  

  Serial.println ("Please enter second number in format nnn: ");
  secondNumber = dataInput();

  /* Message must be in format cnnnnnn
     therefore second number must be greater than or equal to -99 and less than or equal to 999*/
  if (secondNumber >= -99 && secondNumber <= 999) 
    digitalWrite(LED_RED, LOW);
    Serial.println (secondNumber); // Prints the second set of numbers for the user to view
  
  else 
    digitalWrite(LED_RED, HIGH);
    Serial.println ("--------------- ERROR ---------------");
  

  /* Give time for the red error LED to stay on
     for the user to notice he/she has made an invalid input */
  delay(500);
  digitalWrite(LED_RED, LOW);

  // As this case is for subtraction, it will subtract the first and second numbers
  value = (firstNumber - secondNumber);
  Serial.print("Value: ");
  // Prints the value of the two sets of numbers so that the user can see the value of their message
  Serial.println(value);

  /////// The 'sign' flag ///////
  if (value >= 1) 
    // If value is a positive number (excluding 0) turn orange LED on
    digitalWrite(LED_ORANGE, HIGH);
  
  else if (value <= -1) 
    // If value is a negative number, turn orange LED off
    digitalWrite(LED_ORANGE, LOW);
  
  else if (value == 0) 
    // If value is equal to 0, set orange LED at half brightness
    int brightness;
    // Changes the brightness of the LED so that it is half bright
    brightness++;
    brightness %= 128;
    analogWrite(LED_ORANGE, brightness);
  

  /////// The 'even' flag ///////
  if ((value % 2) == 0) 
    // If the modulo is equal to 0, it's an even
    digitalWrite(LED_BLUE, HIGH);
   else 
    // If the modulo is not equal to 0, it's an odd
    digitalWrite(LED_BLUE, LOW);
  


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void multiplication() 

  Serial.println ("M");
  Serial.println ("Please enter the first number in format nnn: ");
  firstNumber = dataInput();

  /* Message must be in format cnnnnnn
     therefore first number must be greater than or equal to -99 and less than or equal to 999*/
  if (firstNumber >= -99 && firstNumber <= 999) 
    Serial.println (firstNumber); // Prints the first set of numbers for the user to view
  
  else 
    /* If the data input does not match the format cnnnnnn then this error message will display
       if the input is invalid the red LED will also turn on */
    digitalWrite(LED_RED, HIGH);
    Serial.println ("--------------- ERROR ---------------");
  

  Serial.println ("Please enter second number in format nnn: ");
  secondNumber = dataInput();

  /* Message must be in format cnnnnnn
     therefore second number must be greater than or equal to -99 and less than or equal to 999*/
  if (secondNumber >= -99 && secondNumber <= 999) 
    digitalWrite(LED_RED, LOW);
    Serial.println (secondNumber); // Prints the second set of numbers for the user to view
  
  else 
    digitalWrite(LED_RED, HIGH);
    Serial.println ("--------------- ERROR ---------------");
  

  /* Give time for the red error LED to stay on
     for the user to notice he/she has made an invalid input */
  delay(500);
  digitalWrite(LED_RED, LOW);

  // As this case is for multiplication, it will multiply the first and second numbers
  value = (firstNumber * secondNumber);
  Serial.print("Value: ");
  // Prints the value of the two sets of numbers so that the user can see the value of their message
  Serial.println(value);

  /////// The 'sign' flag ///////
  if (value >= 1) 
    // If value is a positive number (excluding 0) turn orange LED on
    digitalWrite(LED_ORANGE, HIGH);
  
  else if (value <= -1) 
    // If value is a negative number, turn orange LED off
    digitalWrite(LED_ORANGE, LOW);
  
  else if (value == 0) 
    // If value is equal to 0, set orange LED at half brightness
    int brightness;
    // Changes the brightness of the LED so that it is half bright
    brightness++;
    brightness %= 128;
    analogWrite(LED_ORANGE, brightness);
  

  /////// The 'even' flag ///////
  if ((value % 2) == 0) 
    // If the modulo is equal to 0, it's an even
    digitalWrite(LED_BLUE, HIGH);
   else 
    // If the modulo is not equal to 0, it's an odd
    digitalWrite(LED_BLUE, LOW);
  


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void division() 

  Serial.println ("D");
  Serial.println ("Please enter the first number in format nnn: ");
  firstNumber = dataInput();

  /* Message must be in format cnnnnnn
     therefore first number must be greater than or equal to -99 and less than or equal to 999
     Division by 0 is not allowed */
  if (firstNumber >= -99 && firstNumber <= 999 && firstNumber != 0) 
    Serial.println (firstNumber); // Prints the first set of numbers for the user to view
  
  else if (firstNumber == 0) 
    /* If the data input does not match the format cnnnnnn or if the input is
       equal to 0 then this error message will display, and if the input is invalid
       the red LED will also turn on, or if the input equals to 0 the LED will flash
       and stay on at half brightness */
    blinkError();
    Serial.println ("--------------- ERROR ---------------");
  
  else 
    digitalWrite(LED_RED, HIGH);
  

  Serial.println ("Please enter second number in format nnn: ");
  secondNumber = dataInput();

  /* Message must be in format cnnnnnn
     therefore second number must be greater than or equal to -99 and less than or equal to 999
     Division by 0 is not allowed */
  if (secondNumber >= -99 && secondNumber <= 999 && secondNumber != 0) 
    digitalWrite(LED_RED, LOW);
    Serial.println (secondNumber); // Prints the second set of numbers for the user to view
  
  else if (secondNumber == 0) 
    blinkError();
    Serial.println ("--------------- ERROR ---------------");
  
  else 
    digitalWrite(LED_RED, HIGH);
    Serial.println ("--------------- ERROR ---------------");
  

  /* Give time for the red error LED to stay on
     for the user to notice he/she has made an invalid input */
  delay(500);
  digitalWrite(LED_RED, LOW);

  // As this case is for division, it will divide the first and second numbers
  value = (firstNumber / secondNumber);
  Serial.print("Value: ");
  // Prints the value of the two sets of numbers so that the user can see the value of their message
  Serial.println(value);

  /////// The 'sign' flag ///////
  if (value >= 1) 
    // If value is a positive number (excluding 0) turn orange LED on
    digitalWrite(LED_ORANGE, HIGH);
  
  else if (value <= -1) 
    // If value is a negative number, turn orange LED off
    digitalWrite(LED_ORANGE, LOW);
  
  else if (value == 0) 
    // If value is equal to 0, set orange LED at half brightness
    int brightness;
    // Changes the brightness of the LED so that it is half bright
    brightness++;
    brightness %= 128;
    analogWrite(LED_ORANGE, brightness);
  

  /////// The 'even' flag ///////
  if ((value % 2) == 0) 
    // If the modulo is equal to 0, it's an even
    digitalWrite(LED_BLUE, HIGH);
   else 
    // If the modulo is not equal to 0, it's an odd
    digitalWrite(LED_BLUE, LOW);
  


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void potentiometer() 

  Serial.println("P");

  do 
    if (potValue >= 0 && potValue <= 6) 
      /* If the value on the potentiometer is greater than or equal to 0,
         or less than or equal to 6, the red LED will turn on */

      potValue = analogRead(potPin); // Reads the value on the pin
      potValue = map(potValue, 0, 1023, 0, 31); // Changes the default range from 0 to 1023 to 0 to 31

      // Converts the DEC value of the potentiometer to BIN
      Serial.println(potValue, BIN);
      delay(500);

      digitalWrite(LED_RED, HIGH);
      digitalWrite(LED_ORANGE, LOW);
      digitalWrite(LED_YELLOW, LOW);
      digitalWrite(LED_GREEN, LOW);
      digitalWrite(LED_BLUE, LOW);
    
    else if (potValue >= 7 && potValue <= 12) 
      /* If the value on the potentiometer is greater than or equal to 3,
         or less than or equal to 12, the orange LED will turn on */

      potValue = analogRead(potPin); // Reads the value on the pin
      potValue = map(potValue, 0, 1023, 0, 31); // Changes the default range from 0 to 1023 to 0 to 31

      Serial.println(potValue, BIN);
      delay(500);

      digitalWrite(LED_RED, LOW);
      digitalWrite(LED_ORANGE, HIGH);
      digitalWrite(LED_YELLOW, LOW);
      digitalWrite(LED_GREEN, LOW);
      digitalWrite(LED_BLUE, LOW);
    
    else if (potValue >= 13 && potValue <= 18) 
      /* If the value on the potentiometer is greater than or equal to 13,
         or less than or equal to 18, the yellow LED will turn on */

      potValue = analogRead(potPin); // Reads the value on the pin
      potValue = map(potValue, 0, 1023, 0, 31); // Changes the default range from 0 to 1023 to 0 to 31

      Serial.println(potValue, BIN);
      delay(500);

      digitalWrite(LED_RED, LOW);
      digitalWrite(LED_ORANGE, LOW);
      digitalWrite(LED_YELLOW, HIGH);
      digitalWrite(LED_GREEN, LOW);
      digitalWrite(LED_BLUE, LOW);
    
    else if (potValue >= 19 && potValue <= 24) 
      /* If the value on the potentiometer is greater than or equal to 19,
         or less than or equal to 24, the green LED will turn on */

      potValue = analogRead(potPin); // Reads the value on the pin
      potValue = map(potValue, 0, 1023, 0, 31); // Changes the default range from 0 to 1023 to 0 to 31

      Serial.println(potValue, BIN);
      delay(500);

      digitalWrite(LED_RED, LOW);
      digitalWrite(LED_ORANGE, LOW);
      digitalWrite(LED_YELLOW, LOW);
      digitalWrite(LED_GREEN, HIGH);
      digitalWrite(LED_BLUE, LOW);
    
    else if (potValue >= 25 && potValue <= 31) 
      /*If the value on the potentiometer is greater than or equal to 25,
        or less than or equal to 31, the blue LED will turn on */

      potValue = analogRead(potPin); // Reads the value on the pin
      potValue = map(potValue, 0, 1023, 0, 31); // Changes the default range from 0 to 1023 to 0 to 31

      Serial.println(potValue, BIN);
      delay(500);

      digitalWrite(LED_RED, LOW);
      digitalWrite(LED_ORANGE, LOW);
      digitalWrite(LED_YELLOW, LOW);
      digitalWrite(LED_GREEN, LOW);
      digitalWrite(LED_BLUE, HIGH);
    
   while (true);


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void blinkError() 

  /////// The 'error' flag ///////

  int brightness = 0;

  // The for loop will loop 5 times
  for (int x = 0; x < 5; x++) 

    digitalWrite(LED_RED, HIGH);
    delay(100);
    digitalWrite(LED_RED, LOW);
    delay(100);

    // Changes the brightness of the LED so that it is half bright
    brightness++;
    brightness %= 128;
    analogWrite(LED_RED, brightness);
  

【问题讨论】:

如何输入?你看到了什么错误信息? 我正在使用 arduino 和 arduino IDE。当我打开串行监视器时,它要求用户输入 A、S、M、D 或 P。当我当前输入 A 时,它会输出错误消息--------ERROR----- - 我在每个功能中都有,然后要求用户再次输入。如果我将它保留在 1、2、3、4、5 它可以工作,然后继续要求用户输入第一个数字等。我希望它接受字符串输入,但我已经尝试过了,但由于某种原因无法做到. 【参考方案1】:

在您的char dataInput() 函数中,您使用Serial.parseInt() 进行串行输入,当输入'A' parseInt 时将返回0。请参阅documentation。要读取字符,请使用 Serial.read() 获取单个字符。

void loop() 

  Serial.println ("Enter your chosen operator");
  Serial.println ("A for Addition, S for Subtraction, M for Multiplication,");
  Serial.println ("D for Division, and P for Potentiometer: ");

  // Takes an input from the user
  char theOperator = Serial.read();

  switch(theOperator) 
    case 'A':
    // If input is equal to 1, carry out function 'addition()'
      addition();
      break;
    case 'S':
    // If input is equal to 2, carry out function 'subtraction()'
      subtraction();
      break;
    case 'M':
    // If input is equal to 3, carry out function 'multiplication()'
      multiplication();
      break;
    case 'D':
    // If input is equal to 4, carry out function 'division()'
      division();
      break;
  case 'P':
    // If input is equal to 5, carry out function 'potentiometer()'
    potentiometer();
    break;
  

【讨论】:

我已将 Serial.parseInt() 更改为 Serial.read() 并离开 theOperator = dataInput();因为如果我在循环中将 dataInput() 更改为 Serial.read() 或按照全局定义,它只会循环“输入您选择的运算符”并且不允许我输入。但是,如果我将其保留为 dataInput(),我可以输入 A、S、M、D 和 P,但是当我随后输入第一个数字时,它会输出奇怪的值,例如 1 是 49,而 123 是---- -ERROR----- 并添加这两个并输出 98?我很困惑。 不要改变数据输入,只改变循环中读取的字符。 我不太明白我要改变什么

以上是关于将单个字符串/字符输入串行监视器的主要内容,如果未能解决你的问题,请参考以下文章

如何将浮点值从一个蓝牙模块发送到其他模块(HC 05)

arduino_1.0.1的串口监视器应该怎么用?

怎么在arduino串口监视器上实现输入一个字符一次某引脚为高电平,再输入一次变为低电平?

port.ReadLine() - 不更新

为啥我的 python 串行代码不起作用,而从 arduino 串行监视器发送相同的数据?

多线程 Python Tkinter 串行监视器中的按钮问题