Skip to content

Commit

Permalink
Add file input option
Browse files Browse the repository at this point in the history
Seems to be a bug on MacOS where data is truncated so an arbitrary delay is used

serialport/serialport-rs#117
  • Loading branch information
jonas-lindmark committed Aug 31, 2024
1 parent bb2e271 commit e69dd8b
Showing 1 changed file with 37 additions and 21 deletions.
58 changes: 37 additions & 21 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::{thread, time};
use std::fs::File;
use std::io::{self, Read, Write};
use std::io::ErrorKind::NotFound;
use std::{thread, time};
use std::path::PathBuf;
use std::time::Duration;

use clap::Parser;
Expand All @@ -17,6 +19,10 @@ struct Cli {
#[arg(short, long, default_value_t = 115_200)]
baud: u32,

/// read form this file
#[clap(short, long)]
input_file: Option<PathBuf>,

/// Wait up to 10 seconds for the serial port to appear
#[arg(short, long, default_value_t = false)]
wait: bool,
Expand All @@ -32,27 +38,37 @@ fn main() {

match port {
Ok(mut port) => {
// Clone the port
let mut clone = port.try_clone().expect("Failed to clone");
// Send out 4 bytes every second
thread::spawn(move || loop {
for i in io::stdin().bytes() {
clone
.write_all(&[i.unwrap()])
.expect("Failed to write to serial port");
}
});
if let Some(path) = cli.input_file {
let mut file = File::open(&path).unwrap();
let mut buffer = Vec::new();
file.read_to_end(&mut buffer).unwrap();
port.write_all(&buffer).unwrap();
thread::sleep(Duration::from_millis(500));
eprintln!("Wrote {} to {} at {} baud", path.as_os_str().to_str().unwrap(), &cli.port, &cli.baud);
} else {

let mut serial_buf: Vec<u8> = vec![0; 1000];
println!("Receiving data on {} at {} baud:", &cli.port, &cli.baud);
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);
std::process::exit(1);
},
// Clone the port
let mut clone = port.try_clone().expect("Failed to clone");
// Send out 4 bytes every second
thread::spawn(move || loop {
for i in io::stdin().bytes() {
clone
.write_all(&[i.unwrap()])
.expect("Failed to write to serial port");
}
});

let mut serial_buf: Vec<u8> = vec![0; 1000];
eprintln!("Opened {} at {} baud", &cli.port, &cli.baud);
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);
std::process::exit(1);
}
}
}
}
}
Expand Down

0 comments on commit e69dd8b

Please sign in to comment.