arduino uno如果字符串有一个字

时间:2016-06-23 15:42:44

标签: string arduino-uno

我对Arduino Uno很新,需要一些建议......所以我们走了。

我想用我的Arduino:
1.读取我的序列数据 - 接收为纯文本
2.在收到的数据行中查找特定单词 3.只传输/打印完整的字符串,如果它包含特定的"字"

我找到了这个草图,只有在我寻找char

时它才有用
// Example 3 - Receive with start- and end-markers

const byte numChars = 32;
char receivedChars[numChars];

boolean newData = false;

void setup() {
    Serial.begin(9600);
    Serial.println("<Arduino is ready>");
}

void loop() {
    recvWithStartEndMarkers();
    showNewData();
}

void recvWithStartEndMarkers() {
    static boolean recvInProgress = false;
    static byte ndx = 0;
    char startMarker = '<';
    char endMarker = '>';
    char rc;

    while (Serial.available() > 0 && newData == false) {
        rc = Serial.read();

        if (recvInProgress == true) {
            if (rc != endMarker) {
                receivedChars[ndx] = rc;
                ndx++;
                if (ndx >= numChars) {
                    ndx = numChars - 1;
                }
            }
            else {
                receivedChars[ndx] = '\0'; // terminate the string
                recvInProgress = false;
                ndx = 0;
                newData = true;
            }
        }

        else if (rc == startMarker) {
            recvInProgress = true;
        }
    }
}

void showNewData() {
    if (newData == true) {
        Serial.print("This just in ... ");
        Serial.println(receivedChars);
        newData = false;
    }
}

1 个答案:

答案 0 :(得分:0)

我认为最好使用String类方法。 您可以使用Serial.readString()获取数据 然后使用String方法查找特定单词。 这是一些有用的链接

  

https://www.arduino.cc/en/Serial/ReadString

     

https://www.arduino.cc/en/Reference/StringObject