aniker-gps/examples/multiple_sentences.rs
2025-04-05 10:03:53 +01:00

38 lines
1.6 KiB
Rust

// Example demonstrating how to parse multiple NMEA sentences
use aniker_gps::{parse_gga, GpsError, Position};
fn main() {
// A collection of NMEA sentences
let sentences = vec![
"$GPGGA,210230,3855.4487,N,09446.0071,W,1,07,1.1,370.5,M,-29.5,M,,*7A",
"$GPGGA,210231,3855.4488,N,09446.0072,W,1,07,1.1,371.5,M,-29.5,M,,*7B",
"$GPGGA,210232,3855.4489,N,09446.0073,W,1,07,1.1,372.5,M,-29.5,M,,*7C",
"$GPRMC,210233,A,3855.4490,N,09446.0074,W,0.0,076.2,130495,003.8,E*69", // Invalid (RMC)
"$GPGGA,210234,3855.4491,N,09446.0075,W,1,07,1.1,373.5,M,-29.5,M,,*7D",
];
// Process each sentence
for (i, sentence) in sentences.iter().enumerate() {
let mut sentence = sentence.to_string();
match parse_gga(&mut sentence) {
Ok(position) => {
println!("Sentence {}: Successfully parsed", i + 1);
println!(" Latitude: {}", position.lat);
println!(" Longitude: {}", position.long);
println!(" Altitude: {}", position.alt);
}
Err(e) => {
println!("Sentence {}: Error", i + 1);
match e {
GpsError::InvalidMessageType => {
println!(" Invalid message type, expected GGA")
}
GpsError::InvalidLength => println!(" Invalid GGA sentence length"),
GpsError::ParseError(msg) => println!(" Parse error: {}", msg),
GpsError::Other(msg) => println!(" Other error: {}", msg),
}
}
}
}
}