使用Arduino将serial.read()转换为可用字符串

77
我正在使用两个Arduino板,使用NewSoftSerialRF收发器相互发送纯文本字符串。
每个字符串可能有20-30个字符长度。如何将Serial.read()转换为字符串,以便我可以执行if x == "testing statements"等操作?

请检查我下面的答案,它比你选择的答案更直接/简单。 - Ihab
15个回答

1

serial.read()上使用字符串追加运算符,它比string.concat()效果更好。

char r;
string mystring = "";

while(serial.available()){
    r = serial.read();
    mystring = mystring + r; 
}

在将流保存到字符串中(在这种情况下是mystring)后,使用SubString函数提取您要查找的内容。

1
如果您使用连接方法,则在使用if else方法时不要忘记修剪字符串。

0

许多伟大的答案,这是我对问题中要求的确切功能的两分钱。

此外,它应该更容易阅读和调试。

代码经过128个字符的输入测试。

在Arduino uno r3(Arduino IDE 1.6.8)上测试。

功能:

  • 使用串行命令输入打开或关闭Arduino板载led(引脚13)。

命令:

  • LED.ON
  • LED.OFF

注意:记得根据你的板速度更改波特率。

// Turns Arduino onboard led (pin 13) on or off using serial command input.

// Pin 13, a LED connected on most Arduino boards.
int const LED = 13;

// Serial Input Variables
int intLoopCounter = 0;
String strSerialInput = "";

// the setup routine runs once when you press reset:
void setup() 
{
  // initialize the digital pin as an output.
  pinMode(LED, OUTPUT);

  // initialize serial port
  Serial.begin(250000); // CHANGE BAUD RATE based on the board speed.

  // initialized
  Serial.println("Initialized.");
}

// the loop routine runs over and over again forever:
void loop() 
{
  // Slow down a bit. 
  // Note: This may have to be increased for longer strings or increase the iteration in GetPossibleSerialData() function.
  delay(1);
  CheckAndExecuteSerialCommand();  
}

void CheckAndExecuteSerialCommand()
{
  //Get Data from Serial
  String serialData = GetPossibleSerialData();
  bool commandAccepted = false;

  if (serialData.startsWith("LED.ON"))
  {
    commandAccepted = true;
    digitalWrite(LED, HIGH);   // turn the LED on (HIGH is the voltage level)
  }
  else if  (serialData.startsWith("LED.OFF"))
  {
    commandAccepted = true;
    digitalWrite(LED, LOW);    // turn the LED off by making the voltage LOW
  }
  else if (serialData != "")
  {
    Serial.println();
    Serial.println("*** Command Failed ***");
    Serial.println("\t" + serialData);
    Serial.println();
    Serial.println();
    Serial.println("*** Invalid Command ***");
    Serial.println();
    Serial.println("Try:");
    Serial.println("\tLED.ON");
    Serial.println("\tLED.OFF");
    Serial.println();
  }

  if (commandAccepted)
  {
    Serial.println();
    Serial.println("*** Command Executed ***");
    Serial.println("\t" + serialData);
    Serial.println();
  }
}

String GetPossibleSerialData()
{
  String retVal;
  int iteration = 10; // 10 times the time it takes to do the main loop
  if (strSerialInput.length() > 0)
  {
    // Print the retreived string after looping 10(iteration) ex times
    if (intLoopCounter > strSerialInput.length() + iteration)
    {
        retVal = strSerialInput;
        strSerialInput = "";
        intLoopCounter = 0;
    } 
    intLoopCounter++;
  }

  return retVal;
}

void serialEvent()
{  
  while (Serial.available())
  {    
    strSerialInput.concat((char) Serial.read());
  } 
}

0

我可以这样做:

void setup() {
  Serial.begin(9600);
}

void loop() {
  String message = "";
  while (Serial.available())
    message.concat((char) Serial.read());
  if (message != "")
    Serial.println(message);
}

0

这个方法对我总是有效的 :)

String _SerialRead = "";
    
void setup() {
  Serial.begin(9600);
}
    
void loop() {
  while (Serial.available() > 0)        //Only run when there is data available
 {
    _SerialRead += char(Serial.read()); //Here every received char will be
                                        //added to _SerialRead
    if (_SerialRead.indexOf("S") > 0)   //Checks for the letter S
    {
      _SerialRead = "";                 //Do something then clear the string
    }
  }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接