Skip to content

Commit

Permalink
Add a prompt
Browse files Browse the repository at this point in the history
Signed-off-by: Bin.H <H-H-bin@163.com>
  • Loading branch information
Bin.H authored and Bin.H committed Oct 30, 2021
1 parent c58e460 commit 99e025d
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 24 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
rustyline = "9.0.0"
serde = "1.0.130"
serialport = "4.0.1"
[target.'cfg(windows)'.dependencies]
Expand Down
68 changes: 44 additions & 24 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ use std::io::{self, Write};
use std::time::Duration;
use wmi::{COMLibrary, WMIConnection, WMIError};

use rustyline::error::ReadlineError;
use rustyline::Editor;

#[cfg(windows)]
#[allow(non_camel_case_types)]
#[allow(non_snake_case)]
Expand Down Expand Up @@ -112,37 +115,54 @@ fn main() {
println!("{:?}", &builder);

//3. Write to port
let msg = "ATI\r\n";
let mut port = builder.open().unwrap_or_else(|e| {
eprintln!("Failed to open \"{}\". Error: {}", port_name, e);
::std::process::exit(1);
});

println!(
"Writing {} to {} at {} baud",
&msg, &port_name, &baud_rate
);
//TODO:Add auto complete with AT command history
//TODO:Colors

match port.write(&msg.as_bytes()) {
Ok(_) => {
print!("{}", &msg);
std::io::stdout().flush().unwrap();
}
Err(ref e) if e.kind() == io::ErrorKind::TimedOut => (),
Err(e) => eprintln!("{:?}", e),
// `()` can be used when no completer is required
let mut rl = Editor::<()>::new();
if rl.load_history("history.txt").is_err() {
println!("No previous history.");
}
loop {
let readline = rl.readline("❯❯ ");
match readline {
Ok(line) => {
let line = line + "\r";
rl.add_history_entry(line.as_str());

//4. Read data
let mut serial_buf: Vec<u8> = vec![0; 1000];
println!("Receiving data on {} at {} baud:", &port_name, &baud_rate);
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),
}
//3. Write to port
match port.write(&line.as_bytes()) {
Ok(_) => {}
Err(ref e) if e.kind() == io::ErrorKind::TimedOut => (),
Err(e) => eprintln!("{:?}", e),
}

//TODO:Add a loop for data read/write
//TODO:Add promot like a shell
//TODO:Add auto complete with AT command history
//TODO:Colors
//4. Read data
let mut serial_buf: Vec<u8> = vec![0; 1000];
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),
}
}
Err(ReadlineError::Interrupted) => {
println!("CTRL-C");
break;
}
Err(ReadlineError::Eof) => {
println!("CTRL-D");
break;
}
Err(err) => {
println!("Error: {:?}", err);
break;
}
}
}
rl.save_history("history.txt").unwrap();
}

0 comments on commit 99e025d

Please sign in to comment.