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

45 lines
1.8 KiB
Rust

// Basic example demonstrating how to use the aniker-gps library
use aniker_gps::{parse_gga, GpsError, Position};
fn main() {
// Example NMEA GGA sentence
let mut gga_example =
"$GPGGA,210230,3855.4487,N,09446.0071,W,1,07,1.1,370.5,M,-29.5,M,,*7A".to_string();
// Parse the GGA sentence and handle the result
match parse_gga(&mut gga_example) {
Ok(position) => {
println!("Successfully parsed GGA sentence:");
println!("Latitude: {}", position.lat);
println!("Longitude: {}", position.long);
println!("Altitude: {}", position.alt);
}
Err(e) => {
println!("Error parsing GGA sentence:");
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),
}
}
}
// Example of handling an invalid sentence
let mut invalid_example =
"$GPRMC,210230,A,3855.4487,N,09446.0071,W,0.0,076.2,130495,003.8,E*69".to_string();
match parse_gga(&mut invalid_example) {
Ok(_) => println!("Unexpected: Successfully parsed invalid sentence"),
Err(e) => {
println!("Expected error:");
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),
}
}
}
}