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
/*
 * File: src/serial/settings.rs
 * Date: 01.10.2018
 * Author: MarkAtk
 * 
 * MIT License
 * 
 * Copyright (c) 2018 MarkAtk
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is furnished to do
 * so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

use std::time::Duration;

use serialport;

/// Number of bits for each data character.
#[derive(PartialEq, Clone, Eq, Copy, Debug)]
pub enum DataBits {
    /// 5 bits per character.
    Five,
    /// 6 bits per character.
    Six,
    /// 7 bits per character. Used for true ASCII.
    Seven,
    /// 8 bits per character. This is default in most cases.
    Eight
}

/// Data parity check modes.
#[derive(PartialEq, Clone, Eq, Copy, Debug)]
pub enum Parity {
    /// No parity bit used.
    None,
    /// Parity bit sets for even number of 1 bits.
    Even,
    /// Parity bit sets for odd number of 1 bits.
    Odd
}

/// Number of stop bits.
#[derive(PartialEq, Clone, Eq, Copy, Debug)]
pub enum StopBits {
    /// One stop bit.
    One,
    /// Two stop bits.
    Two
}

/// Flow control modes.
#[derive(PartialEq, Clone, Eq, Copy, Debug)]
pub enum FlowControl {
    /// No flow control.
    None,
    /// Flow control using ASCII XON/XOFF bytes.
    Software,
    /// Flow control using RTS/CTS or DTR/DSR signals.
    Hardware
}

/// Settings of a serial port connection.
#[derive(PartialEq, Clone, Eq, Copy, Debug)]
pub struct Settings {
    /// Baud rate in bits per second.
    pub baud_rate: u32,
    /// Timeout duration in milliseconds.
    pub timeout: u64,
    /// Number of data bits.
    pub data_bits: DataBits,
    /// Parity bit mode.
    pub parity: Parity,
    /// Number of stop bits.
    pub stop_bits: StopBits,
    /// Flow control mode.
    pub flow_control: FlowControl
}

impl Settings {
    pub (super) fn to_serial_port_settings(&self) -> serialport::SerialPortSettings {
        let data_bits = match self.data_bits {
            DataBits::Five => serialport::DataBits::Five,
            DataBits::Six => serialport::DataBits::Six,
            DataBits::Seven => serialport::DataBits::Seven,
            DataBits::Eight => serialport::DataBits::Eight
        };

        let parity = match self.parity {
            Parity::None => serialport::Parity::None,
            Parity::Even => serialport::Parity::Even,
            Parity::Odd => serialport::Parity::Odd
        };

        let stop_bits = match self.stop_bits {
            StopBits::One => serialport::StopBits::One,
            StopBits::Two => serialport::StopBits::Two
        };

        let flow_control = match self.flow_control {
            FlowControl::None => serialport::FlowControl::None,
            FlowControl::Software => serialport::FlowControl::Software,
            FlowControl::Hardware => serialport::FlowControl::Hardware
        };

        serialport::SerialPortSettings {
            baud_rate: self.baud_rate,
            timeout: Duration::from_millis(self.timeout),
            data_bits,
            parity,
            stop_bits,
            flow_control
        }
    }

    pub fn to_short_string(&self) -> String {
        let data_bits = match self.data_bits {
            DataBits::Five => 5,
            DataBits::Six => 6,
            DataBits::Seven => 7,
            DataBits::Eight => 8
        };

        let parity = match self.parity {
            Parity::None => "N",
            Parity::Even => "E",
            Parity::Odd => "O"
        };

        let stop_bits = match self.stop_bits {
            StopBits::One => 1,
            StopBits::Two => 2
        };

        format!("{} {}{}{}", self.baud_rate, data_bits, parity, stop_bits)
    }
}

impl Default for Settings {
    fn default() -> Settings {
        Settings {
            baud_rate: 9600,
            timeout: 1000,
            data_bits: DataBits::Eight,
            parity: Parity::None,
            stop_bits: StopBits::One,
            flow_control: FlowControl::None
        }
    }
}