从Arduino的输入引脚读取串行数据

时间:2015-03-17 12:14:09

标签: input arduino xbee

我有来自Xbee的Dout的GPS串行信号。我用示波器检查了它并且信号存在。 我的问题:

如何用arduino读取串行信号(我想我应该把它送到输入引脚)?

然后将其打印到串行监视器。 感谢

1 个答案:

答案 0 :(得分:0)

我找到了读取发送到RX的GPS信号的解决方案。 它在Arduino下 示例 - >通信 - >的serialEvent

String inputString = "";         // a string to hold incoming data
boolean stringComplete = false;  // whether the string is complete

void setup() {
  // initialize serial:
  Serial.begin(9600);
  // reserve 200 bytes for the inputString:
  inputString.reserve(200);
}

void loop() {
  // print the string when a newline arrives:
  if (stringComplete) {
    Serial.println(inputString);
    // clear the string:
    inputString = "";
    stringComplete = false;
  }
}

/*
  SerialEvent occurs whenever a new data comes in the
 hardware serial RX.  This routine is run between each
 time loop() runs, so using delay inside loop can delay
 response.  Multiple bytes of data may be available.
 */
void serialEvent() {
  while (Serial.available()) {
    // get the new byte:
    char inChar = (char)Serial.read();
    // add it to the inputString:
    inputString += inChar;
    // if the incoming character is a newline, set a flag
    // so the main loop can do something about it:
    if (inChar == '\n') {
      stringComplete = true;
    }
  }
}

现在将具有串行信号的线连接到RX(引脚0)并运行串行监视器,您将看到如下内容:

$GPGLL,5652.36437,N,08901.52702,W,225356.00,A,D*73
$GPGSV,4,1,15,07,42,306,33,08,16,110,33,09,53,253,37,10,05,281,23*72
$GPRMC,225357.00,A,5652.36445,N,08901.52698,W,0.145,,180315,,,D*62