Skip to content

Commit 9beebde

Browse files
authored
Fix ReadSTB Timing (#38)
1 parent 8676aff commit 9beebde

File tree

5 files changed

+101
-10
lines changed

5 files changed

+101
-10
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how
2525
### Changed
2626

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

2932
## [0.18.2]
3033

instrument-repl/src/repl.rs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ use std::{
1515
fs::{self, File},
1616
io::{self, Read, Write},
1717
path::PathBuf,
18-
sync::mpsc::{channel, SendError, Sender, TryRecvError},
18+
process::exit,
19+
sync::mpsc::{channel, Sender, TryRecvError},
1920
thread::JoinHandle,
20-
time::Duration,
21+
time::{Duration, Instant},
2122
};
2223
use tracing::{debug, error, info, instrument, trace, warn};
2324

@@ -205,17 +206,23 @@ impl Repl {
205206
}
206207

207208
let mut prompt = true;
209+
let mut command_written = true;
210+
let mut last_read = Instant::now();
208211
debug!("Starting user loop");
209212
'user_loop: loop {
210213
self.inst.set_nonblocking(true)?;
211214
std::thread::sleep(Duration::from_micros(1));
212-
let mut read_buf: Vec<u8> = vec![0; 1024];
213-
let read_size = self.inst.read(&mut read_buf)?;
214-
let read_buf: Vec<u8> = read_buf[..read_size].into();
215-
prompt = self.handle_data(&read_buf, prompt, &mut prev_state, &mut state)?;
215+
if command_written || last_read.elapsed() >= Duration::from_secs(3) {
216+
let mut read_buf: Vec<u8> = vec![0; 1024];
217+
let read_size = self.inst.read(&mut read_buf)?;
218+
let read_buf: Vec<u8> = read_buf[..read_size].into();
219+
prompt = self.handle_data(&read_buf, prompt, &mut prev_state, &mut state)?;
220+
last_read = Instant::now();
221+
}
216222

217223
if prompt {
218224
prompt = false;
225+
command_written = false;
219226
Self::print_flush(&"\nTSP> ".blue())?;
220227
}
221228
match loop_in.try_recv() {
@@ -224,6 +231,7 @@ impl Repl {
224231
match msg {
225232
Request::Tsp(tsp) => {
226233
self.inst.write_all(format!("{tsp}\n").as_bytes())?;
234+
command_written = true;
227235
prev_state = None;
228236
}
229237
Request::GetError => {
@@ -268,6 +276,7 @@ impl Repl {
268276
}
269277
}
270278
prompt = false;
279+
command_written = true;
271280
}
272281
Request::TspLinkNodes { json_file } => {
273282
self.set_lang_config_path(json_file.to_string_lossy().to_string());
@@ -293,6 +302,7 @@ impl Repl {
293302
// lose runtime state, so we can't save the previous
294303
// setting, so we just hardcode it to enabled here.
295304
self.inst.write_all(b"localnode.prompts=1\n")?;
305+
command_written = true;
296306
}
297307
Request::Exit => {
298308
info!("Exiting...");
@@ -301,6 +311,7 @@ impl Repl {
301311
Request::Reset => {
302312
self.inst.as_mut().reset()?;
303313
prompt = true;
314+
command_written = true;
304315
}
305316
Request::Help { sub_cmd } => {
306317
prompt = true;
@@ -714,9 +725,9 @@ impl Repl {
714725
let mut input = String::new();
715726
let _ = std::io::stdin().read_line(&mut input)?;
716727
let req = Self::parse_user_commands(&input)?;
717-
match out.send(req.clone()) {
718-
Ok(()) => {}
719-
Err(SendError(_)) => break 'input_loop,
728+
if out.send(req.clone()).is_err() {
729+
error!("User input thread could not send to Receiver. Closing!");
730+
exit(1);
720731
}
721732
// This `if` statement seeks to fix the NOTE above about not exiting.
722733
// It feels a little awkward, but should be effective.

kic-discover-visa/src/visa.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use std::{collections::HashSet, ffi::CString, time::Duration};
22

3-
use async_std::fs::write;
43
use serde::{Deserialize, Serialize};
54
use tracing::{error, trace};
65
use tsp_toolkit_kic_lib::{

kic-visa/src/main.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,15 @@ fn get_instrument_access(inst: &mut Box<dyn Instrument>) -> anyhow::Result<()> {
576576
Ok(())
577577
}
578578

579+
fn pause_exit_on_error() {
580+
eprintln!(
581+
"\n\n{}",
582+
"An error occured. Press Enter to close this program.".yellow()
583+
);
584+
let mut buf = String::new();
585+
let _ = std::io::stdin().read_line(&mut buf);
586+
}
587+
579588
#[instrument(skip(args))]
580589
fn connect(args: &ArgMatches) -> anyhow::Result<()> {
581590
info!("Connecting to instrument");
@@ -588,26 +597,50 @@ fn connect(args: &ArgMatches) -> anyhow::Result<()> {
588597
Ok(c) => c,
589598
Err(e) => {
590599
error!("Unable to parse connection information: {e}");
600+
eprintln!(
601+
"{}",
602+
format!("\nUnable to parse connection information: {e}\n\nUnrecoverable error. Closing.").red()
603+
);
604+
pause_exit_on_error();
591605
return Err(e);
592606
}
593607
};
594608
let mut instrument: Box<dyn Instrument> = match connect_async_instrument(conn) {
595609
Ok(i) => i,
596610
Err(e) => {
597611
error!("Error connecting to async instrument: {e}");
612+
eprintln!(
613+
"{}",
614+
format!(
615+
"\nError connecting to async instrument: {e}\n\nUnrecoverable error. Closing."
616+
)
617+
.red()
618+
);
619+
pause_exit_on_error();
598620
return Err(e);
599621
}
600622
};
601623

602624
if let Err(e) = get_instrument_access(&mut instrument) {
603625
error!("Error setting up instrument: {e}");
626+
eprintln!(
627+
"{}",
628+
format!("\nError setting up instrument: {e}\n\nUnrecoverable error. Closing.").red()
629+
);
630+
pause_exit_on_error();
604631
return Err(e);
605632
}
606633

607634
let info = match instrument.info() {
608635
Ok(i) => i,
609636
Err(e) => {
610637
error!("Error getting instrument info: {e}");
638+
eprintln!(
639+
"{}",
640+
format!("\nError getting instrument info: {e}\n\nUnrecoverable error. Closing.")
641+
.red()
642+
);
643+
pause_exit_on_error();
611644
return Err(e.into());
612645
}
613646
};
@@ -619,6 +652,12 @@ fn connect(args: &ArgMatches) -> anyhow::Result<()> {
619652
info!("Starting instrument REPL");
620653
if let Err(e) = repl.start() {
621654
error!("Error in REPL: {e}");
655+
eprintln!(
656+
"{}",
657+
format!("\n{e}\n\nClosing instrument connection...").red()
658+
);
659+
drop(repl);
660+
pause_exit_on_error();
622661
}
623662

624663
Ok(())

kic/src/main.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,15 @@ fn get_instrument_access(inst: &mut Box<dyn Instrument>) -> anyhow::Result<()> {
572572
Ok(())
573573
}
574574

575+
fn pause_exit_on_error() {
576+
eprintln!(
577+
"\n\n{}",
578+
"An error occured. Press Enter to close this program.".yellow()
579+
);
580+
let mut buf = String::new();
581+
let _ = std::io::stdin().read_line(&mut buf);
582+
}
583+
575584
#[instrument(skip(args))]
576585
fn connect(args: &ArgMatches) -> anyhow::Result<()> {
577586
info!("Connecting to instrument");
@@ -584,26 +593,50 @@ fn connect(args: &ArgMatches) -> anyhow::Result<()> {
584593
Ok(c) => c,
585594
Err(e) => {
586595
error!("Unable to parse connection information: {e}");
596+
eprintln!(
597+
"{}",
598+
format!("\nUnable to parse connection information: {e}\n\nUnrecoverable error. Closing.").red()
599+
);
600+
pause_exit_on_error();
587601
return Err(e);
588602
}
589603
};
590604
let mut instrument: Box<dyn Instrument> = match connect_async_instrument(conn) {
591605
Ok(i) => i,
592606
Err(e) => {
593607
error!("Error connecting to async instrument: {e}");
608+
eprintln!(
609+
"{}",
610+
format!(
611+
"\nError connecting to async instrument: {e}\n\nUnrecoverable error. Closing."
612+
)
613+
.red()
614+
);
615+
pause_exit_on_error();
594616
return Err(e);
595617
}
596618
};
597619

598620
if let Err(e) = get_instrument_access(&mut instrument) {
599621
error!("Error setting up instrument: {e}");
622+
eprintln!(
623+
"{}",
624+
format!("\nError setting up instrument: {e}\n\nUnrecoverable error. Closing.").red()
625+
);
626+
pause_exit_on_error();
600627
return Err(e);
601628
}
602629

603630
let info = match instrument.info() {
604631
Ok(i) => i,
605632
Err(e) => {
606633
error!("Error getting instrument info: {e}");
634+
eprintln!(
635+
"{}",
636+
format!("\nError getting instrument info: {e}\n\nUnrecoverable error. Closing.")
637+
.red()
638+
);
639+
pause_exit_on_error();
607640
return Err(e.into());
608641
}
609642
};
@@ -615,6 +648,12 @@ fn connect(args: &ArgMatches) -> anyhow::Result<()> {
615648
info!("Starting instrument REPL");
616649
if let Err(e) = repl.start() {
617650
error!("Error in REPL: {e}");
651+
eprintln!(
652+
"{}",
653+
format!("\n{e}\n\nClosing instrument connection...").red()
654+
);
655+
drop(repl);
656+
pause_exit_on_error();
618657
}
619658

620659
Ok(())

0 commit comments

Comments
 (0)