Arduino在从设备接收的串行文本中找不到字符串中的子字符串

时间:2019-07-06 18:28:57

标签: android arduino substring

我正在设置从Android到Arduino的发送GPS位置,以便Arduino跟随Android手机。我从Android获得了一个字符串,并且似乎正确地获得了该字符串并获得了适当的长度,但是我无法从该字符串中找到标识符(“ Lon”或“ Lat”)。

我从Android获取了一个字符串,将接收到的字节数组放入数组中,将数组转换为字符串,然后对其进行了测试以检查其是否为字符串,并检查了字符串的长度,一切似乎都是合法的,但我无法确定我的标识符是否在提取数据的字符串中。

#include <SoftwareSerial.h>
// Example 2 - Receive with an end-marker

const byte numChars = 32;
char receivedChars[numChars];   // an array to store the received data
String mlong;
String mlat;
long mlong_val;
long mlat_val;
String temp_mlat_val;
String temp_mlong_val;
String temp_str;
boolean newData = false;

SoftwareSerial BT_Serial(10, 11);

void setup() {
      Serial.begin(9600);
      Serial.println("Bluetooth receive 003");
      BT_Serial.begin(9600);
}

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

void recvWithEndMarker() {
    static byte ndx = 0;
    char endMarker = '\n';
    char rc;

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

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

void showNewData() {
    if (newData == true) {
        Serial.print("This just in ... ");
        Serial.println(receivedChars);
        String temp_str = receivedChars;      
        Serial.print("This is the string, I hope ");
        Serial.println(temp_str);
        unsigned int lastStringLength = temp_str.length();
        Serial.print("This is the length of the string, I hope ");
        Serial.println(lastStringLength);
        if (temp_str.substring(lastStringLength) == "Lat"){
          Serial.println("Lat");
          }
        else {Serial.println("After looking for Lat, not found");
        }  
        newData = false;
    }
}

void loop() {
  // Set up a String:
  String stringOne = "Content-Type: text/html";
  Serial.println(stringOne);

  // substring(index) looks for the substring from the index position to the end:
  if (stringOne.substring(19) == "html") {
    Serial.println("It's an html file");
  }
  // you can also look for a substring in the middle of a string:
  if (stringOne.substring(14, 18) == "text") {
    Serial.println("It's a text-based file");
  }

尝试用我的代码中的字符串替换其字符串

 if (newData == true) {
    Serial.print("This just in ... ");
    temp_str = "Lat12.34567";
    Serial.print("This is the string, I hope ");
    Serial.println(temp_str);
    unsigned int lastStringLength = temp_str.length();
    Serial.print("This is the length of the string, I hope ");
    Serial.println(lastStringLength);
    if (temp_str.substring(lastStringLength) == "Lat"){
      Serial.println("Lat");
      }
    else {Serial.println("After looking for Lat, not found");
    }  
    newData = false;

不仅适用于字符串,而且不适用于Android

如果标识符“ Lat”位于字符串中,则仅打印“ Lat”,否则不会打印

0 个答案:

没有答案