serialport5 5.0.2

A cross-platform low-level serial port library
Documentation
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
//! This example performs a test using real hardware ports.
//!
//! This tool serves as a debugging aid when encountering errors using `serialport-rs`. It should
//! expose any kernel or driver bugs that your system may have by running physical ports through
//! many configurations.
//!
//! There are 3 ways to run this example:
//!
//!  1) With a single port not connected to an external device:
//!     `cargo run --example hardware_check /dev/ttyUSB0`
//!
//!  2) With a single port physically connected in loopback mode (RX<->TX)
//!     `cargo run --example hardware_check /dev/ttyUSB0 --loopback`
//!
//!  3) With two ports physically connected to each other
//!     `cargo run --example hardware_check /dev/ttyUSB0 /dev/ttyUSB1`

use std::io::{Read, Write};
use std::str;

use clap::{App, AppSettings, Arg};

use serialport5::{ClearBuffer, DataBits, FlowControl, Parity, SerialPort, StopBits};

fn main() {
    let matches = App::new("Serialport Example - Hardware Check")
        .about("Test hardware capabilities of serial ports")
        .setting(AppSettings::DisableVersionFlag)
        .arg(Arg::new("port")
             .help("The device path to a serial port")
             .use_delimiter(false)
             .required(true))
        .arg(Arg::new("loopback")
             .help("Run extra tests if the port is configured for hardware loopback. Mutually exclusive with the --loopback-port option")
             .use_delimiter(false)
             .conflicts_with("loopback-port")
             .long("loopback"))
        .arg(Arg::new("loopback-port")
             .help("The device path of a second serial port that is connected to the first serial port. Mutually exclusive with the --loopback option.")
             .use_delimiter(false)
             .takes_value(true)
             .long("loopback-port"))
        .get_matches();
    let port1_name = matches.value_of("port").unwrap();
    let port2_name = matches.value_of("loopback-port").unwrap_or("");
    let port1_loopback = matches.is_present("loopback");

    // Loopback mode is only available when a single port is specified
    if port1_loopback && port2_name != "" {
        eprintln!("ERROR: loopback mode can only be enabled when a single port is specified.");
        ::std::process::exit(1);
    }

    // Run single-port tests on port1
    let mut port1 = match SerialPort::builder().open(port1_name) {
        Err(e) => {
            eprintln!("Failed to open \"{}\". Error: {}", port1_name, e);
            ::std::process::exit(1);
        }
        Ok(p) => p,
    };
    test_single_port(&mut port1, port1_loopback);

    if port2_name != "" {
        // Run single-port tests on port2
        let mut port2 = match SerialPort::builder().open(port2_name) {
            Err(e) => {
                eprintln!("Failed to open \"{}\". Error: {}", port2_name, e);
                ::std::process::exit(1);
            }
            Ok(p) => p,
        };
        test_single_port(&mut port2, false);

        // Test loopback pair
        test_dual_ports(&mut port1, &mut port2);
    }
}

macro_rules! baud_rate_check {
    ($port:ident, $baud:expr) => {
        let baud_rate = $baud;
        if let Err(e) = $port.set_baud_rate(baud_rate) {
            println!("  {:?}: FAILED ({})", baud_rate, e);
        }
        match $port.baud_rate() {
            Err(_) => println!("  {:?}: FAILED (error retrieving baud rate)", baud_rate),
            Ok(r) if r != baud_rate => println!(
                "  {:?}: FAILED (baud rate {:?} does not match set baud rate {:?})",
                baud_rate, r, baud_rate
            ),
            Ok(_) => println!("  {:?}: success", baud_rate),
        }
    };
}

macro_rules! data_bits_check {
    ($port:ident, $data_bits:path) => {
        let data_bits = $data_bits;
        if let Err(e) = $port.set_data_bits(data_bits) {
            println!("  {:?}: FAILED ({})", data_bits, e);
        } else {
            match $port.data_bits() {
                Err(_) => println!("FAILED to retrieve data bits"),
                Ok(r) if r != data_bits => println!(
                    "  {:?}: FAILED (data bits {:?} does not match set data bits {:?})",
                    data_bits, r, data_bits
                ),
                Ok(_) => println!("  {:?}: success", data_bits),
            }
        }
    };
}

macro_rules! flow_control_check {
    ($port:ident, $flow_control:path) => {
        let flow_control = $flow_control;
        if let Err(e) = $port.set_flow_control(flow_control) {
            println!("  {:?}: FAILED ({})", flow_control, e);
        } else {
            match $port.flow_control() {
                Err(_) => println!("FAILED to retrieve flow control"),
                Ok(r) if r != flow_control => println!(
                    "  {:?}: FAILED (flow control {:?} does not match set flow control {:?})",
                    flow_control, r, flow_control
                ),
                Ok(_) => println!("  {:?}: success", flow_control),
            }
        }
    };
}

macro_rules! parity_check {
    ($port:ident, $parity:path) => {
        let parity = $parity;
        if let Err(e) = $port.set_parity(parity) {
            println!("  {:?}: FAILED ({})", parity, e);
        } else {
            match $port.parity() {
                Err(_) => println!("FAILED to retrieve parity"),
                Ok(r) if r != parity => println!(
                    "  {:?}: FAILED (parity {:?} does not match set parity {:?})",
                    parity, r, parity
                ),
                Ok(_) => println!("  {:?}: success", parity),
            }
        }
    };
}

macro_rules! stop_bits_check {
    ($port:ident, $stop_bits:path) => {
        let stop_bits = $stop_bits;
        if let Err(e) = $port.set_stop_bits(stop_bits) {
            println!("  {:?}: FAILED ({})", stop_bits, e);
        } else {
            match $port.stop_bits() {
                Err(_) => println!("FAILED to retrieve stop bits"),
                Ok(r) if r != stop_bits => println!(
                    "FAILED, stop bits {:?} does not match set stop bits {:?}",
                    r, stop_bits
                ),
                Ok(_) => println!("  {:?}: success", stop_bits),
            }
        }
    };
}

macro_rules! clear_check {
    ($port:ident, $buffer_direction:path) => {
        let buffer_direction = $buffer_direction;
        match $port.clear(buffer_direction) {
            Ok(_) => println!("  {:?}: success", buffer_direction),
            Err(ref e) => println!("  {:?}: FAILED ({})", buffer_direction, e),
        }
    };
}

macro_rules! call_query_method_check {
    ($port:ident, $func:path) => {
        match $func($port) {
            Ok(_) => println!("  {}: success", stringify!($func)),
            Err(ref e) => println!("  {}: FAILED ({})", stringify!($func), e),
        }
    };
}

fn test_single_port(port: &mut SerialPort, loopback: bool) {
    println!("Testing '{}':", port.name().unwrap());

    // Test setting standard baud rates
    println!("Testing baud rates...");
    baud_rate_check!(port, 9600);
    baud_rate_check!(port, 38_400);
    baud_rate_check!(port, 115_200);

    // Test setting non-standard baud rates
    println!("Testing non-standard baud rates...");
    baud_rate_check!(port, 10_000);
    baud_rate_check!(port, 600_000);
    baud_rate_check!(port, 1_800_000);

    // Test setting the data bits
    println!("Testing data bits...");
    data_bits_check!(port, DataBits::Five);
    data_bits_check!(port, DataBits::Six);
    data_bits_check!(port, DataBits::Seven);
    data_bits_check!(port, DataBits::Eight);

    // Test setting flow control
    println!("Testing flow control...");
    flow_control_check!(port, FlowControl::Software);
    flow_control_check!(port, FlowControl::Hardware);
    flow_control_check!(port, FlowControl::None);

    // Test setting parity
    println!("Testing parity...");
    parity_check!(port, Parity::Odd);
    parity_check!(port, Parity::Even);
    parity_check!(port, Parity::None);

    // Test setting stop bits
    println!("Testing stop bits...");
    stop_bits_check!(port, StopBits::Two);
    stop_bits_check!(port, StopBits::One);

    // Test bytes to read and write
    println!("Testing bytes to read and write...");
    call_query_method_check!(port, SerialPort::bytes_to_write);
    call_query_method_check!(port, SerialPort::bytes_to_read);

    // Test clearing a buffer
    println!("Test clearing software buffers...");
    clear_check!(port, ClearBuffer::Input);
    clear_check!(port, ClearBuffer::Output);
    clear_check!(port, ClearBuffer::All);

    // Test transmitting data
    print!("Testing data transmission...");
    std::io::stdout().flush().unwrap();
    // Make sure the port has sane defaults
    set_defaults(port);
    let msg = "Test Message";
    port.write_all(msg.as_bytes())
        .expect("Unable to write bytes.");
    println!("success");

    print!("Testing data reception...");
    if loopback {
        let mut buf = [0u8; 12];
        if port.read_exact(&mut buf).is_err() {
            println!("FAILED");
        } else {
            assert_eq!(
                str::from_utf8(&buf).unwrap(),
                msg,
                "Received message does not match sent"
            );
            println!("success");
        }
    }
}

fn test_dual_ports(port1: &mut SerialPort, port2: &mut SerialPort) {
    println!(
        "Testing paired ports '{}' and '{}':",
        port1.name().unwrap(),
        port2.name().unwrap()
    );

    // Make sure both ports are set to sane defaults
    set_defaults(port1);
    set_defaults(port2);

    let msg = "Test Message";
    let mut buf = [0u8; 12];

    // Test sending strings from port1 to port2
    println!(
        "  Transmitting from {} to {}...",
        port1.name().unwrap(),
        port2.name().unwrap()
    );
    let baud_rate = 2_000_000;
    print!("     At {},8,n,1,noflow...", baud_rate);
    std::io::stdout().flush().unwrap();
    if port1.set_baud_rate(baud_rate).is_ok() && port2.set_baud_rate(baud_rate).is_ok() {
        port1
            .write_all(msg.as_bytes())
            .expect("Unable to write bytes.");
        if port2.read_exact(&mut buf).is_err() {
            println!("FAILED");
        } else {
            assert_eq!(
                str::from_utf8(&buf).unwrap(),
                msg,
                "Received message does not match sent"
            );
            println!("success");
        }
    } else {
        println!("FAILED (does this platform & port support arbitrary baud rates?)");
    }
    let baud_rate = 115_200;
    print!("     At {},8,n,1,noflow...", baud_rate);
    std::io::stdout().flush().unwrap();
    if port1.set_baud_rate(baud_rate).is_ok() && port2.set_baud_rate(baud_rate).is_ok() {
        port1
            .write_all(msg.as_bytes())
            .expect("Unable to write bytes.");
        if port2.read_exact(&mut buf).is_err() {
            println!("FAILED");
        } else {
            assert_eq!(
                str::from_utf8(&buf).unwrap(),
                msg,
                "Received message does not match sent"
            );
            println!("success");
        }
    } else {
        println!("FAILED");
    }
    let baud_rate = 57_600;
    print!("     At {},8,n,1,noflow...", baud_rate);
    std::io::stdout().flush().unwrap();
    if port1.set_baud_rate(baud_rate).is_ok() && port2.set_baud_rate(baud_rate).is_ok() {
        port1
            .write_all(msg.as_bytes())
            .expect("Unable to write bytes.");
        if port2.read_exact(&mut buf).is_err() {
            println!("FAILED");
        } else {
            assert_eq!(
                str::from_utf8(&buf).unwrap(),
                msg,
                "Received message does not match sent"
            );
            println!("success");
        }
    } else {
        println!("FAILED");
    }
    let baud_rate = 10_000;
    print!("     At {},8,n,1,noflow...", baud_rate);
    std::io::stdout().flush().unwrap();
    if port1.set_baud_rate(baud_rate).is_ok() && port2.set_baud_rate(baud_rate).is_ok() {
        port1
            .write_all(msg.as_bytes())
            .expect("Unable to write bytes.");
        if port2.read_exact(&mut buf).is_err() {
            println!("FAILED");
        } else {
            assert_eq!(
                str::from_utf8(&buf).unwrap(),
                msg,
                "Received message does not match sent"
            );
            println!("success");
        }
    } else {
        println!("FAILED (does this platform & port support arbitrary baud rates?)");
    }
    let baud_rate = 9600;
    print!("     At {},8,n,1,noflow...", baud_rate);
    std::io::stdout().flush().unwrap();
    if port1.set_baud_rate(baud_rate).is_ok() && port2.set_baud_rate(baud_rate).is_ok() {
        port1
            .write_all(msg.as_bytes())
            .expect("Unable to write bytes.");
        if port2.read_exact(&mut buf).is_err() {
            println!("FAILED");
        } else {
            assert_eq!(
                str::from_utf8(&buf).unwrap(),
                msg,
                "Received message does not match sent"
            );
            println!("success");
        }
    } else {
        println!("FAILED");
    }

    // Test flow control
    port1.set_flow_control(FlowControl::Software).unwrap();
    port2.set_flow_control(FlowControl::Software).unwrap();
    print!("     At 9600,8,n,1,softflow...");
    std::io::stdout().flush().unwrap();
    port2
        .write_all(msg.as_bytes())
        .expect("Unable to write bytes.");
    if port1.read_exact(&mut buf).is_err() {
        println!("FAILED");
    } else {
        assert_eq!(
            str::from_utf8(&buf).unwrap(),
            msg,
            "Received message does not match sent"
        );
        println!("success");
    }
    port1.set_flow_control(FlowControl::Hardware).unwrap();
    port2.set_flow_control(FlowControl::Hardware).unwrap();
    print!("     At 9600,8,n,1,hardflow...");
    std::io::stdout().flush().unwrap();
    port2
        .write_all(msg.as_bytes())
        .expect("Unable to write bytes.");
    if port1.read_exact(&mut buf).is_err() {
        println!("FAILED");
    } else {
        assert_eq!(
            str::from_utf8(&buf).unwrap(),
            msg,
            "Received message does not match sent"
        );
        println!("success");
    }
}

fn set_defaults(port: &mut SerialPort) {
    port.set_baud_rate(9600).unwrap();
    port.set_data_bits(DataBits::Eight).unwrap();
    port.set_flow_control(FlowControl::Software).unwrap();
    port.set_parity(Parity::None).unwrap();
    port.set_stop_bits(StopBits::One).unwrap();
    port.set_read_timeout(None).unwrap();
    port.set_write_timeout(None).unwrap();
}