serial_unit_testing/serial/
settings.rs1use std::time::Duration;
30use std::convert::From;
31
32use serialport;
33
34#[derive(PartialEq, Clone, Eq, Copy, Debug)]
36pub enum DataBits {
37 Five,
39 Six,
41 Seven,
43 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#[derive(PartialEq, Clone, Eq, Copy, Debug)]
71pub enum Parity {
72 None,
74 Even,
76 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#[derive(PartialEq, Clone, Eq, Copy, Debug)]
102pub enum StopBits {
103 One,
105 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#[derive(PartialEq, Clone, Eq, Copy, Debug)]
129pub enum FlowControl {
130 None,
132 Software,
134 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#[derive(PartialEq, Clone, Eq, Copy, Debug)]
160pub struct Settings {
161 pub baud_rate: u32,
163 pub timeout: u64,
165 pub data_bits: DataBits,
167 pub parity: Parity,
169 pub stop_bits: StopBits,
171 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}