Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expand basic_cli eample #20

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ublox for Rust
uBlox for Rust
==============

[![Rust](https://github.com/ublox-rs/ublox/actions/workflows/rust.yml/badge.svg)](https://github.com/ublox-rs/ublox/actions/workflows/rust.yml)
Expand All @@ -13,7 +13,7 @@ ublox for Rust
[mrvs-url]: https://www.whatrustisit.com
[mrvs-badge]: https://img.shields.io/badge/minimum%20rustc-1.65-blue?logo=rust

This project aims to build a pure-rust I/O library for ublox GPS devices, specifically using the UBX protocol.
This project aims to build a pure-rust I/O library for uBlox GPS devices, specifically using the UBX protocol.

Examples of usage of the library can be found in the [examples/](./examples) directory. A basic CLI for interacting with an uBlox device can be found in [examples/basic_cli](./examples/basic_cli/) directory. See the specific [examples/README](./examples/README.md).

Expand Down
1 change: 1 addition & 0 deletions examples/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[workspace]
exclude = ["target"]
members = ["*"]
resolver = "2"
6 changes: 3 additions & 3 deletions examples/basic_cli/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
authors = ["Lane Kolbly <[email protected]>"]
edition = "2018"
name = "ublox_cli"
authors = ["Lane Kolbly <[email protected]>", "Andrei Gherghescu <[email protected]>"]
edition = "2021"
name = "example-basi-cli"
publish = false
rust-version = "1.70"
version = "0.1.0"
Expand Down
189 changes: 178 additions & 11 deletions examples/basic_cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ fn main() {
.short('s')
.long("baud")
.required(false)
.default_value("9600")
.value_parser(value_parser!(u32))
.help("Baud rate of the port"),
)
Expand All @@ -43,9 +44,78 @@ fn main() {
.long("data-bits")
.help("Number of data bits to use")
.required(false)
.value_parser(["5", "6", "7", "8"])
.value_parser(["7", "8"])
.default_value("8"),
)
.arg(
Arg::new("parity")
.long("parity")
.help("Parity")
.required(false)
.value_parser(["even", "odd"]),
)
.arg(
Arg::new("in-ublox")
.long("in-ublox")
.default_value("true")
.action(clap::ArgAction::SetTrue)
.help("Enable receiving UBX proprietary protocol on the serial line."),
)
.arg(
Arg::new("in-nmea")
.long("in-nmea")
.default_value("false")
.action(clap::ArgAction::SetTrue)
.help("Enable receiving NMEA protocol on the serial line."),
)
.arg(
Arg::new("in-rtcm")
.long("in-rtcm")
.default_value("false")
.action(clap::ArgAction::SetTrue)
.help("Enable receiving RTCM protocol on the serial line."),
)
.arg(
Arg::new("in-rtcm3")
.long("in-rtcm3")
.default_value("false")
.action(clap::ArgAction::SetTrue)
.help(
"Enable receiving RTCM3 protocol on the serial line.
Not supported on uBlox protocol versions below 20.",
),
)
.arg(
Arg::new("out-ublox")
.long("out-ublox")
.action(clap::ArgAction::SetTrue)
.help("Enable sending UBX proprietary protocol on the serial line."),
)
.arg(
Arg::new("out-nmea")
.long("out-nmea")
.action(clap::ArgAction::SetTrue)
.help("Enable sending NMEA protocol on the serial line."),
)
.arg(
Arg::new("out-rtcm3")
.long("out-rtcm3")
.action(clap::ArgAction::SetTrue)
.help(
"Enable seding RTCM3 protocol on the serial line.
Not supported on uBlox protocol versions below 20.",
),
)
.arg(
Arg::new("ublox-port")
.long("cfg-port")
.required(false)
.default_value("usb")
.value_parser(value_parser!(String))
.help(
"Apply configuration to the corresponding port. Supported: usb, uart1, uart2",
),
)
.get_matches();

let port = matches
Expand All @@ -56,39 +126,108 @@ fn main() {
Some("2") => SerialStopBits::Two,
_ => SerialStopBits::One,
};

let data_bits = match matches.get_one::<String>("data-bits").map(|s| s.as_str()) {
Some("5") => SerialDataBits::Five,
Some("6") => SerialDataBits::Six,
Some("7") => SerialDataBits::Seven,
_ => SerialDataBits::Eight,
Some("8") => SerialDataBits::Eight,
_ => {
println!("Number of DataBits supported by uBlox is either 7 or 8");
std::process::exit(1);
},
};

let parity = match matches.get_one::<String>("parity").map(|s| s.as_str()) {
Some("odd") => SerialParity::Even,
Some("even") => SerialParity::Odd,
_ => SerialParity::None,
};

let builder = serialport::new(port, baud)
.stop_bits(stop_bits)
.data_bits(data_bits)
.timeout(Duration::from_millis(1))
.parity(SerialParity::None)
.timeout(Duration::from_millis(10))
.parity(parity)
.flow_control(SerialFlowControl::None);

println!("{:?}", &builder);
let port = builder.open().unwrap_or_else(|e| {
eprintln!("Failed to open \"{}\". Error: {}", port, e);
::std::process::exit(1);
});

let mut device = Device::new(port);

let inproto = match (
matches.get_flag("in-ublox"),
matches.get_flag("in-nmea"),
matches.get_flag("in-rtcm"),
matches.get_flag("in-rtcm3"),
) {
(true, false, false, false) => InProtoMask::UBLOX,
(false, true, false, false) => InProtoMask::NMEA,
(false, false, true, false) => InProtoMask::RTCM,
(false, false, false, true) => InProtoMask::RTCM3,
(true, true, false, false) => InProtoMask::union(InProtoMask::UBLOX, InProtoMask::NMEA),
(true, false, true, false) => InProtoMask::union(InProtoMask::UBLOX, InProtoMask::RTCM),
(true, false, false, true) => InProtoMask::union(InProtoMask::UBLOX, InProtoMask::RTCM3),
(false, true, true, false) => InProtoMask::union(InProtoMask::NMEA, InProtoMask::RTCM),
(false, true, false, true) => InProtoMask::union(InProtoMask::NMEA, InProtoMask::RTCM3),
(true, true, true, false) => InProtoMask::union(
InProtoMask::union(InProtoMask::UBLOX, InProtoMask::NMEA),
InProtoMask::RTCM,
),
(true, true, false, true) => InProtoMask::union(
InProtoMask::union(InProtoMask::UBLOX, InProtoMask::NMEA),
InProtoMask::RTCM3,
),
(_, _, true, true) => {
eprintln!("Cannot use RTCM and RTCM3 simultaneously. Choose one or the other");
std::process::exit(1)
},
(false, false, false, false) => InProtoMask::UBLOX,
};

let outproto = match (
matches.get_flag("out-ublox"),
matches.get_flag("out-nmea"),
matches.get_flag("out-rtcm3"),
) {
(true, false, false) => OutProtoMask::UBLOX,
(false, true, false) => OutProtoMask::NMEA,
(false, false, true) => OutProtoMask::RTCM3,
(true, true, false) => OutProtoMask::union(OutProtoMask::UBLOX, OutProtoMask::NMEA),
(true, false, true) => OutProtoMask::union(OutProtoMask::UBLOX, OutProtoMask::RTCM3),
(false, true, true) => OutProtoMask::union(OutProtoMask::NMEA, OutProtoMask::RTCM3),
(true, true, true) => OutProtoMask::union(
OutProtoMask::union(OutProtoMask::UBLOX, OutProtoMask::NMEA),
OutProtoMask::RTCM3,
),
(false, false, false) => OutProtoMask::UBLOX,
};

let (port_id, port_name) = match matches.get_one::<String>("ublox-port").map(|s| s.as_str()) {
Some(x) if x == "usb" => (UartPortId::Usb, x),
Some(x) if x == "uart1" => (UartPortId::Uart1, x),
Some(x) if x == "uart2" => (UartPortId::Uart2, x),
_ => (UartPortId::Usb, "USB"),
};

// Configure the device to talk UBX
println!("Configuring UART1 port ...");
println!("Configuring '{}' port ...", port_name.to_uppercase());
device
.write_all(
&CfgPrtUartBuilder {
portid: UartPortId::Uart1,
portid: port_id,
reserved0: 0,
tx_ready: 0,
mode: UartMode::new(DataBits::Eight, Parity::None, StopBits::One),
mode: UartMode::new(
ublox_databits(data_bits),
ublox_parity(parity),
ublox_stopbits(stop_bits),
),
baud_rate: baud,
in_proto_mask: InProtoMask::UBLOX,
out_proto_mask: OutProtoMask::union(OutProtoMask::NMEA, OutProtoMask::UBLOX),
in_proto_mask: inproto,
out_proto_mask: outproto,
flags: 0,
reserved5: 0,
}
Expand All @@ -100,6 +239,7 @@ fn main() {
.expect("Could not acknowledge UBX-CFG-PRT-UART msg");

// Enable the NavPvt packet
println!("Enable UBX-NAV-PVT message on selected ports ...");
device
.write_all(
&CfgMsgAllPortsBuilder::set_rate_for::<NavPvt>([0, 1, 0, 0, 0, 0]).into_packet_bytes(),
Expand Down Expand Up @@ -164,6 +304,33 @@ fn main() {
}
}

fn ublox_stopbits(s: SerialStopBits) -> StopBits {
// Seriaport crate doesn't support the other StopBits option of uBlox
match s {
SerialStopBits::One => StopBits::One,
SerialStopBits::Two => StopBits::Two,
}
}

fn ublox_databits(d: SerialDataBits) -> DataBits {
match d {
SerialDataBits::Seven => DataBits::Seven,
SerialDataBits::Eight => DataBits::Eight,
_ => {
println!("uBlox only supports Seven or Eight data bits");
DataBits::Eight
},
}
}

fn ublox_parity(v: SerialParity) -> Parity {
match v {
SerialParity::Even => Parity::Even,
SerialParity::Odd => Parity::Odd,
SerialParity::None => Parity::None,
}
}

struct Device {
port: Box<dyn serialport::SerialPort>,
parser: Parser<Vec<u8>>,
Expand Down
3 changes: 2 additions & 1 deletion ublox/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
//!
//! This project aims to build a pure-rust I/O library for ublox GPS devices, specifically using the UBX protocol.
//!
//! An example of using this library to talk to a device can be seen in the ublox_cli subfolder of this project.
//! An example of using this library to talk to a device can be seen in the examples/basic_cli subfolder of this project.
//! More examples are available in the examples subfolder.
//!
//! Constructing Packets
//! ====================
Expand Down
Loading