Arduino替换代码

时间:2012-08-01 01:18:37

标签: gps arduino

我是Arduino和C编程的新手。

我正在制作一个GPS speedo,我正在尝试读取一些序列,从子串中存储一个值并通过串口回送它。

目前我在存储子字符串时遇到了问题。

我已经达到了能够在<>之间获取一些数据的程度。 但数据不是那样的。它是NMEA数据流,我想要的数据介于,N,,K,之间。

所以我一直在尝试将,N,替换为<,将,K,替换为>

无法让它发挥作用。我得到error: request for member 'replace' in 'c', which is of non-class type 'char'

到目前为止,这是我的代码......

int indata = 0;
int scrubdata = 0;
char inString[32];
int stringPos = 0;
boolean startRead = false; // is reading?

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

void loop() {
  String pageValue = readPage();
  Serial.print(pageValue);
}

String readPage(){
  //read the page, and capture & return everything between '<' and '>'

  stringPos = 0;
  memset( &inString, 0, 32 ); //clear inString memory

  while(true){
    if (Serial.available() > 0) {

      char c = Serial.read();
      c.replace(",N,", "<");
      c.replace(",K,", ">");

      if (c == '<' ) { //'<' is our begining character
        startRead = true; //Ready to start reading the part 
      }
      else if(startRead){

        if(c != '>'){ //'>' is our ending character
          inString[stringPos] = c;
          stringPos ++;
        }
        else{
          //got what we need here! We can disconnect now
          startRead = false;
          return inString;
        }
      }
    }
  }
}

1 个答案:

答案 0 :(得分:2)

默认值:

如果您必须以这种方式处理数据,

Serial.read()会返回int,请尝试将其转换为char

 char c = (char) Serial.read();

另一种方法:

使用Serial.find()寻找您的开始字符串(丢弃不需要的数据)然后读取数据,直到您使用Serial.readBytesUntil()结束字符“,K,

这样的事情会很有效:

char inData[64];         //adjust for your data size
Serial.setTimeout(2000); //Defaults to 1000 msecs set if necessary
Serial.find(",N,");      //Start of Data
int bRead = Serial.readBytesUntil(",K,", inData, 64);  //Read until end of data
inData[bRead] = 0x00;    //Zero terminate if using this as a string
return inData;