2023-04-26 10:51:23 +02:00
|
|
|
use std::path::Path;
|
|
|
|
use std::time::Duration;
|
2023-04-27 10:48:38 +02:00
|
|
|
use std::{env, io, process, thread};
|
2023-04-26 10:51:23 +02:00
|
|
|
|
|
|
|
use rand::random;
|
|
|
|
use serial::SerialPort;
|
|
|
|
|
|
|
|
pub fn main() {
|
|
|
|
if let Some(port_name) = env::args().nth(1) {
|
2023-04-27 10:48:38 +02:00
|
|
|
let idles = env::args().position(|x| x == "--idles").is_some();
|
2023-04-26 10:51:23 +02:00
|
|
|
|
|
|
|
println!("Saturating port {:?} with 115200 8N1", port_name);
|
2023-04-27 10:48:38 +02:00
|
|
|
println!("Idles: {}", idles);
|
|
|
|
println!("Process ID: {}", process::id());
|
2023-04-26 10:51:23 +02:00
|
|
|
let mut port = serial::open(&port_name).unwrap();
|
2023-04-27 10:48:38 +02:00
|
|
|
if saturate(&mut port, idles).is_err() {
|
2023-04-26 10:51:23 +02:00
|
|
|
eprintln!("Unable to saturate port");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let path = env::args().next().unwrap();
|
|
|
|
let basepath = Path::new(&path).with_extension("");
|
|
|
|
let basename = basepath.file_name().unwrap();
|
|
|
|
eprintln!("USAGE: {} <port-name>", basename.to_string_lossy());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-27 10:48:38 +02:00
|
|
|
fn saturate<T: SerialPort>(port: &mut T, idles: bool) -> io::Result<()> {
|
2023-04-26 10:51:23 +02:00
|
|
|
port.reconfigure(&|settings| {
|
|
|
|
settings.set_baud_rate(serial::Baud115200)?;
|
|
|
|
settings.set_char_size(serial::Bits8);
|
|
|
|
settings.set_parity(serial::ParityNone);
|
|
|
|
settings.set_stop_bits(serial::Stop1);
|
|
|
|
Ok(())
|
|
|
|
})?;
|
|
|
|
|
|
|
|
let mut written = 0;
|
|
|
|
loop {
|
|
|
|
let len = random::<usize>() % 0x1000;
|
|
|
|
let buf: Vec<u8> = (written..written + len).map(|x| x as u8).collect();
|
|
|
|
|
|
|
|
port.write_all(&buf)?;
|
|
|
|
|
2023-04-27 10:48:38 +02:00
|
|
|
if idles {
|
2023-04-26 10:51:23 +02:00
|
|
|
let micros = (random::<usize>() % 1000) as u64;
|
|
|
|
println!("Sleeping {}us", micros);
|
|
|
|
port.flush().unwrap();
|
|
|
|
thread::sleep(Duration::from_micros(micros));
|
|
|
|
}
|
|
|
|
|
|
|
|
written += len;
|
|
|
|
println!("Written: {}", written);
|
|
|
|
}
|
2023-04-27 10:48:38 +02:00
|
|
|
}
|