Skip to content
Merged
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
51 changes: 42 additions & 9 deletions examples/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,34 @@ use std::{
net::{IpAddr, SocketAddr},
path::PathBuf,
str::FromStr,
time::Duration,
};
use tokio::{net::TcpStream, time::timeout};
use tokio_serial::{SerialPort, SerialPortBuilderExt};
use tracing_subscriber;
use udp_stream::UdpStream;

#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
pub struct Args {
#[arg(long, group = "source", conflicts_with_all = ["udp_address"])]
#[arg(long, group = "source", conflicts_with_all = ["udp_address", "tcp_address"])]
pub serial_port: Option<PathBuf>,
#[arg(long, default_value_t = 115200)]
pub serial_baud_rate: u32,
#[arg(long, group = "source", conflicts_with_all = ["serial_port"])]
#[arg(long, group = "source", conflicts_with_all = ["serial_port", "tcp_address"])]
pub udp_address: Option<IpAddr>,
#[arg(long, default_value_t = 8080)]
pub udp_port: u32,
#[arg(long, group = "source", conflicts_with_all = ["serial_port", "udp_address"])]
pub tcp_address: Option<IpAddr>,
#[arg(long, default_value_t = 8080)]
pub tcp_port: u32,
}

pub enum Port {
Serial(tokio_serial::SerialStream),
Udp(udp_stream::UdpStream),
Tcp(TcpStream),
}

pub fn configure_tracing() {
Expand All @@ -37,8 +44,8 @@ pub fn configure_tracing() {
pub async fn create_port() -> Port {
let args = Args::parse();

match (args.serial_port, args.udp_address) {
(Some(serial_port), None) => {
match (args.serial_port, args.udp_address, args.tcp_address) {
(Some(serial_port), None, None) => {
println!("Using serial port: {:?}", serial_port);
let port = tokio_serial::new(serial_port.to_string_lossy(), args.serial_baud_rate)
.open_native_async()
Expand All @@ -50,7 +57,7 @@ pub async fn create_port() -> Port {
port.clear(tokio_serial::ClearBuffer::All).unwrap();
Port::Serial(port)
}
(None, Some(udp_address)) => {
(None, Some(udp_address), None) => {
println!("Using UDP address: {}", udp_address);
let socket_addr = SocketAddr::from_str(&format!("{}:{}", udp_address, args.udp_port))
.map_err(|e| {
Expand All @@ -67,12 +74,38 @@ pub async fn create_port() -> Port {
.unwrap();
Port::Udp(port)
}
(None, None) => {
eprintln!("Error: either serial_port_name or udp_address must be provided");
(None, None, Some(tcp_address)) => {
println!("Using TCP address: {}", tcp_address);
let socket_addr = SocketAddr::from_str(&format!("{}:{}", tcp_address, args.tcp_port))
.map_err(|e| {
eprintln!("Error parsing TCP address: {}", e);
e
})
.unwrap();

match timeout(Duration::from_secs(10), TcpStream::connect(socket_addr)).await {
Ok(connection_result) => match connection_result {
Ok(port) => {
println!("Successfully connected to TCP server");
Port::Tcp(port)
}
Err(e) => {
eprintln!("Error connecting to TCP socket: {}", e);
std::process::exit(1);
}
},
Err(_) => {
eprintln!("TCP connection timed out after 10 seconds");
std::process::exit(1);
}
}
}
(None, None, None) => {
eprintln!("Error: either serial_port, udp_address, or tcp_address must be provided");
std::process::exit(1);
}
(Some(_), Some(_)) => {
eprintln!("Error: serial_port_name and udp_address are mutually exclusive");
_ => {
eprintln!("Error: serial_port, udp_address, and tcp_address are mutually exclusive");
std::process::exit(1);
}
}
Expand Down
25 changes: 25 additions & 0 deletions examples/omniscan450.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
mod common;
use common::{configure_tracing, create_port, Port};

use bluerobotics_ping::{device::PingDevice, error::PingError, omniscan450::Device as OmniScan450};

#[tokio::main]
async fn main() -> Result<(), PingError> {
configure_tracing();

println!("Parsing user provided values and creating port...");
let port = create_port().await;

println!("Creating your Omniscan device");
let omniscan450 = match port {
Port::Serial(port) => OmniScan450::new(port),
Port::Udp(port) => OmniScan450::new(port),
Port::Tcp(port) => OmniScan450::new(port),
};

let device_information = omniscan450.device_information().await?;

println!("Device information: {device_information:?}");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible for us to have more messages ?


Ok(())
}
1 change: 1 addition & 0 deletions examples/ping_1d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ async fn main() -> Result<(), PingError> {
let ping1d = match port {
Port::Serial(port) => Ping1D::new(port),
Port::Udp(port) => Ping1D::new(port),
Port::Tcp(port) => Ping1D::new(port),
};

// Creating a subscription channel which will receive 30 Profile measurements, we'll check this after the next methods!
Expand Down
1 change: 1 addition & 0 deletions examples/ping_360.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ async fn main() -> Result<(), PingError> {
let ping360 = match port {
Port::Serial(port) => Ping360::new(port),
Port::Udp(port) => Ping360::new(port),
Port::Tcp(port) => Ping360::new(port),
};

println!("Reading transducer data:");
Expand Down
1 change: 1 addition & 0 deletions examples/ping_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ async fn main() -> Result<(), PingError> {
let ping = match port {
Port::Serial(port) => Device::new(port),
Port::Udp(port) => Device::new(port),
Port::Tcp(port) => Device::new(port),
};

// Creating a subscription channel which will receive 2 Protocol Messages, we'll print the device id!
Expand Down