Skip to content

Commit e855702

Browse files
committed
Initial commit.
Added: * Working code to get serial port list.
0 parents  commit e855702

File tree

6 files changed

+369
-0
lines changed

6 files changed

+369
-0
lines changed

.github/workflows/main.yml

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
name: Release
2+
3+
on:
4+
push:
5+
tags:
6+
- '*'
7+
8+
jobs:
9+
create-release:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- uses: actions/checkout@v2
13+
- uses: taiki-e/create-gh-release-action@v1
14+
with:
15+
changelog: CHANGELOG.md
16+
env:
17+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
18+
19+
upload-assets:
20+
strategy:
21+
matrix:
22+
os:
23+
- ubuntu-latest
24+
- macos-latest
25+
- windows-latest
26+
runs-on: ${{ matrix.os }}
27+
steps:
28+
- uses: actions/checkout@v2
29+
- name: Install libudev-dev
30+
if: ${{ matrix.os == 'ubuntu-latest' }}
31+
run: sudo apt-get install -y libudev-dev
32+
- uses: taiki-e/upload-rust-binary-action@v1
33+
with:
34+
# (required)
35+
bin: zephyr-tools-monitor
36+
# (optional) On which platform to distribute the `.tar.gz` file.
37+
# [default value: unix]
38+
# [possible values: all, unix, windows, none]
39+
tar: unix
40+
# (optional) On which platform to distribute the `.zip` file.
41+
# [default value: windows]
42+
# [possible values: all, unix, windows, none]
43+
zip: windows
44+
# (optional) Archive name (non-extension portion of filename) to be uploaded.
45+
# [default value: $bin-$target]
46+
# [possible values: the following variables and any string]
47+
# variables:
48+
# - $bin - Binary name (non-extension portion of filename).
49+
# - $target - Target triple.
50+
# - $tag - Tag of this release.
51+
archive: $bin-$tag-$target
52+
env:
53+
# (required)
54+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
target

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
## [0.1.0]
2+
3+
Initial release.
4+
5+
### Added:
6+
7+
* List functionality directly to JSON for easy parsing.

Cargo.lock

Lines changed: 226 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[package]
2+
name = "zephyr-tools-monitor"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[dependencies]
9+
pico-args = "0.4.2"
10+
serde_json = "1.0.73"
11+
serialport = "4.0.1"

src/main.rs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
use serde_json;
2+
use serialport::available_ports;
3+
4+
const HELP: &str = "\
5+
App
6+
USAGE:
7+
app [OPTIONS]
8+
FLAGS:
9+
-h, --help Prints help information
10+
OPTIONS:
11+
--list, -l Lists serial ports avaiable
12+
ARGS:
13+
<INPUT>
14+
";
15+
16+
#[derive(Debug)]
17+
struct AppArgs {
18+
list: bool,
19+
}
20+
21+
fn parse_args() -> Result<AppArgs, pico_args::Error> {
22+
let mut pargs = pico_args::Arguments::from_env();
23+
24+
// Help has a higher priority and should be handled separately.
25+
if pargs.contains(["-h", "--help"]) {
26+
print!("{}", HELP);
27+
std::process::exit(0);
28+
}
29+
30+
let args = AppArgs {
31+
list: pargs.contains(["-l", "--list"]),
32+
};
33+
34+
// It's up to the caller what to do with the remaining arguments.
35+
let remaining = pargs.finish();
36+
if !remaining.is_empty() {
37+
eprintln!("Warning: unused arguments left: {:?}.", remaining);
38+
}
39+
40+
Ok(args)
41+
}
42+
43+
fn main() {
44+
let args = match parse_args() {
45+
Ok(v) => v,
46+
Err(e) => {
47+
eprintln!("Error: {}.", e);
48+
std::process::exit(1);
49+
}
50+
};
51+
52+
if args.list {
53+
match available_ports() {
54+
Ok(ports) => {
55+
// Get the portnames
56+
let ports: Vec<String> = ports.iter().map(|x| x.port_name.clone()).collect();
57+
58+
// Turn them into a json string
59+
let json = serde_json::to_string(&ports).unwrap();
60+
61+
// Print em
62+
print!("{}", json);
63+
}
64+
Err(e) => {
65+
eprintln!("{:?}", e);
66+
eprintln!("Error listing serial ports");
67+
}
68+
}
69+
}
70+
}

0 commit comments

Comments
 (0)