Arduino UNO单片机解析农芯科技移动站GPRMC格式报文的经度、纬度和前进速度的程序,5s输出一次
时间: 2023-06-27 17:06:09 浏览: 50
以下是基于 Arduino UNO 的程序,可以解析农芯科技移动站GPRMC格式报文的经度、纬度和前进速度,并且每5秒输出一次:
```C++
#include <SoftwareSerial.h>
SoftwareSerial gpsSerial(2, 3); // RX, TX
void setup() {
Serial.begin(9600);
gpsSerial.begin(9600);
}
void loop() {
while (gpsSerial.available()) {
char c = gpsSerial.read();
if (c == '$') { // GPRMC格式报文以$开头
String sentence = gpsSerial.readStringUntil('\r');
if (sentence.startsWith("GPRMC")) {
int comma1 = sentence.indexOf(',');
int comma2 = sentence.indexOf(',', comma1 + 1);
int comma3 = sentence.indexOf(',', comma2 + 1);
int comma4 = sentence.indexOf(',', comma3 + 1);
int comma5 = sentence.indexOf(',', comma4 + 1);
int comma6 = sentence.indexOf(',', comma5 + 1);
int comma7 = sentence.indexOf(',', comma6 + 1);
if (comma7 > 0) { // 如果有定位数据
String time = sentence.substring(comma1 + 1, comma2);
float latitude = sentence.substring(comma2 + 1, comma3).toFloat() / 100;
String latitudeDir = sentence.substring(comma3 + 1, comma4);
float longitude = sentence.substring(comma4 + 1, comma5).toFloat() / 100;
String longitudeDir = sentence.substring(comma5 + 1, comma6);
float speed = sentence.substring(comma6 + 1, comma7).toFloat() * 1.852; // 千米/小时
Serial.print("Time: ");
Serial.print(time);
Serial.print(", Latitude: ");
Serial.print(latitude, 6);
Serial.print(latitudeDir);
Serial.print(", Longitude: ");
Serial.print(longitude, 6);
Serial.print(longitudeDir);
Serial.print(", Speed: ");
Serial.print(speed, 2);
Serial.println(" km/h");
}
}
}
}
delay(5000); // 每5秒输出一次
}
```
需要注意的是,本程序使用了软串口,将GPS模块的TX和RX分别连接到Arduino的2号和3号引脚。如果您使用的是硬件串口,可以将程序中的`SoftwareSerial`部分替换为`Serial1`或其他对应的硬件串口名称。此外,本程序中将GPRMC格式报文解析为了时间、纬度、经度和速度四个数据,如果您需要解析更多的GPS数据,可以根据GPRMC格式报文的定义自行添加解析代码。