1

我对 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;
        }
      }
    }
  }
}
4

1 回答 1

2

By Default:

Serial.read() returns an int if you must process the data this way, try casting it to char with:

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

Another way to do this:

Would be to seek your beginning string (discarding un-needed data) using Serial.find() then reading data until you met your end character ",K," with Serial.readBytesUntil()

Something like this would work quite well:

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;
于 2012-08-01T04:16:48.650 回答