Convert serial.read() into a useable string using

2019-01-07 02:14发布

I'm using two Arduinos to sent plain text strings to each other using newsoftserial and an RF transceiver.

Each string is perhaps 20-30 characters in length. How do I convert Serial.read() into a string so I can do if x == "testing statements", etc.?

标签: arduino
15条回答
Ridiculous、
2楼-- · 2019-01-07 02:30

Unlimited string readed

  String content = "";
  char character;

  while(Serial.available()) {
      character = Serial.read();
      content.concat(character);
  }

  if (content != "") {
    Serial.println(content);
  }
查看更多
Melony?
3楼-- · 2019-01-07 02:30

Use string append operator on the serial.read(). It works better than string.concat()

char r;
string mystring = "";

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

After you are done saving the stream in a string(mystring, in this case), use SubString functions to extract what you are looking for.

查看更多
Rolldiameter
4楼-- · 2019-01-07 02:32

Credit for this goes to magma. Great answer, but here it is using c++ style strings instead of c style strings. Some users may find that easier.

String string = "";
char ch; // Where to store the character read

void setup() {
    Serial.begin(9600);
    Serial.write("Power On");
}

boolean Comp(String par) {
    while (Serial.available() > 0) // Don't read unless
                                   // there you know there is data
    {
        ch = Serial.read(); // Read a character
        string += ch; // Add it
    }

    if (par == string) {
        string = "";
        return(true);
    }
    else {
        //dont reset string
        return(false);
    }
}

void loop()
{
    if (Comp("m1 on")) {
        Serial.write("Motor 1 -> Online\n");
    }
    if (Comp("m1 off")) {
        Serial.write("Motor 1 -> Offline\n");
    }
}
查看更多
小情绪 Triste *
5楼-- · 2019-01-07 02:33

This would be way easier:

 char data [21];
 int number_of_bytes_received;

 if(Serial.available() > 0)
 {
   number_of_bytes_received = Serial.readBytesUntil (13,data,20); // read bytes (max. 20) from buffer, untill <CR> (13). store bytes in data. count the bytes recieved.
   data[number_of_bytes_received] = 0; // add a 0 terminator to the char array
 } 

 bool result = strcmp (data, "whatever");
 // strcmp returns 0; if inputs match.
 // http://en.cppreference.com/w/c/string/byte/strcmp


 if (result == 0)
 {
   Serial.println("data matches whatever");
 } 
 else 
 {
   Serial.println("data does not match whatever");
 }
查看更多
放荡不羁爱自由
6楼-- · 2019-01-07 02:37

You can use Serial.readString() and Serial.readStringUntil() to parse strings from Serial on the Arduino.

You can also use Serial.parseInt() to read integer values from serial.

int x;
String str;

void loop() 
{
    if(Serial.available() > 0)
    {
        str = Serial.readStringUntil('\n');
        x = Serial.parseInt();
    }
}

The value to send over serial would be my string\n5 and the result would be str = "my string" and x = 5

查看更多
太酷不给撩
7楼-- · 2019-01-07 02:38

From Help with Serial.Read() getting string:

char inData[20]; // Allocate some space for the string
char inChar=-1; // Where to store the character read
byte index = 0; // Index into array; where to store the character

void setup() {
    Serial.begin(9600);
    Serial.write("Power On");
}

char Comp(char* This) {
    while (Serial.available() > 0) // Don't read unless
                                   // there you know there is data
    {
        if(index < 19) // One less than the size of the array
        {
            inChar = Serial.read(); // Read a character
            inData[index] = inChar; // Store it
            index++; // Increment where to write next
            inData[index] = '\0'; // Null terminate the string
        }
    }

    if (strcmp(inData,This)  == 0) {
        for (int i=0;i<19;i++) {
            inData[i]=0;
        }
        index=0;
        return(0);
    }
    else {
        return(1);
    }
}

void loop()
{
    if (Comp("m1 on")==0) {
        Serial.write("Motor 1 -> Online\n");
    }
    if (Comp("m1 off")==0) {
        Serial.write("Motor 1 -> Offline\n");
    }
}
查看更多
登录 后发表回答