From 4eeaa3efa618abf540d6f66db5ee90bb0efff2e3 Mon Sep 17 00:00:00 2001 From: Nikhil Gupta Date: Tue, 27 Jul 2021 02:18:42 +0530 Subject: [PATCH 1/3] #58 seria-monitor --- serialmonitor/Cargo.lock | 243 +++++++++++++++++++++++++++++++++ serialmonitor/Cargo.toml | 11 ++ serialmonitor/src/main.rs | 180 ++++++++++++++++++++++++ serialmonitor/src/recieved.txt | 0 4 files changed, 434 insertions(+) create mode 100644 serialmonitor/Cargo.lock create mode 100644 serialmonitor/Cargo.toml create mode 100644 serialmonitor/src/main.rs create mode 100644 serialmonitor/src/recieved.txt diff --git a/serialmonitor/Cargo.lock b/serialmonitor/Cargo.lock new file mode 100644 index 00000000..880d2fd9 --- /dev/null +++ b/serialmonitor/Cargo.lock @@ -0,0 +1,243 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "CoreFoundation-sys" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d0e9889e6db118d49d88d84728d0e964d973a5680befb5f85f55141beea5c20b" +dependencies = [ + "libc", + "mach 0.1.2", +] + +[[package]] +name = "IOKit-sys" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "99696c398cbaf669d2368076bdb3d627fb0ce51a26899d7c61228c5c0af3bf4a" +dependencies = [ + "CoreFoundation-sys", + "libc", + "mach 0.1.2", +] + +[[package]] +name = "aho-corasick" +version = "0.7.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e37cfd5e7657ada45f742d6e99ca5788580b5c529dc78faf11ece6dc702656f" +dependencies = [ + "memchr", +] + +[[package]] +name = "autocfg" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" + +[[package]] +name = "bitflags" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" + +[[package]] +name = "cc" +version = "1.0.69" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e70cc2f62c6ce1868963827bd677764c62d07c3d9a3e1fb1177ee1a9ab199eb2" + +[[package]] +name = "cfg-if" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" + +[[package]] +name = "chrono" +version = "0.4.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "670ad68c9088c2a963aaa298cb369688cf3f9465ce5e2d4ca10e6e0098a1ce73" +dependencies = [ + "libc", + "num-integer", + "num-traits", + "time", + "winapi", +] + +[[package]] +name = "libc" +version = "0.2.98" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "320cfe77175da3a483efed4bc0adc1968ca050b098ce4f2f1c13a56626128790" + +[[package]] +name = "libudev" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ea626d3bdf40a1c5aee3bcd4f40826970cae8d80a8fec934c82a63840094dcfe" +dependencies = [ + "libc", + "libudev-sys", +] + +[[package]] +name = "libudev-sys" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c8469b4a23b962c1396b9b451dda50ef5b283e8dd309d69033475fa9b334324" +dependencies = [ + "libc", + "pkg-config", +] + +[[package]] +name = "mach" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2fd13ee2dd61cc82833ba05ade5a30bb3d63f7ced605ef827063c63078302de9" +dependencies = [ + "libc", +] + +[[package]] +name = "mach" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "86dd2487cdfea56def77b88438a2c915fb45113c5319bfe7e14306ca4cd0b0e1" +dependencies = [ + "libc", +] + +[[package]] +name = "memchr" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b16bd47d9e329435e309c58469fe0791c2d0d1ba96ec0954152a5ae2b04387dc" + +[[package]] +name = "nix" +version = "0.16.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dd0eaf8df8bab402257e0a5c17a254e4cc1f72a93588a1ddfb5d356c801aa7cb" +dependencies = [ + "bitflags", + "cc", + "cfg-if", + "libc", + "void", +] + +[[package]] +name = "num-integer" +version = "0.1.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2cc698a63b549a70bc047073d2949cce27cd1c7b0a4a862d08a8031bc2801db" +dependencies = [ + "autocfg", + "num-traits", +] + +[[package]] +name = "num-traits" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a64b1ec5cda2586e284722486d802acf1f7dbdc623e2bfc57e65ca1cd099290" +dependencies = [ + "autocfg", +] + +[[package]] +name = "pkg-config" +version = "0.3.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3831453b3449ceb48b6d9c7ad7c96d5ea673e9b470a1dc578c2ce6521230884c" + +[[package]] +name = "regex" +version = "1.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d07a8629359eb56f1e2fb1652bb04212c072a87ba68546a04065d525673ac461" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.6.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b" + +[[package]] +name = "serialmonitor" +version = "0.1.0" +dependencies = [ + "chrono", + "serialport", +] + +[[package]] +name = "serialport" +version = "4.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5d8cd7c0f22290ee2c01457009fa6fc1cae4153d5608a924e5dc423babc2c655" +dependencies = [ + "CoreFoundation-sys", + "IOKit-sys", + "bitflags", + "cfg-if", + "libudev", + "mach 0.2.3", + "nix", + "regex", + "winapi", +] + +[[package]] +name = "time" +version = "0.1.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6db9e6914ab8b1ae1c260a4ae7a49b6c5611b40328a735b21862567685e73255" +dependencies = [ + "libc", + "wasi", + "winapi", +] + +[[package]] +name = "void" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d" + +[[package]] +name = "wasi" +version = "0.10.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a143597ca7c7793eff794def352d41792a93c481eb1042423ff7ff72ba2c31f" + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" diff --git a/serialmonitor/Cargo.toml b/serialmonitor/Cargo.toml new file mode 100644 index 00000000..dd6a1597 --- /dev/null +++ b/serialmonitor/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "serialmonitor" +version = "0.1.0" +authors = ["Nikhil Gupta "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +serialport = "4.0.1" +chrono = "0.4.19" diff --git a/serialmonitor/src/main.rs b/serialmonitor/src/main.rs new file mode 100644 index 00000000..a1d0fb64 --- /dev/null +++ b/serialmonitor/src/main.rs @@ -0,0 +1,180 @@ +// RustDuino : A generic HAL implementation for Arduino Boards in Rust +// Copyright (C) 2021 Nikhil Gupta,Indian Institute of Technology Kanpur +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published +// by the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see +use serialport; +use chrono::{Datelike, Timelike,Local}; +use std::fs::File; +use std::fs::OpenOptions; +use std::io; +use std::io::ErrorKind; +use std::io::Write; +use std::time::Duration; +fn main() { + println!("Welcome to Arduino Serial Monitor\nWrite help to get list of functions available:"); + + let _help1: Vec = vec![104, 101, 108, 112, 13, 10]; + let _ser: Vec = vec![ + 83, 101, 114, 105, 97, 108, 95, 80, 111, 114, 116, 115, 95, 97, 118, 97, 105, 108, 97, 98, + 108, 101, 13, 10, + ]; + let _port1: Vec = vec![110, 101, 119, 95, 112, 111, 114, 116, 13, 10]; + let _read1: Vec = vec![114, 101, 97, 100, 13, 10]; + let _write1: Vec = vec![119, 114, 105, 116, 101, 13, 10]; + let _exit1: Vec = vec![101, 120, 105, 116, 13, 10]; + let f = OpenOptions::new().write(true).open("recieved.txt"); + let mut f = match f { + Ok(file) => file, + Err(error) => match error.kind() { + ErrorKind::NotFound => match File::create("recieved.txt") { + Ok(fc) => fc, + Err(e) => panic!("Problem creating the file: {:?}", e), + }, + other_error => { + panic!("Problem opening the file: {:?}", other_error) + } + }, + }; + let flag = 0; + loop { + println!("Please type the function:"); + let mut arg: String = String::new(); + io::stdin().read_line(&mut arg).expect("input failed"); + let arg1 = arg.into_bytes(); + + if flag == 0 { + if arg1.eq(&_help1) { + help(); + } else if arg1.eq(&_ser) { + ports(); + } else if arg1.eq(&_port1) { + new_port(); + } else if arg1.eq(&_read1) { + read(&mut f); + } else if arg1.eq(&_write1) { + } else if arg1.eq(&_exit1) { + break; + } else { + } + } /* + match arg1{ + help1=>{help();}, + ser=>{ports_avaialable();}, + port1=>{println!("port is already chosen")}, + read=>{ + let mut str=String::new(); + read(&mut str); + }, + exit1=>{//std::mem::drop + break; + }, + _=>(), + }; + */ + } +} + +fn help() { + println!("1.Serial_Ports_available\n2.new_port\n3.read\n4.exit"); +} +fn ports() { + let ports = serialport::available_ports().expect("No ports found!"); + for p in ports { + println!("{}", p.port_name); + } +} + +fn new_port() { + println!("Please give the name of port:"); + + let mut name: String = String::new(); + io::stdin().read_line(&mut name).expect("input failed"); + let name = name.trim(); + + println!("Please give the baud rate:"); + + let mut baud1: String = String::new(); + io::stdin().read_line(&mut baud1).expect("input failed"); + let baud = baud1.trim().parse::().unwrap(); + + let _serial_port = serialport::new(name, baud) + .timeout(Duration::from_millis(100)) + .open() + .expect("Failed to open port"); +} +fn read(f:&mut File) { + println!("Please give the name of port:"); + let mut name: String = String::new(); + io::stdin().read_line(&mut name).expect("input failed"); + let name = name.trim(); + + println!("Please give the baud rate:"); + + let mut baud1: String = String::new(); + io::stdin().read_line(&mut baud1).expect("input failed"); + let baud = baud1.trim().parse::().unwrap(); + + let mut port = serialport::new(name, baud) + .timeout(Duration::from_millis(100)) + .open() + .expect("Failed to open port"); + + + loop { + println!("Send Data:"); + f.write_all(b"Send Data:").expect("Unable to write data"); + let mut arg: String = String::new(); + io::stdin().read_line(&mut arg).expect("input failed"); + let arg1 = arg.trim().as_bytes(); + let _exit2: Vec = vec![101, 120, 105, 116, 13, 10]; + if arg1.eq(&_exit2){ + break; + } + port.write(arg1).expect("Write failed!"); + time(); + f.write_all(&arg1).expect("Unable to write data"); + + let mut serial_buf: Vec = vec![0; 32]; + let _k = port.read(serial_buf.as_mut_slice()); + let _k = match _k { + Ok(t) => { + println!("Data Recieved:{:?}", serial_buf); + time(); + f.write_all(b"Send Data:").expect("Unable to write data"); + f.write_all(&serial_buf).expect("Unable to write data"); + t + } + Err(_e) => (0), + }; + } +} + + +fn time(){ + let now = Local::now(); + let (_is_pm, _hour) = now.hour12(); + let (_is_common_era, _year) = now.year_ce(); + println!( + "{:02}:{:02}:{:02} {} {}-{:02}-{:02} {:?}", + _hour, + now.minute(), + now.second(), + if _is_pm { "PM" } else { "AM" }, + _year, + now.month(), + now.day(), + now.weekday() + ); + +} \ No newline at end of file diff --git a/serialmonitor/src/recieved.txt b/serialmonitor/src/recieved.txt new file mode 100644 index 00000000..e69de29b From 28501412d61aa8058233f9a03d4ec0e6d3517bab Mon Sep 17 00:00:00 2001 From: Nikhil Gupta Date: Wed, 28 Jul 2021 17:57:06 +0530 Subject: [PATCH 2/3] #58 serial monitor completed --- serialmonitor/src/main.rs | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/serialmonitor/src/main.rs b/serialmonitor/src/main.rs index a1d0fb64..7fde4a97 100644 --- a/serialmonitor/src/main.rs +++ b/serialmonitor/src/main.rs @@ -23,7 +23,7 @@ use std::io::Write; use std::time::Duration; fn main() { println!("Welcome to Arduino Serial Monitor\nWrite help to get list of functions available:"); - + // let _help1: Vec = vec![104, 101, 108, 112, 13, 10]; let _ser: Vec = vec![ 83, 101, 114, 105, 97, 108, 95, 80, 111, 114, 116, 115, 95, 97, 118, 97, 105, 108, 97, 98, @@ -84,7 +84,7 @@ fn main() { */ } } - +///This function gives list of available functions: fn help() { println!("1.Serial_Ports_available\n2.new_port\n3.read\n4.exit"); } @@ -93,8 +93,8 @@ fn ports() { for p in ports { println!("{}", p.port_name); } + let _s3:Vec=time(); } - fn new_port() { println!("Please give the name of port:"); @@ -113,6 +113,8 @@ fn new_port() { .open() .expect("Failed to open port"); } + + fn read(f:&mut File) { println!("Please give the name of port:"); let mut name: String = String::new(); @@ -142,17 +144,21 @@ fn read(f:&mut File) { break; } port.write(arg1).expect("Write failed!"); - time(); + // let mut s1:Vec=Vec::new(); + let _s2:Vec=time(); f.write_all(&arg1).expect("Unable to write data"); + f.write_all(&_s2).expect("Unable to write data"); let mut serial_buf: Vec = vec![0; 32]; let _k = port.read(serial_buf.as_mut_slice()); let _k = match _k { Ok(t) => { println!("Data Recieved:{:?}", serial_buf); - time(); + //let mut s1:Vec=Vec::new(); + let _s1:Vec=time(); f.write_all(b"Send Data:").expect("Unable to write data"); f.write_all(&serial_buf).expect("Unable to write data"); + f.write_all(&_s1).expect("Unable to write data"); t } Err(_e) => (0), @@ -161,20 +167,29 @@ fn read(f:&mut File) { } -fn time(){ +fn time()->Vec{ let now = Local::now(); - let (_is_pm, _hour) = now.hour12(); + let _hour = now.hour(); let (_is_common_era, _year) = now.year_ce(); println!( - "{:02}:{:02}:{:02} {} {}-{:02}-{:02} {:?}", + "{:02}:{:02}:{:02} {}-{:02}-{:02} {:?}", _hour, now.minute(), now.second(), - if _is_pm { "PM" } else { "AM" }, _year, now.month(), now.day(), now.weekday() ); + let hour1:String=_hour.to_string(); + let min:String=now.minute().to_string(); + let sec:String=now.second().to_string(); + let year1:String=_hour.to_string(); + let month:String=now.minute().to_string(); + let day:String=now.second().to_string(); + let week:String=now.weekday().to_string(); +let s=hour1 + ":"+&min+":" + &sec + " " + &year1 + "-" +&month + "-" +&day + " " + &week; + let _s1:Vec=s.into_bytes(); + _s1 } \ No newline at end of file From 3cbf765cb2926324e11c1e4dd3d6bfcb9fb2ba44 Mon Sep 17 00:00:00 2001 From: Nikhil Gupta Date: Fri, 30 Jul 2021 16:39:00 +0530 Subject: [PATCH 3/3] #56 serial_monitor --- serialmonitor/Cargo.lock | 242 +++++++++++++++++++++++++++++++++++++- serialmonitor/Cargo.toml | 2 + serialmonitor/src/main.rs | 187 +++++++++++------------------ 3 files changed, 313 insertions(+), 118 deletions(-) diff --git a/serialmonitor/Cargo.lock b/serialmonitor/Cargo.lock index 880d2fd9..7298dd84 100644 --- a/serialmonitor/Cargo.lock +++ b/serialmonitor/Cargo.lock @@ -30,6 +30,26 @@ dependencies = [ "memchr", ] +[[package]] +name = "ansi_term" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b" +dependencies = [ + "winapi", +] + +[[package]] +name = "atty" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" +dependencies = [ + "hermit-abi", + "libc", + "winapi", +] + [[package]] name = "autocfg" version = "1.0.1" @@ -54,6 +74,12 @@ version = "0.1.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + [[package]] name = "chrono" version = "0.4.19" @@ -67,6 +93,64 @@ dependencies = [ "winapi", ] +[[package]] +name = "clap" +version = "2.33.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "37e58ac78573c40708d45522f0d80fa2f01cc4f9b4e2bf749807255454312002" +dependencies = [ + "ansi_term", + "atty", + "bitflags", + "strsim", + "textwrap", + "unicode-width", + "vec_map", +] + +[[package]] +name = "crossterm" +version = "0.20.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c0ebde6a9dd5e331cd6c6f48253254d117642c31653baa475e394657c59c1f7d" +dependencies = [ + "bitflags", + "crossterm_winapi", + "libc", + "mio", + "parking_lot", + "signal-hook", + "signal-hook-mio", + "winapi", +] + +[[package]] +name = "crossterm_winapi" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3a6966607622438301997d3dac0d2f6e9a90c68bb6bc1785ea98456ab93c0507" +dependencies = [ + "winapi", +] + +[[package]] +name = "hermit-abi" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" +dependencies = [ + "libc", +] + +[[package]] +name = "instant" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bee0328b1209d157ef001c94dd85b4f8f64139adb0eac2659f4b08382b2f474d" +dependencies = [ + "cfg-if 1.0.0", +] + [[package]] name = "libc" version = "0.2.98" @@ -93,6 +177,24 @@ dependencies = [ "pkg-config", ] +[[package]] +name = "lock_api" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0382880606dff6d15c9476c416d18690b72742aa7b605bb6dd6ec9030fbf07eb" +dependencies = [ + "scopeguard", +] + +[[package]] +name = "log" +version = "0.4.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "51b9bbe6c47d51fc3e1a9b945965946b4c44142ab8792c50835a980d362c2710" +dependencies = [ + "cfg-if 1.0.0", +] + [[package]] name = "mach" version = "0.1.2" @@ -117,6 +219,28 @@ version = "2.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b16bd47d9e329435e309c58469fe0791c2d0d1ba96ec0954152a5ae2b04387dc" +[[package]] +name = "mio" +version = "0.7.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8c2bdb6314ec10835cd3293dd268473a835c02b7b352e788be788b3c6ca6bb16" +dependencies = [ + "libc", + "log", + "miow", + "ntapi", + "winapi", +] + +[[package]] +name = "miow" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9f1c5b025cda876f66ef43a113f91ebc9f4ccef34843000e0adf6ebbab84e21" +dependencies = [ + "winapi", +] + [[package]] name = "nix" version = "0.16.1" @@ -125,11 +249,20 @@ checksum = "dd0eaf8df8bab402257e0a5c17a254e4cc1f72a93588a1ddfb5d356c801aa7cb" dependencies = [ "bitflags", "cc", - "cfg-if", + "cfg-if 0.1.10", "libc", "void", ] +[[package]] +name = "ntapi" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f6bb902e437b6d86e03cce10a7e2af662292c5dfef23b65899ea3ac9354ad44" +dependencies = [ + "winapi", +] + [[package]] name = "num-integer" version = "0.1.44" @@ -149,12 +282,46 @@ dependencies = [ "autocfg", ] +[[package]] +name = "parking_lot" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6d7744ac029df22dca6284efe4e898991d28e3085c706c972bcd7da4a27a15eb" +dependencies = [ + "instant", + "lock_api", + "parking_lot_core", +] + +[[package]] +name = "parking_lot_core" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa7a782938e745763fe6907fc6ba86946d72f49fe7e21de074e08128a99fb018" +dependencies = [ + "cfg-if 1.0.0", + "instant", + "libc", + "redox_syscall", + "smallvec", + "winapi", +] + [[package]] name = "pkg-config" version = "0.3.19" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3831453b3449ceb48b6d9c7ad7c96d5ea673e9b470a1dc578c2ce6521230884c" +[[package]] +name = "redox_syscall" +version = "0.2.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5ab49abadf3f9e1c4bc499e8845e152ad87d2ad2d30371841171169e9d75feee" +dependencies = [ + "bitflags", +] + [[package]] name = "regex" version = "1.5.4" @@ -172,11 +339,19 @@ version = "0.6.25" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b" +[[package]] +name = "scopeguard" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" + [[package]] name = "serialmonitor" version = "0.1.0" dependencies = [ "chrono", + "clap", + "crossterm", "serialport", ] @@ -189,7 +364,7 @@ dependencies = [ "CoreFoundation-sys", "IOKit-sys", "bitflags", - "cfg-if", + "cfg-if 0.1.10", "libudev", "mach 0.2.3", "nix", @@ -197,6 +372,57 @@ dependencies = [ "winapi", ] +[[package]] +name = "signal-hook" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "470c5a6397076fae0094aaf06a08e6ba6f37acb77d3b1b91ea92b4d6c8650c39" +dependencies = [ + "libc", + "signal-hook-registry", +] + +[[package]] +name = "signal-hook-mio" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "29fd5867f1c4f2c5be079aee7a2adf1152ebb04a4bc4d341f504b7dece607ed4" +dependencies = [ + "libc", + "mio", + "signal-hook", +] + +[[package]] +name = "signal-hook-registry" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e51e73328dc4ac0c7ccbda3a494dfa03df1de2f46018127f60c693f2648455b0" +dependencies = [ + "libc", +] + +[[package]] +name = "smallvec" +version = "1.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fe0f37c9e8f3c5a4a66ad655a93c74daac4ad00c441533bf5c6e7990bb42604e" + +[[package]] +name = "strsim" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" + +[[package]] +name = "textwrap" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060" +dependencies = [ + "unicode-width", +] + [[package]] name = "time" version = "0.1.44" @@ -208,6 +434,18 @@ dependencies = [ "winapi", ] +[[package]] +name = "unicode-width" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9337591893a19b88d8d87f2cec1e73fad5cdfd10e5a6f349f498ad6ea2ffb1e3" + +[[package]] +name = "vec_map" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191" + [[package]] name = "void" version = "1.0.2" diff --git a/serialmonitor/Cargo.toml b/serialmonitor/Cargo.toml index dd6a1597..698a1811 100644 --- a/serialmonitor/Cargo.toml +++ b/serialmonitor/Cargo.toml @@ -9,3 +9,5 @@ edition = "2018" [dependencies] serialport = "4.0.1" chrono = "0.4.19" +crossterm = "0.20.0" +clap = "2.33.3" diff --git a/serialmonitor/src/main.rs b/serialmonitor/src/main.rs index 7fde4a97..7a5c0ae6 100644 --- a/serialmonitor/src/main.rs +++ b/serialmonitor/src/main.rs @@ -1,5 +1,5 @@ // RustDuino : A generic HAL implementation for Arduino Boards in Rust -// Copyright (C) 2021 Nikhil Gupta,Indian Institute of Technology Kanpur +// Copyright (C) 2021 Nikhil Gupta, Indian Institute of Technology Kanpur // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU Affero General Public License as published @@ -13,6 +13,7 @@ // // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see + use serialport; use chrono::{Datelike, Timelike,Local}; use std::fs::File; @@ -21,18 +22,38 @@ use std::io; use std::io::ErrorKind; use std::io::Write; use std::time::Duration; +use clap::{Arg, App}; +use std::thread; fn main() { - println!("Welcome to Arduino Serial Monitor\nWrite help to get list of functions available:"); - // - let _help1: Vec = vec![104, 101, 108, 112, 13, 10]; - let _ser: Vec = vec![ - 83, 101, 114, 105, 97, 108, 95, 80, 111, 114, 116, 115, 95, 97, 118, 97, 105, 108, 97, 98, - 108, 101, 13, 10, - ]; - let _port1: Vec = vec![110, 101, 119, 95, 112, 111, 114, 116, 13, 10]; - let _read1: Vec = vec![114, 101, 97, 100, 13, 10]; - let _write1: Vec = vec![119, 114, 105, 116, 101, 13, 10]; - let _exit1: Vec = vec![101, 120, 105, 116, 13, 10]; + println!("Welcome to Arduino Serial Monitor"); + let args = App::new("Serialmonitor") + .version("0.1.0") + .about("nothing much") + .author("Teamrustduino(nikhil2121)") + .arg( + Arg::with_name("portname") + .short("l") + .long("portname") + .takes_value(true)) + .arg( + Arg::with_name("baud") + .short("b") + .long("baud") + .takes_value(true), + ).get_matches(); + let name: &str = args.value_of("portname").unwrap_or("COM4"); + let baud: &str = args.value_of("baud").unwrap_or("9600"); + + let baud1 = baud.trim().parse::().unwrap(); + let name1=name.clone(); + let mut port = serialport::new(name, baud1) + .timeout(Duration::from_millis(100)) + .open() + .expect("Failed to open port"); + let mut port1 = serialport::new(name1, baud1) + .timeout(Duration::from_millis(100)) + .open() + .expect("Failed to open port"); let f = OpenOptions::new().write(true).open("recieved.txt"); let mut f = match f { Ok(file) => file, @@ -46,96 +67,42 @@ fn main() { } }, }; - let flag = 0; - loop { - println!("Please type the function:"); - let mut arg: String = String::new(); - io::stdin().read_line(&mut arg).expect("input failed"); - let arg1 = arg.into_bytes(); - if flag == 0 { - if arg1.eq(&_help1) { - help(); - } else if arg1.eq(&_ser) { - ports(); - } else if arg1.eq(&_port1) { - new_port(); - } else if arg1.eq(&_read1) { - read(&mut f); - } else if arg1.eq(&_write1) { - } else if arg1.eq(&_exit1) { - break; - } else { + let f1 = OpenOptions::new().write(true).open("send.txt"); + let mut f1 = match f1 { + Ok(file) => file, + Err(error) => match error.kind() { + ErrorKind::NotFound => match File::create("send.txt") { + Ok(fc) => fc, + Err(e) => panic!("Problem creating the file: {:?}", e), + }, + other_error => { + panic!("Problem opening the file: {:?}", other_error) } - } /* - match arg1{ - help1=>{help();}, - ser=>{ports_avaialable();}, - port1=>{println!("port is already chosen")}, - read=>{ - let mut str=String::new(); - read(&mut str); - }, - exit1=>{//std::mem::drop - break; - }, - _=>(), - }; - */ - } -} -///This function gives list of available functions: -fn help() { - println!("1.Serial_Ports_available\n2.new_port\n3.read\n4.exit"); -} -fn ports() { - let ports = serialport::available_ports().expect("No ports found!"); - for p in ports { - println!("{}", p.port_name); + }, + }; + + thread::spawn(move || { + loop{ + let mut serial_buf: Vec = vec![0; 32]; + let _k = port1.read(serial_buf.as_mut_slice()); + let _k = match _k { + Ok(t) => { + println!("\nData Recieved:{:?}", serial_buf); + let _s1:Vec=time(); + f.write_all(b"Send Data:").expect("Unable to write data"); + f.write_all(&serial_buf).expect("Unable to write data"); + f.write_all(&_s1).expect("Unable to write data"); + t + } + Err(_e) => (0), + }; } - let _s3:Vec=time(); -} -fn new_port() { - println!("Please give the name of port:"); - - let mut name: String = String::new(); - io::stdin().read_line(&mut name).expect("input failed"); - let name = name.trim(); - - println!("Please give the baud rate:"); - - let mut baud1: String = String::new(); - io::stdin().read_line(&mut baud1).expect("input failed"); - let baud = baud1.trim().parse::().unwrap(); - - let _serial_port = serialport::new(name, baud) - .timeout(Duration::from_millis(100)) - .open() - .expect("Failed to open port"); -} - - -fn read(f:&mut File) { - println!("Please give the name of port:"); - let mut name: String = String::new(); - io::stdin().read_line(&mut name).expect("input failed"); - let name = name.trim(); - - println!("Please give the baud rate:"); - - let mut baud1: String = String::new(); - io::stdin().read_line(&mut baud1).expect("input failed"); - let baud = baud1.trim().parse::().unwrap(); - - let mut port = serialport::new(name, baud) - .timeout(Duration::from_millis(100)) - .open() - .expect("Failed to open port"); - + }); loop { - println!("Send Data:"); - f.write_all(b"Send Data:").expect("Unable to write data"); + println!("\nSend Data:"); + f1.write_all(b"Send Data:").expect("Unable to write data"); let mut arg: String = String::new(); io::stdin().read_line(&mut arg).expect("input failed"); let arg1 = arg.trim().as_bytes(); @@ -144,26 +111,12 @@ fn read(f:&mut File) { break; } port.write(arg1).expect("Write failed!"); - // let mut s1:Vec=Vec::new(); let _s2:Vec=time(); - f.write_all(&arg1).expect("Unable to write data"); - f.write_all(&_s2).expect("Unable to write data"); - - let mut serial_buf: Vec = vec![0; 32]; - let _k = port.read(serial_buf.as_mut_slice()); - let _k = match _k { - Ok(t) => { - println!("Data Recieved:{:?}", serial_buf); - //let mut s1:Vec=Vec::new(); - let _s1:Vec=time(); - f.write_all(b"Send Data:").expect("Unable to write data"); - f.write_all(&serial_buf).expect("Unable to write data"); - f.write_all(&_s1).expect("Unable to write data"); - t - } - Err(_e) => (0), - }; + f1.write_all(&arg1).expect("Unable to write data"); + f1.write_all(&_s2).expect("Unable to write data"); } + + } @@ -192,4 +145,6 @@ let s=hour1 + ":"+&min+":" + &sec + " " + &year1 + "-" +&month + "-" +&day + " " let _s1:Vec=s.into_bytes(); _s1 -} \ No newline at end of file +} + +