Skip to content
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
7 changes: 4 additions & 3 deletions CHANGELOG.md
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops, sorry about that!

Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ All notable changes to the `Serial Monitor` crate will be documented in this fil
* allow to plot only every n-th point (max points is 5000, if dataset is larger, it will reduce it by showing only every
2nd point. if it is larger than 10000 only every 3rd point etc...)
* Fixed the theme preference setting not being properly restored after restarting the application (
thanks [@Rahinx](https://github.com/Rahix))
* Fixed bug where you couldn't change the color and label of columns (thanks [@Rahinx](https://github.com/Rahix))
* Fixed bug where for a single column of data the graph would never show (thanks [@Rahinx](https://github.com/Rahix))
thanks [@Rahix](https://github.com/Rahix))
* Fixed bug where you couldn't change the color and label of columns (thanks [@Rahix](https://github.com/Rahix))
* Fixed bug where for a single column of data the graph would never show (thanks [@Rahix](https://github.com/Rahix))
* Added a commandline interface for selecting the serial port and its settings.

## 0.3.4

Expand Down
21 changes: 21 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ tempfile = { version = "3.15", optional = true }
reqwest = { version = "0.12", default-features = false, features = ["blocking", "json", "rustls-tls", "http2"], optional = true }
semver = { version = "1.0.24", optional = true }
crossbeam-channel = "0.5.14"
gumdrop = "0.8.1"

[target.'cfg(not(target_os = "ios"))'.dependencies]
eframe = { version = "0.32", features = ["persistence", "wayland", "x11"] }
Expand Down
37 changes: 35 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ sudo apt-get install libxcb-render0-dev libxcb-shape0-dev libxcb-xfixes0-dev lib

### Compile from source

The source code can be run using ```cargo run``` or bundled to a platform-executable using cargo bundle.
The source code can be run using `cargo run` or bundled to a platform-executable using cargo bundle.
Currently [cargo bundle](https://github.com/burtonageo/cargo-bundle) only supports linux and macOS
bundles [see github issue](https://github.com/burtonageo/cargo-bundle/issues/77).
As a work-around we can use [cargo wix](https://github.com/volks73/cargo-wix) to create a windows installer.
Expand Down Expand Up @@ -70,6 +70,39 @@ cargo install cargo-wix
cargo wix
```

## Commandline Arguments
You can start the app with commandline arguments to automatically select a serial port and its settings:

```text
Usage: serial-monitor-rust [OPTIONS]

Positional arguments:
device Serial port device to open on startup

Optional arguments:
-b, --baudrate BAUDRATE Baudrate (default=9600)
-d, --databits DATABITS Data bits (5, 6, 7, default=8)
-f, --flow FLOW Flow conrol (hard, soft, default=none)
-s, --stopbits STOPBITS Stop bits (default=1, 2)
-p, --parity PARITY Parity (odd, even, default=none)
-F, --file FILE Load data from a file instead of a serial port
--column COLUMN-LABELS Column labels, can be specified multiple times for more columns
--color COLUMN-COLORS Column colors (hex color without #), can be specified multiple times for more columns
-h, --help
```

Example usage:

```sh
serial-monitor-rust /dev/ttyACM0 --baudrate 115200
```

You can also preconfigure the column settings. The following example configures the name and color for two columns in the incoming data:

```sh
serial-monitor-rust --column Raw --color '808080' --column Temperature --color 'ff8000' /dev/ttyACM0
```

## Features:

- [X] Plotting and printing of data simultaneously
Expand Down Expand Up @@ -110,4 +143,4 @@ Tested on:
- Windows 10 x86
- ...

One might have to delete the ```Cargo.lock``` file before compiling.
One might have to delete the `Cargo.lock` file before compiling.
13 changes: 11 additions & 2 deletions src/gui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ pub struct MyApp {
show_warning_window: WindowFeedback,
do_not_show_clear_warning: bool,
init: bool,
cli_column_colors: Vec<egui::Color32>,
#[cfg(feature = "self_update")]
new_release: Option<Release>,
}
Expand All @@ -176,6 +177,7 @@ impl MyApp {
load_names_rx: Receiver<Vec<String>>,
send_tx: Sender<String>,
gui_cmd_tx: Sender<GuiCommand>,
cli_column_colors: Vec<egui::Color32>,
) -> Self {
let mut file_dialog = FileDialog::default()
//.initial_directory(PathBuf::from("/path/to/app"))
Expand Down Expand Up @@ -256,6 +258,7 @@ impl MyApp {
init: false,
show_color_window: ColorWindow::NoShow,
file_opened: false,
cli_column_colors,
#[cfg(feature = "self_update")]
new_release: None,
settings_window_open: false,
Expand Down Expand Up @@ -325,9 +328,15 @@ impl MyApp {
}
if self.colors.len() != self.labels.len() {
self.colors = (0..max(self.labels.len(), 1))
.map(|i| COLORS[i % COLORS.len()])
.map(|i| {
self.cli_column_colors
.get(i)
.copied()
.unwrap_or(COLORS[i % COLORS.len()])
})
.collect();
self.color_vals = (0..max(self.data.plots.len(), 1)).map(|_| 0.0).collect();
self.color_vals =
(0..max(self.data.plots.len(), 1)).map(|_| 0.0).collect();
}
}

Expand Down
118 changes: 110 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ use crossbeam_channel::{select, Receiver, Sender};
use eframe::egui::{vec2, ViewportBuilder};
use eframe::{egui, icon_data};
use egui_plot::PlotPoint;
pub use gumdrop::Options;
use preferences::AppInfo;
use std::cmp::max;
use std::path::PathBuf;
use std::sync::{Arc, RwLock};
use std::thread;
use std::time::Duration;
use std::{env, thread};

mod color_picker;
mod custom_highlighter;
Expand Down Expand Up @@ -76,6 +77,7 @@ fn main_thread(
load_rx: Receiver<PathBuf>,
load_names_tx: Sender<Vec<String>>,
gui_cmd_rx: Receiver<GuiCommand>,
cli_column_labels: Vec<String>,
) {
// reads data from mutex, samples and saves if needed
let mut data = DataContainer::default();
Expand Down Expand Up @@ -110,7 +112,7 @@ fn main_thread(
data.dataset = vec![vec![]; max(split_data.len(), 1)];
if let Ok(mut gui_data) = data_lock.write() {
gui_data.plots = (0..max(split_data.len(), 1))
.map(|i| (format!("Column {i}"), vec![]))
.map(|i| (cli_column_labels.get(i).cloned().unwrap_or_else(|| format!("Column {i}")), vec![]))
.collect();
}
failed_format_counter = 0;
Expand Down Expand Up @@ -262,13 +264,114 @@ fn main_thread(
}
}

fn parse_databits(s: &str) -> Result<serialport::DataBits, String> {
let d: u8 = s
.parse()
.map_err(|_e| format!("databits not a number: {s}"))?;
Ok(serialport::DataBits::try_from(d).map_err(|_e| format!("invalid databits: {s}"))?)
}

fn parse_flow(s: &str) -> Result<serialport::FlowControl, String> {
match s {
"none" => Ok(serialport::FlowControl::None),
"soft" => Ok(serialport::FlowControl::Software),
"hard" => Ok(serialport::FlowControl::Hardware),
_ => Err(format!("invalid flow-control: {s}")),
}
}

fn parse_stopbits(s: &str) -> Result<serialport::StopBits, String> {
let d: u8 = s
.parse()
.map_err(|_e| format!("stopbits not a number: {s}"))?;
Ok(serialport::StopBits::try_from(d).map_err(|_e| format!("invalid stopbits: {s}"))?)
}

fn parse_parity(s: &str) -> Result<serialport::Parity, String> {
match s {
"none" => Ok(serialport::Parity::None),
"odd" => Ok(serialport::Parity::Odd),
"even" => Ok(serialport::Parity::Even),
_ => Err(format!("invalid parity setting: {s}")),
}
}

fn parse_color(s: &str) -> Result<egui::Color32, String> {
Ok(egui::ecolor::HexColor::from_str_without_hash(s)
.map_err(|e| format!("invalid color {s:?}: {e:?}"))?
.color())
}

#[derive(Debug, Options)]
struct CliOptions {
/// Serial port device to open on startup
#[options(free)]
device: Option<String>,

/// Baudrate (default=9600)
#[options(short = "b")]
baudrate: Option<u32>,

/// Data bits (5, 6, 7, default=8)
#[options(short = "d", parse(try_from_str = "parse_databits"))]
databits: Option<serialport::DataBits>,

/// Flow conrol (hard, soft, default=none)
#[options(short = "f", parse(try_from_str = "parse_flow"))]
flow: Option<serialport::FlowControl>,

/// Stop bits (default=1, 2)
#[options(short = "s", parse(try_from_str = "parse_stopbits"))]
stopbits: Option<serialport::StopBits>,

/// Parity (odd, even, default=none)
#[options(short = "p", parse(try_from_str = "parse_parity"))]
parity: Option<serialport::Parity>,

/// Load data from a file instead of a serial port
#[options(short = "F")]
file: Option<std::path::PathBuf>,

/// Column labels, can be specified multiple times for more columns
#[options(no_short, long = "column")]
column_labels: Vec<String>,

/// Column colors (hex color without #), can be specified multiple times for more columns
#[options(no_short, long = "color", parse(try_from_str = "parse_color"))]
column_colors: Vec<egui::Color32>,

help: bool,
}

fn main() {
egui_logger::builder().init().unwrap();

let args = CliOptions::parse_args_default_or_exit();

let gui_settings = load_gui_settings();
let saved_serial_device_configs = load_serial_settings();

let device_lock = Arc::new(RwLock::new(Device::default()));
let mut device = Device::default();
if let Some(name) = args.device {
device.name = name;
}
if let Some(baudrate) = args.baudrate {
device.baud_rate = baudrate;
}
if let Some(databits) = args.databits {
device.data_bits = databits;
}
if let Some(flow) = args.flow {
device.flow_control = flow;
}
if let Some(stopbits) = args.stopbits {
device.stop_bits = stopbits;
}
if let Some(parity) = args.parity {
device.parity = parity;
}

let device_lock = Arc::new(RwLock::new(device));
let devices_lock = Arc::new(RwLock::new(vec![gui_settings.device.clone()]));
let data_lock = Arc::new(RwLock::new(GuiOutputDataContainer::default()));
let connected_lock = Arc::new(RwLock::new(false));
Expand Down Expand Up @@ -316,14 +419,12 @@ fn main() {
load_rx,
loaded_names_tx,
gui_cmd_rx,
args.column_labels,
);
});

let args: Vec<String> = env::args().collect();
if args.len() > 1 {
load_tx
.send(PathBuf::from(&args[1]))
.expect("failed to send file");
if let Some(file) = args.file {
load_tx.send(file).expect("failed to send file");
}

let options = eframe::NativeOptions {
Expand Down Expand Up @@ -372,6 +473,7 @@ fn main() {
loaded_names_rx,
send_tx,
gui_cmd_tx,
args.column_colors,
)))
}),
) {
Expand Down