Last active
June 30, 2025 10:57
-
-
Save M0r13n/ed3fe7e36a183d52d6050612175d01ff to your computer and use it in GitHub Desktop.
Read NMEA AIS messages from file using Rust
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use nmea_parser::*; | |
use std::fs; | |
use std::time::Instant; | |
fn main() { | |
let mut parser = NmeaParser::new(); | |
let mut tot = 0; | |
let start = Instant::now(); | |
// Ignore file reading errors | |
let contents = match fs::read_to_string("messages.txt") { | |
Ok(content) => content, | |
Err(_) => { | |
eprintln!("Could not read file, exiting"); | |
return; | |
} | |
}; | |
for line in contents.lines() { | |
let sentence = line.trim(); | |
if sentence.is_empty() { | |
continue; | |
} | |
// Ignore parsing errors and continue | |
if let Ok(parsed_message) = parser.parse_sentence(sentence) { | |
tot +=1; | |
match parsed_message { | |
ParsedMessage::VesselDynamicData(vdd) => { | |
// println!("MMSI: {}", vdd.mmsi); | |
// println!("Speed: {:.1} kts", vdd.sog_knots.unwrap_or(0.0)); | |
// println!("Heading: {}°", vdd.heading_true.unwrap_or(0.0)); | |
// println!(""); | |
continue | |
}, | |
ParsedMessage::VesselStaticData(vsd) => { | |
// println!("MMSI: {}", vsd.mmsi); | |
// println!("Flag: {}", vsd.country().unwrap_or("Unknown")); | |
// println!("Name: {}", vsd.name.unwrap_or_else(|| "Unknown".to_string())); | |
// println!("Type: {}", vsd.ship_type); | |
// println!(""); | |
continue | |
} | |
_ => { | |
// Ignore other message types | |
} | |
} | |
} | |
// If parsing fails, just continue to next line (ignore error) | |
} | |
// Print summary | |
let duration = start.elapsed(); | |
println!("=== SUMMARY ==="); | |
println!("Decoded {} NMEA AIS messages in {:.2?}", tot, duration); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment