From 725917aa888b2cc3955e9771609184f7958aecfc Mon Sep 17 00:00:00 2001 From: Federico Lolli <federico.lolli@skywarder.eu> Date: Mon, 22 Jan 2024 10:24:07 +0100 Subject: [PATCH] removed bin and moved all to main --- Cargo.toml | 3 -- src/bin/duplex.rs | 86 ------------------------------- src/main.rs | 126 +++++++++++++++++++++++++++++----------------- 3 files changed, 79 insertions(+), 136 deletions(-) delete mode 100644 src/bin/duplex.rs diff --git a/Cargo.toml b/Cargo.toml index 6678dfb..03636db 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,3 @@ edition = "2021" [dependencies] clap = "4.4.11" serialport = "4.2.2" - -[[bin]] -name = "duplex" diff --git a/src/bin/duplex.rs b/src/bin/duplex.rs deleted file mode 100644 index 44fdc73..0000000 --- a/src/bin/duplex.rs +++ /dev/null @@ -1,86 +0,0 @@ -//! Duplex example -//! -//! This example tests the ability to clone a serial port. It works by creating -//! a new file descriptor, and therefore a new `SerialPort` object that's safe -//! to send to a new thread. -//! -//! This example selects the first port on the system, clones the port into a child -//! thread that writes data to the port every second. While this is running the parent -//! thread continually reads from the port. -//! -//! To test this, have a physical or virtual loopback device connected as the -//! only port in the system. - -use std::io::{Read, Write}; -use std::time::Duration; -use std::{io, thread}; - -fn main() { - // let mut s = String::new(); - // let mut f = std::fs::File::open("mavlink.csv").expect("Unable to open file"); - // f.read_to_string(&mut s).expect("Unable to read string"); - // let values = s - // .trim() - // .split(',') - // .map(|s| s.parse::<f64>().unwrap()) - // .collect::<Vec<_>>(); - - // dbg!(&values); - - // Open the first serialport available. - let port_name = "/dev/tty.usbmodem143203"; - let mut port = serialport::new(port_name, 115200) - .open() - .expect("Failed to open serial port"); - - // Clone the port - let mut clone = port.try_clone().expect("Failed to clone"); - - // Send out 4 bytes every second - thread::spawn(move || { - thread::sleep(Duration::from_millis(2000)); - let mut mavlink_buffer = [0u8; 158]; - mavlink_buffer[0..8].copy_from_slice(&1_000_000_000u64.to_le_bytes()); - for i in 0..36 { - mavlink_buffer[8 + i * 4..12 + i * 4].copy_from_slice(&999f32.to_le_bytes()); - } - mavlink_buffer[152..158].fill(100u8); - - // mavlink_buffer[158] = b'\n'; - - // let mut send_buffer = [0u8; 316]; - // for i in 0..316 { - // if i % 2 == 0 { - // send_buffer[i] = mavlink_buffer[i / 2]; - // } else { - // send_buffer[i] = b'\n'; - // } - // } - - clone - // .write_all(&send_buffer) - .write_all(&mavlink_buffer) - .expect("Failed to write to serial port"); - }); - - // Read the four bytes back from the cloned port - let mut buffer = [0u8; 1000]; - loop { - match port.read(&mut buffer) { - Ok(t) => { - io::stdout().write_all(&buffer[..t]).unwrap(); - // let mut read = 0; - // for chunk in buffer[..bytes].chunks(4) { - // read += 4; - // let mut utfbuf = [0u8; 4]; - // utfbuf[0..chunk.len()].copy_from_slice(chunk); - // let value = String::from_utf8_lossy(&utfbuf); - // print!("{}", value); - // } - } - Err(ref e) if e.kind() == io::ErrorKind::TimedOut => (), - Err(e) => eprintln!("{:?}", e), - } - thread::sleep(Duration::from_millis(100)); - } -} diff --git a/src/main.rs b/src/main.rs index 9ad3a7e..44fdc73 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,54 +1,86 @@ -use std::io::{self, Write}; -use std::time::Duration; +//! Duplex example +//! +//! This example tests the ability to clone a serial port. It works by creating +//! a new file descriptor, and therefore a new `SerialPort` object that's safe +//! to send to a new thread. +//! +//! This example selects the first port on the system, clones the port into a child +//! thread that writes data to the port every second. While this is running the parent +//! thread continually reads from the port. +//! +//! To test this, have a physical or virtual loopback device connected as the +//! only port in the system. -use clap::{Arg, Command}; +use std::io::{Read, Write}; +use std::time::Duration; +use std::{io, thread}; fn main() { - let matches = Command::new("Serialport Example - Receive Data") - .about("Reads data from a serial port and echoes it to stdout") - .disable_version_flag(true) - .arg( - Arg::new("port") - .help("The device path to a serial port") - .use_value_delimiter(false) - .required(true), - ) - .arg( - Arg::new("baud") - .help("The baud rate to connect at") - .use_value_delimiter(false) - .required(true) - .value_parser(valid_baud), - ) - .get_matches(); - - let port_name = matches.get_one::<String>("port").unwrap(); - let baud_rate = *matches.get_one::<u32>("baud").unwrap(); - - let port = serialport::new(port_name, baud_rate) - .timeout(Duration::from_millis(10)) - .open(); - - match port { - Ok(mut port) => { - let mut serial_buf: Vec<u8> = vec![0; 1000]; - println!("Receiving data on {} at {} baud:", &port_name, &baud_rate); - loop { - match port.read(serial_buf.as_mut_slice()) { - Ok(t) => io::stdout().write_all(&serial_buf[..t]).unwrap(), - Err(ref e) if e.kind() == io::ErrorKind::TimedOut => (), - Err(e) => eprintln!("{:?}", e), - } - } + // let mut s = String::new(); + // let mut f = std::fs::File::open("mavlink.csv").expect("Unable to open file"); + // f.read_to_string(&mut s).expect("Unable to read string"); + // let values = s + // .trim() + // .split(',') + // .map(|s| s.parse::<f64>().unwrap()) + // .collect::<Vec<_>>(); + + // dbg!(&values); + + // Open the first serialport available. + let port_name = "/dev/tty.usbmodem143203"; + let mut port = serialport::new(port_name, 115200) + .open() + .expect("Failed to open serial port"); + + // Clone the port + let mut clone = port.try_clone().expect("Failed to clone"); + + // Send out 4 bytes every second + thread::spawn(move || { + thread::sleep(Duration::from_millis(2000)); + let mut mavlink_buffer = [0u8; 158]; + mavlink_buffer[0..8].copy_from_slice(&1_000_000_000u64.to_le_bytes()); + for i in 0..36 { + mavlink_buffer[8 + i * 4..12 + i * 4].copy_from_slice(&999f32.to_le_bytes()); } - Err(e) => { - eprintln!("Failed to open \"{}\". Error: {}", port_name, e); - ::std::process::exit(1); + mavlink_buffer[152..158].fill(100u8); + + // mavlink_buffer[158] = b'\n'; + + // let mut send_buffer = [0u8; 316]; + // for i in 0..316 { + // if i % 2 == 0 { + // send_buffer[i] = mavlink_buffer[i / 2]; + // } else { + // send_buffer[i] = b'\n'; + // } + // } + + clone + // .write_all(&send_buffer) + .write_all(&mavlink_buffer) + .expect("Failed to write to serial port"); + }); + + // Read the four bytes back from the cloned port + let mut buffer = [0u8; 1000]; + loop { + match port.read(&mut buffer) { + Ok(t) => { + io::stdout().write_all(&buffer[..t]).unwrap(); + // let mut read = 0; + // for chunk in buffer[..bytes].chunks(4) { + // read += 4; + // let mut utfbuf = [0u8; 4]; + // utfbuf[0..chunk.len()].copy_from_slice(chunk); + // let value = String::from_utf8_lossy(&utfbuf); + // print!("{}", value); + // } + } + Err(ref e) if e.kind() == io::ErrorKind::TimedOut => (), + Err(e) => eprintln!("{:?}", e), } + thread::sleep(Duration::from_millis(100)); } } - -fn valid_baud(val: &str) -> Result<u32, String> { - val.parse::<u32>() - .map_err(|_| format!("Invalid baud rate '{}' specified", val)) -} -- GitLab