1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
|
extern crate serial;
use serial::prelude::*;
use std::{string::String, env, io, collections::HashMap, time::Duration};
const PARAMETER_TABLE : [&'static str; 2] = ["--port-name", "--disable-output"];
/// Main function.
fn main() {
let arg_hash: Option<HashMap<String, String>> = process_arguments();
// Read command line arguments.
match arg_hash {
None => return,
_ => app(arg_hash.unwrap())
}
}
fn app(arg_hash: HashMap<String, String>) {
}
struct CmdLineArg {
arg_str : &'static str,
param_str : Option<&'static str>,
is_required : bool,
explanation : &'static str
}
const CMD_LINE_ARGS : [CmdLineArg; 2] =
[
CmdLineArg {
arg_str : "--port-name",
param_str : Some("[PORT]"),
is_required : true,
explanation : "Sets serial port"
},
CmdLineArg {
arg_str : "--disable-output",
param_str : None,
is_required : false,
explanation : "Disables incoming debug messages from the console"
}
];
fn process_arguments() -> Option<HashMap<String, String>> {
if env::args_os().count() <= 1 {
println!("rspsxserial command line arguments:");
for arg in CMD_LINE_ARGS.iter() {
let mut line = String::new();
line.push_str(arg.arg_str);
match arg.param_str {
None => {},
_ => {
line.push(' ');
line.push_str(arg.param_str.unwrap());
}
};
line.push('\t');
line.push_str(arg.explanation);
println!("{}", line);
}
return None;
}
enum ExpectedParameter
{
ParameterOption,
ParameterValue
};
let mut parameter_state = ExpectedParameter::ParameterOption;
let mut arg_hash: HashMap<String, String> = HashMap::new();
let mut parameter_name = String::new();
for arg in env::args_os().skip(1) {
let arg_str = arg.into_string().unwrap();
match parameter_state {
ExpectedParameter::ParameterOption => {
if arg_str.starts_with("--")
{
// Looks like a valid parameter
for param in CMD_LINE_ARGS.iter() {
if arg_str == param.arg_str.to_string() {
println!("Found valid parameter {}", arg_str);
parameter_name = arg_str;
match param.param_str {
None => {
println!("This option uses no parameters");
arg_hash.insert(parameter_name.clone(), String::from(""));
parameter_state = ExpectedParameter::ParameterOption;
}
_ => {
parameter_state = ExpectedParameter::ParameterValue;
}
}
break;
}
}
}
else
{
return None;
}
},
ExpectedParameter::ParameterValue => {
let parameter_value = arg_str;
arg_hash.insert(parameter_name.clone(), parameter_value.clone());
parameter_state = ExpectedParameter::ParameterOption;
}
}
}
println!("Hash = {:?}", arg_hash);
Some(arg_hash)
}
/** let mut port = serial::open(&arg).unwrap();
interact(&mut port).unwrap();*/
/// This function reconfigures a serial port with default parameters
fn interact<T: SerialPort>(port: &mut T) -> io::Result<()> {
port.reconfigure(&|settings| {
settings.set_baud_rate(serial::Baud9600)?;
settings.set_char_size(serial::Bits8);
settings.set_parity(serial::ParityNone);
settings.set_stop_bits(serial::Stop1);
settings.set_flow_control(serial::FlowNone);
Ok(())
})?;
port.set_timeout(Duration::from_millis(1000))?;
let buf: Vec<u8> = (0..255).collect();
port.write(&buf[..])?;
//port.read(&mut buf[..])?;
Ok(())
}
|