serial_unit_testing/serial/
settings.rs

1/*
2 * File: src/serial/settings.rs
3 * Date: 01.10.2018
4 * Author: MarkAtk
5 *
6 * MIT License
7 *
8 * Copyright (c) 2018 MarkAtk
9 *
10 * Permission is hereby granted, free of charge, to any person obtaining a copy of
11 * this software and associated documentation files (the "Software"), to deal in
12 * the Software without restriction, including without limitation the rights to
13 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
14 * of the Software, and to permit persons to whom the Software is furnished to do
15 * so, subject to the following conditions:
16 *
17 * The above copyright notice and this permission notice shall be included in all
18 * copies or substantial portions of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * SOFTWARE.
27 */
28
29use std::time::Duration;
30use std::convert::From;
31
32use serialport;
33
34/// Number of bits for each data character.
35#[derive(PartialEq, Clone, Eq, Copy, Debug)]
36pub enum DataBits {
37    /// 5 bits per character.
38    Five,
39    /// 6 bits per character.
40    Six,
41    /// 7 bits per character. Used for true ASCII.
42    Seven,
43    /// 8 bits per character. This is default in most cases.
44    Eight
45}
46
47impl From<serialport::DataBits> for DataBits {
48    fn from(data_bits: serialport::DataBits) -> Self {
49        match data_bits {
50            serialport::DataBits::Five => DataBits::Five,
51            serialport::DataBits::Six => DataBits::Six,
52            serialport::DataBits::Seven => DataBits::Seven,
53            serialport::DataBits::Eight => DataBits::Eight
54        }
55    }
56}
57
58impl From<DataBits> for serialport::DataBits {
59    fn from(data_bits: DataBits) -> Self {
60        match data_bits {
61            DataBits::Five => serialport::DataBits::Five,
62            DataBits::Six => serialport::DataBits::Six,
63            DataBits::Seven => serialport::DataBits::Seven,
64            DataBits::Eight => serialport::DataBits::Eight
65        }
66    }
67}
68
69/// Data parity check modes.
70#[derive(PartialEq, Clone, Eq, Copy, Debug)]
71pub enum Parity {
72    /// No parity bit used.
73    None,
74    /// Parity bit sets for even number of 1 bits.
75    Even,
76    /// Parity bit sets for odd number of 1 bits.
77    Odd
78}
79
80impl From<serialport::Parity> for Parity {
81    fn from(parity: serialport::Parity) -> Self {
82        match parity {
83            serialport::Parity::None => Parity::None,
84            serialport::Parity::Even => Parity::Even,
85            serialport::Parity::Odd => Parity::Odd
86        }
87    }
88}
89
90impl From<Parity> for serialport::Parity {
91    fn from(parity: Parity) -> Self {
92        match parity {
93            Parity::None => serialport::Parity::None,
94            Parity::Even => serialport::Parity::Even,
95            Parity::Odd => serialport::Parity::Odd
96        }
97    }
98}
99
100/// Number of stop bits.
101#[derive(PartialEq, Clone, Eq, Copy, Debug)]
102pub enum StopBits {
103    /// One stop bit.
104    One,
105    /// Two stop bits.
106    Two
107}
108
109impl From<serialport::StopBits> for StopBits {
110    fn from(stop_bits: serialport::StopBits) -> Self {
111        match stop_bits {
112            serialport::StopBits::One => StopBits::One,
113            serialport::StopBits::Two => StopBits::Two
114        }
115    }
116}
117
118impl From<StopBits> for serialport::StopBits {
119    fn from(stop_bits: StopBits) -> Self {
120        match stop_bits {
121            StopBits::One => serialport::StopBits::One,
122            StopBits::Two => serialport::StopBits::Two
123        }
124    }
125}
126
127/// Flow control modes.
128#[derive(PartialEq, Clone, Eq, Copy, Debug)]
129pub enum FlowControl {
130    /// No flow control.
131    None,
132    /// Flow control using ASCII XON/XOFF bytes.
133    Software,
134    /// Flow control using RTS/CTS or DTR/DSR signals.
135    Hardware
136}
137
138impl From<serialport::FlowControl> for FlowControl {
139    fn from(parity: serialport::FlowControl) -> Self {
140        match parity {
141            serialport::FlowControl::None => FlowControl::None,
142            serialport::FlowControl::Software => FlowControl::Software,
143            serialport::FlowControl::Hardware => FlowControl::Hardware
144        }
145    }
146}
147
148impl From<FlowControl> for serialport::FlowControl {
149    fn from(parity: FlowControl) -> Self {
150        match parity {
151            FlowControl::None => serialport::FlowControl::None,
152            FlowControl::Software => serialport::FlowControl::Software,
153            FlowControl::Hardware => serialport::FlowControl::Hardware
154        }
155    }
156}
157
158/// Settings of a serial port connection.
159#[derive(PartialEq, Clone, Eq, Copy, Debug)]
160pub struct Settings {
161    /// Baud rate in bits per second.
162    pub baud_rate: u32,
163    /// Timeout duration in milliseconds.
164    pub timeout: u64,
165    /// Number of data bits.
166    pub data_bits: DataBits,
167    /// Parity bit mode.
168    pub parity: Parity,
169    /// Number of stop bits.
170    pub stop_bits: StopBits,
171    /// Flow control mode.
172    pub flow_control: FlowControl
173}
174
175impl Settings {
176    pub fn to_short_string(&self) -> String {
177        let data_bits = match self.data_bits {
178            DataBits::Five => 5,
179            DataBits::Six => 6,
180            DataBits::Seven => 7,
181            DataBits::Eight => 8
182        };
183
184        let parity = match self.parity {
185            Parity::None => "N",
186            Parity::Even => "E",
187            Parity::Odd => "O"
188        };
189
190        let stop_bits = match self.stop_bits {
191            StopBits::One => 1,
192            StopBits::Two => 2
193        };
194
195        format!("{} {}{}{}", self.baud_rate, data_bits, parity, stop_bits)
196    }
197}
198
199impl Default for Settings {
200    fn default() -> Settings {
201        Settings {
202            baud_rate: 9600,
203            timeout: 1000,
204            data_bits: DataBits::Eight,
205            parity: Parity::None,
206            stop_bits: StopBits::One,
207            flow_control: FlowControl::None
208        }
209    }
210}
211
212impl From<serialport::SerialPortSettings> for Settings {
213    fn from(settings: serialport::SerialPortSettings) -> Self {
214        Settings {
215            baud_rate: settings.baud_rate,
216            timeout: settings.timeout.as_millis() as u64,
217            data_bits: settings.data_bits.into(),
218            parity: settings.parity.into(),
219            stop_bits: settings.stop_bits.into(),
220            flow_control: settings.flow_control.into()
221        }
222    }
223}
224
225impl From<Settings> for serialport::SerialPortSettings {
226    fn from(settings: Settings) -> Self {
227        serialport::SerialPortSettings {
228            baud_rate: settings.baud_rate,
229            timeout: Duration::from_millis(settings.timeout),
230            data_bits: settings.data_bits.into(),
231            parity: settings.parity.into(),
232            stop_bits: settings.stop_bits.into(),
233            flow_control: settings.flow_control.into()
234        }
235    }
236}