Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ReadSTB Timing #38

Merged
merged 4 commits into from
Oct 11, 2024
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how
### Changed

- .script , .upgrade and .exit commands descriptions updated
- When using VISA, only call ReadSTB after writing commands to the instrument
(or every 3 seconds for a heartbeat)
- Pause when an error occurs so users can see the errors

## [0.18.2]

Expand Down
29 changes: 20 additions & 9 deletions instrument-repl/src/repl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ use std::{
fs::{self, File},
io::{self, Read, Write},
path::PathBuf,
sync::mpsc::{channel, SendError, Sender, TryRecvError},
process::exit,
sync::mpsc::{channel, Sender, TryRecvError},
thread::JoinHandle,
time::Duration,
time::{Duration, Instant},
};
use tracing::{debug, error, info, instrument, trace, warn};

Expand Down Expand Up @@ -205,17 +206,23 @@ impl Repl {
}

let mut prompt = true;
let mut command_written = true;
let mut last_read = Instant::now();
debug!("Starting user loop");
'user_loop: loop {
self.inst.set_nonblocking(true)?;
std::thread::sleep(Duration::from_micros(1));
let mut read_buf: Vec<u8> = vec![0; 1024];
let read_size = self.inst.read(&mut read_buf)?;
let read_buf: Vec<u8> = read_buf[..read_size].into();
prompt = self.handle_data(&read_buf, prompt, &mut prev_state, &mut state)?;
if command_written || last_read.elapsed() >= Duration::from_secs(3) {
let mut read_buf: Vec<u8> = vec![0; 1024];
let read_size = self.inst.read(&mut read_buf)?;
let read_buf: Vec<u8> = read_buf[..read_size].into();
prompt = self.handle_data(&read_buf, prompt, &mut prev_state, &mut state)?;
last_read = Instant::now();
}

if prompt {
prompt = false;
command_written = false;
Self::print_flush(&"\nTSP> ".blue())?;
}
match loop_in.try_recv() {
Expand All @@ -224,6 +231,7 @@ impl Repl {
match msg {
Request::Tsp(tsp) => {
self.inst.write_all(format!("{tsp}\n").as_bytes())?;
command_written = true;
prev_state = None;
}
Request::GetError => {
Expand Down Expand Up @@ -268,6 +276,7 @@ impl Repl {
}
}
prompt = false;
command_written = true;
}
Request::TspLinkNodes { json_file } => {
self.set_lang_config_path(json_file.to_string_lossy().to_string());
Expand All @@ -293,6 +302,7 @@ impl Repl {
// lose runtime state, so we can't save the previous
// setting, so we just hardcode it to enabled here.
self.inst.write_all(b"localnode.prompts=1\n")?;
command_written = true;
}
Request::Exit => {
info!("Exiting...");
Expand All @@ -301,6 +311,7 @@ impl Repl {
Request::Reset => {
self.inst.as_mut().reset()?;
prompt = true;
command_written = true;
}
Request::Help { sub_cmd } => {
prompt = true;
Expand Down Expand Up @@ -714,9 +725,9 @@ impl Repl {
let mut input = String::new();
let _ = std::io::stdin().read_line(&mut input)?;
let req = Self::parse_user_commands(&input)?;
match out.send(req.clone()) {
Ok(()) => {}
Err(SendError(_)) => break 'input_loop,
if out.send(req.clone()).is_err() {
error!("User input thread could not send to Receiver. Closing!");
exit(1);
}
// This `if` statement seeks to fix the NOTE above about not exiting.
// It feels a little awkward, but should be effective.
Expand Down
1 change: 0 additions & 1 deletion kic-discover-visa/src/visa.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::{collections::HashSet, ffi::CString, time::Duration};

use async_std::fs::write;
use serde::{Deserialize, Serialize};
use tracing::{error, trace};
use tsp_toolkit_kic_lib::{
Expand Down
39 changes: 39 additions & 0 deletions kic-visa/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,15 @@ fn get_instrument_access(inst: &mut Box<dyn Instrument>) -> anyhow::Result<()> {
Ok(())
}

fn pause_exit_on_error() {
eprintln!(
"\n\n{}",
"An error occured. Press Enter to close this program.".yellow()
);
let mut buf = String::new();
let _ = std::io::stdin().read_line(&mut buf);
}

#[instrument(skip(args))]
fn connect(args: &ArgMatches) -> anyhow::Result<()> {
info!("Connecting to instrument");
Expand All @@ -588,26 +597,50 @@ fn connect(args: &ArgMatches) -> anyhow::Result<()> {
Ok(c) => c,
Err(e) => {
error!("Unable to parse connection information: {e}");
eprintln!(
"{}",
format!("\nUnable to parse connection information: {e}\n\nUnrecoverable error. Closing.").red()
);
pause_exit_on_error();
return Err(e);
}
};
let mut instrument: Box<dyn Instrument> = match connect_async_instrument(conn) {
Ok(i) => i,
Err(e) => {
error!("Error connecting to async instrument: {e}");
eprintln!(
"{}",
format!(
"\nError connecting to async instrument: {e}\n\nUnrecoverable error. Closing."
)
.red()
);
pause_exit_on_error();
return Err(e);
}
};

if let Err(e) = get_instrument_access(&mut instrument) {
error!("Error setting up instrument: {e}");
eprintln!(
"{}",
format!("\nError setting up instrument: {e}\n\nUnrecoverable error. Closing.").red()
);
pause_exit_on_error();
return Err(e);
}

let info = match instrument.info() {
Ok(i) => i,
Err(e) => {
error!("Error getting instrument info: {e}");
eprintln!(
"{}",
format!("\nError getting instrument info: {e}\n\nUnrecoverable error. Closing.")
.red()
);
pause_exit_on_error();
return Err(e.into());
}
};
Expand All @@ -619,6 +652,12 @@ fn connect(args: &ArgMatches) -> anyhow::Result<()> {
info!("Starting instrument REPL");
if let Err(e) = repl.start() {
error!("Error in REPL: {e}");
eprintln!(
"{}",
format!("\n{e}\n\nClosing instrument connection...").red()
);
drop(repl);
pause_exit_on_error();
}

Ok(())
Expand Down
39 changes: 39 additions & 0 deletions kic/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,15 @@ fn get_instrument_access(inst: &mut Box<dyn Instrument>) -> anyhow::Result<()> {
Ok(())
}

fn pause_exit_on_error() {
eprintln!(
"\n\n{}",
"An error occured. Press Enter to close this program.".yellow()
);
let mut buf = String::new();
let _ = std::io::stdin().read_line(&mut buf);
}

#[instrument(skip(args))]
fn connect(args: &ArgMatches) -> anyhow::Result<()> {
info!("Connecting to instrument");
Expand All @@ -584,26 +593,50 @@ fn connect(args: &ArgMatches) -> anyhow::Result<()> {
Ok(c) => c,
Err(e) => {
error!("Unable to parse connection information: {e}");
eprintln!(
"{}",
format!("\nUnable to parse connection information: {e}\n\nUnrecoverable error. Closing.").red()
);
pause_exit_on_error();
return Err(e);
}
};
let mut instrument: Box<dyn Instrument> = match connect_async_instrument(conn) {
Ok(i) => i,
Err(e) => {
error!("Error connecting to async instrument: {e}");
eprintln!(
"{}",
format!(
"\nError connecting to async instrument: {e}\n\nUnrecoverable error. Closing."
)
.red()
);
pause_exit_on_error();
return Err(e);
}
};

if let Err(e) = get_instrument_access(&mut instrument) {
error!("Error setting up instrument: {e}");
eprintln!(
"{}",
format!("\nError setting up instrument: {e}\n\nUnrecoverable error. Closing.").red()
);
pause_exit_on_error();
return Err(e);
}

let info = match instrument.info() {
Ok(i) => i,
Err(e) => {
error!("Error getting instrument info: {e}");
eprintln!(
"{}",
format!("\nError getting instrument info: {e}\n\nUnrecoverable error. Closing.")
.red()
);
pause_exit_on_error();
return Err(e.into());
}
};
Expand All @@ -615,6 +648,12 @@ fn connect(args: &ArgMatches) -> anyhow::Result<()> {
info!("Starting instrument REPL");
if let Err(e) = repl.start() {
error!("Error in REPL: {e}");
eprintln!(
"{}",
format!("\n{e}\n\nClosing instrument connection...").red()
);
drop(repl);
pause_exit_on_error();
}

Ok(())
Expand Down
Loading