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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#![allow(unused_features)]
#![cfg_attr(test, plugin(quickcheck_macros))]
#![deny(missing_docs, warnings)]
#![feature(convert)]
#![feature(custom_attribute)]
#![feature(fs)]
#![feature(io)]
#![feature(io_ext)]
#![feature(path)]
#![feature(plugin)]
#![feature(std_misc)]
extern crate termios;
#[cfg(test)]
extern crate quickcheck;
use std::fmt;
use std::fs::{File, self};
use std::io::{Read, Write, self};
use std::os::unix::io::AsRawFd;
use std::path::Path;
pub use termios::BaudRate;
use termios::prelude::*;
#[cfg(test)]
mod socat;
#[cfg(test)]
mod test;
#[derive(Clone, Copy, PartialEq)]
pub struct BlockingMode {
pub bytes: u8,
pub deciseconds: u8,
}
pub struct OpenOptions(fs::OpenOptions);
impl OpenOptions {
pub fn new() -> OpenOptions {
OpenOptions(fs::OpenOptions::new())
}
pub fn read(&mut self, read: bool) -> &mut OpenOptions {
self.0.read(read);
self
}
pub fn write(&mut self, write: bool) -> &mut OpenOptions {
self.0.write(write);
self
}
pub fn open<P: ?Sized>(&self, port: &P) -> io::Result<SerialPort> where
P: AsRef<Path>,
{
self.open_(port.as_ref())
}
fn open_(&self, path: &Path) -> io::Result<SerialPort> {
let file = try!(self.0.open(path));
let mut termios = try!(Termios::fetch(file.as_raw_fd()));
termios.make_raw();
let sp = SerialPort(file);
try!(sp.update(termios));
Ok(sp)
}
}
pub struct SerialPort(File);
impl SerialPort {
pub fn open(port: &Path) -> io::Result<SerialPort> {
OpenOptions::new().open(port)
}
pub fn baud_rate(&self) -> io::Result<(BaudRate, BaudRate)> {
self.fetch().map(|termios| {
(termios.ispeed(), termios.ospeed())
})
}
pub fn blocking_mode(&self) -> io::Result<BlockingMode> {
self.fetch().map(|termios| {
BlockingMode {
bytes: termios.cc[control::Char::VMIN],
deciseconds: termios.cc[control::Char::VTIME],
}
})
}
pub fn data_bits(&self) -> io::Result<DataBits> {
self.fetch().map(|termios| {
match termios.get::<control::CSIZE>() {
control::CSIZE::CS5 => DataBits::Five,
control::CSIZE::CS6 => DataBits::Six,
control::CSIZE::CS7 => DataBits::Seven,
control::CSIZE::CS8 => DataBits::Eight,
}
})
}
pub fn flow_control(&self) -> io::Result<FlowControl> {
self.fetch().map(|termios| {
if termios.contains(control::Flag::CRTSCTS) {
FlowControl::Hardware
} else if termios.contains(input::Flag::IXANY) &&
termios.contains(input::Flag::IXOFF) &&
termios.contains(input::Flag::IXON)
{
FlowControl::Software
} else {
FlowControl::None
}
})
}
pub fn parity(&self) -> io::Result<Parity> {
self.fetch().map(|termios| {
match (
termios.contains(control::Flag::PARENB),
termios.contains(control::Flag::PARODD),
) {
(true, true) => Parity::Odd,
(true, false) => Parity::Even,
(false, _) => Parity::None,
}
})
}
pub fn set_baud_rate(&mut self, direction: Direction, rate: BaudRate) -> io::Result<()> {
self.fetch().and_then(|mut termios| {
match direction {
Direction::Both => termios.set_speed(rate),
Direction::Input => termios.set_ispeed(rate),
Direction::Output => termios.set_ospeed(rate),
}
self.update(termios)
})
}
pub fn set_blocking_mode(&mut self, mode: BlockingMode) -> io::Result<()> {
self.fetch().and_then(|mut termios| {
termios.cc[control::Char::VMIN] = mode.bytes;
termios.cc[control::Char::VTIME] = mode.deciseconds;
self.update(termios)
})
}
pub fn set_data_bits(&mut self, bits: DataBits) -> io::Result<()> {
self.fetch().and_then(|mut termios| {
termios.set(match bits {
DataBits::Five => control::CSIZE::CS5,
DataBits::Six => control::CSIZE::CS6,
DataBits::Seven => control::CSIZE::CS7,
DataBits::Eight => control::CSIZE::CS8,
});
self.update(termios)
})
}
pub fn set_flow_control(&mut self, flow: FlowControl) -> io::Result<()> {
self.fetch().and_then(|mut termios| {
match flow {
FlowControl::Hardware => {
termios.clear(input::Flag::IXANY);
termios.clear(input::Flag::IXOFF);
termios.clear(input::Flag::IXON);
termios.set(control::Flag::CRTSCTS);
},
FlowControl::None => {
termios.clear(control::Flag::CRTSCTS);
termios.clear(input::Flag::IXANY);
termios.clear(input::Flag::IXOFF);
termios.clear(input::Flag::IXON);
},
FlowControl::Software => {
termios.clear(control::Flag::CRTSCTS);
termios.set(input::Flag::IXANY);
termios.set(input::Flag::IXOFF);
termios.set(input::Flag::IXON);
},
}
self.update(termios)
})
}
pub fn set_parity(&mut self, parity: Parity) -> io::Result<()> {
self.fetch().and_then(|mut termios| {
match parity {
Parity::Even => {
termios.clear(control::Flag::PARODD);
termios.set(control::Flag::PARENB);
},
Parity::None => termios.clear(control::Flag::PARENB),
Parity::Odd => {
termios.set(control::Flag::PARENB);
termios.set(control::Flag::PARODD);
},
}
self.update(termios)
})
}
pub fn set_stop_bits(&mut self, bits: StopBits) -> io::Result<()> {
self.fetch().and_then(|mut termios| {
match bits {
StopBits::One => termios.clear(control::Flag::CSTOPB),
StopBits::Two => termios.set(control::Flag::CSTOPB),
}
self.update(termios)
})
}
pub fn stop_bits(&self) -> io::Result<StopBits> {
self.fetch().map(|termios| {
if termios.contains(control::Flag::CSTOPB) {
StopBits::Two
} else {
StopBits::One
}
})
}
fn fetch(&self) -> io::Result<Termios> {
Termios::fetch(self.0.as_raw_fd())
}
fn update(&self, termios: Termios) -> io::Result<()> {
termios.update(self.0.as_raw_fd(), When::Now)
}
}
impl Read for SerialPort {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
self.0.read(buf)
}
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> {
self.0.read_to_end(buf)
}
fn read_to_string(&mut self, buf: &mut String) -> io::Result<usize> {
self.0.read_to_string(buf)
}
}
impl Write for SerialPort {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.0.write(buf)
}
fn flush(&mut self) -> io::Result<()> {
self.0.flush()
}
fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
self.0.write_all(buf)
}
fn write_fmt(&mut self, fmt: fmt::Arguments) -> io::Result<()> {
self.0.write_fmt(fmt)
}
}
#[allow(missing_docs)]
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum DataBits {
Five,
Six,
Seven,
Eight,
}
#[allow(missing_docs)]
#[derive(Clone, Copy)]
pub enum Direction {
Both,
Input,
Output,
}
#[allow(missing_docs)]
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum FlowControl {
Hardware,
None,
Software,
}
#[allow(missing_docs)]
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Parity {
Even,
None,
Odd,
}
#[allow(missing_docs)]
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum StopBits {
One,
Two,
}