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
/// The settings of a serial port.
#[derive(Clone)]
pub struct Settings {
pub(crate) inner: crate::sys::Settings,
}
/// Common baud rates used by many applications and devices.
///
/// Note that Linux, *BSD, Windows and Apple platforms all support custom baud rates, so you are not limited to these values.
/// It is also not guaranteed that all devices support these speeds.
///
/// These speeds can be useful to populate a user interface with some common options though.
pub const COMMON_BAUD_RATES: &[u32] = &[
4800, 9600, 19200, 38400, 57600, 115200, 230400, 460800, 500000, 576000, 921600, 1000000, 1500000, 2000000,
];
/// The number of bits per character for a serial port.
///
/// <div>
/// <span class="stab portability" style="display: inline">Available on <strong>crate feature <code>serde</code></strong> only:</span>
/// This type supports (de)serialization as a number.
/// </div>
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd)]
#[repr(u8)]
pub enum CharSize {
/// Characters of 5 bits.
///
/// <div class="item-info" style="margin-left: 0">
/// <span class="stab portability" style="display: inline">Available on <strong>crate feature <code>serde</code></strong> only:</span>
/// This variant is (de)serialized as the number <code>5</code>.
/// </div>
Bits5 = 5,
/// Characters of 6 bits.
///
/// <div class="item-info" style="margin-left: 0">
/// <span class="stab portability" style="display: inline">Available on <strong>crate feature <code>serde</code></strong> only:</span>
/// This variant is (de)serialized as the number <code>7</code>.
/// </div>
Bits6 = 6,
/// Characters of 7 bits.
///
/// <div class="item-info" style="margin-left: 0">
/// <span class="stab portability" style="display: inline">Available on <strong>crate feature <code>serde</code></strong> only:</span>
/// This variant is (de)serialized as the number <code>7</code>.
/// </div>
Bits7 = 7,
/// Characters of 8 bits.
///
/// <div class="item-info" style="margin-left: 0">
/// <span class="stab portability" style="display: inline">Available on <strong>crate feature <code>serde</code></strong> only:</span>
/// This variant is (de)serialized as the number <code>8</code>.
/// </div>
Bits8 = 8,
}
/// The number of stop bits per character for a serial port.
///
/// <div>
/// <span class="stab portability" style="display: inline">Available on <strong>crate feature <code>serde</code></strong> only:</span>
/// This type supports (de)serialization as a number.
/// </div>
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd)]
#[repr(u8)]
pub enum StopBits {
/// One stop bit.
///
/// <div class="item-info" style="margin-left: 0">
/// <span class="stab portability" style="display: inline">Available on <strong>crate feature <code>serde</code></strong> only:</span>
/// This variant is (de)serialized as the number <code>1</code>.
/// </div>
One = 1,
/// Two stop bit.
///
/// <div class="item-info" style="margin-left: 0">
/// <span class="stab portability" style="display: inline">Available on <strong>crate feature <code>serde</code></strong> only:</span>
/// This variant is (de)serialized as the number <code>2</code>.
/// </div>
Two = 2,
}
/// The type of parity check for a serial port.
///
/// <div>
/// <span class="stab portability" style="display: inline">Available on <strong>crate feature <code>serde</code></strong> only:</span>
/// This type supports (de)serialization as a string.
/// </div>
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd)]
pub enum Parity {
/// Do not add a parity bit and do not check for parity.
///
/// <div class="item-info" style="margin-left: 0">
/// <span class="stab portability" style="display: inline">Available on <strong>crate feature <code>serde</code></strong> only:</span>
/// This variant is (de)serialized as the string <code>"none"</code>.
/// </div>
None,
/// Add a bit to ensure odd parity of all characters send over the serial port.
///
/// Received characters are also expected to have a parity bit and odd parity.
/// What happens with received characters that have invalid parity is platform and device specific.
///
///
/// <div class="item-info" style="margin-left: 0">
/// <span class="stab portability" style="display: inline">Available on <strong>crate feature <code>serde</code></strong> only:</span>
/// This variant is (de)serialized as the string <code>"odd"</code>.
/// </div>
Odd,
/// Add a bit to ensure even parity of all characters send over the serial port.
///
/// Received characters are also expected to have a parity bit and even parity.
/// What happens with received characters that have invalid parity is platform and device specific.
///
///
/// <div class="item-info" style="margin-left: 0">
/// <span class="stab portability" style="display: inline">Available on <strong>crate feature <code>serde</code></strong> only:</span>
/// This variant is (de)serialized as the string <code>"even"</code>.
/// </div>
Even,
}
/// The type of flow control for a serial port.
///
/// <div>
/// <span class="stab portability" style="display: inline">Available on <strong>crate feature <code>serde</code></strong> only:</span>
/// This type supports (de)serialization as a string.
/// </div>
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd)]
pub enum FlowControl {
/// Do not perform any automatic flow control.
///
/// <div class="item-info" style="margin-left: 0">
/// <span class="stab portability" style="display: inline">Available on <strong>crate feature <code>serde</code></strong> only:</span>
/// This variant is (de)serialized as the string <code>"none"</code>.
/// </div>
None,
/// Perform XON/XOFF flow control.
///
/// This is also sometimes referred to as "software flow control".
///
/// <div class="item-info" style="margin-left: 0">
/// <span class="stab portability" style="display: inline">Available on <strong>crate feature <code>serde</code></strong> only:</span>
/// This variant is (de)serialized as the string <code>"xon/xoff"</code>.
/// </div>
XonXoff,
/// Perform RTS/CTS flow control.
///
/// This is also sometimes referred to as "hardware flow control".
///
/// <div class="item-info" style="margin-left: 0">
/// <span class="stab portability" style="display: inline">Available on <strong>crate feature <code>serde</code></strong> only:</span>
/// This variant is (de)serialized as the string <code>"rts/cts"</code>.
/// </div>
RtsCts,
}
impl Settings {
/// Disable all OS level input and output processing.
///
/// All input and output processing will be disabled,
/// and the configuration will be set for 8 bit binary communication,
/// one stop bit, no parity checks and no flow control.
///
/// This is usually a good starting point for manual configuration.
pub fn set_raw(&mut self) {
self.inner.set_raw();
}
/// Set the baud rate to be configured.
///
/// This function returns an error if the platform does not support the requested band-width.
/// Note that the device itself may also not support the requested baud rate, even if the platform does.
/// In that case [`SerialPort::set_configuration()`][crate::SerialPort::set_configuration] will return an error.
pub fn set_baud_rate(&mut self, baud_rate: u32) -> std::io::Result<()> {
self.inner.set_baud_rate(baud_rate)
}
/// Get the baud rate from the configuration.
pub fn get_baud_rate(&self) -> std::io::Result<u32> {
self.inner.get_baud_rate()
}
/// Set the number of bits in a character.
pub fn set_char_size(&mut self, char_size: CharSize) {
self.inner.set_char_size(char_size)
}
/// Get the number of bits in a character.
pub fn get_char_size(&self) -> std::io::Result<CharSize> {
self.inner.get_char_size()
}
/// Set the number of stop bits following each character.
pub fn set_stop_bits(&mut self, stop_bits: StopBits) {
self.inner.set_stop_bits(stop_bits)
}
/// Get the number of stop bits following each character.
pub fn get_stop_bits(&self) -> std::io::Result<StopBits> {
self.inner.get_stop_bits()
}
/// Set the partity check.
pub fn set_parity(&mut self, parity: Parity) {
self.inner.set_parity(parity)
}
/// Get the partity check.
pub fn get_parity(&self) -> std::io::Result<Parity> {
self.inner.get_parity()
}
/// Set the flow control mechanism.
///
/// See the individual documentation of the [`FlowControl`] variants for more information.
pub fn set_flow_control(&mut self, flow_control: FlowControl) {
self.inner.set_flow_control(flow_control)
}
/// Get the flow control mechanism
pub fn get_flow_control(&self) -> std::io::Result<FlowControl> {
self.inner.get_flow_control()
}
/// Get a reference to the raw `termios` struct.
///
/// On Linux and Android this is actually a `termios2` struct.
/// On other Unix platforms, this is a `termios` struct.
///
/// You can use this function to access Unix specific features of the serial port.
/// You code will not be cross platform anymore if you use this.
#[cfg(any(doc, all(unix, feature = "unix")))]
#[cfg_attr(feature = "doc-cfg", doc(cfg(feature = "unix")))]
pub fn as_termios(&self) -> &crate::os::unix::RawTermios {
&self.inner.termios
}
/// Get a mutable reference to the raw `termios` struct.
///
/// On Linux and Android this is actually a `termios2` struct.
/// On other Unix platforms, this is a `termios` struct.
///
/// You can use this function to access Unix specific features of the serial port.
/// You code will not be cross platform anymore if you use this.
#[cfg(any(doc, all(unix, feature = "unix")))]
#[cfg_attr(feature = "doc-cfg", doc(cfg(feature = "unix")))]
pub fn as_termios_mut(&mut self) -> &mut crate::os::unix::RawTermios {
&mut self.inner.termios
}
/// Get a reference to the raw `DCB` struct.
///
/// You can use this function to access Windows specific features of the serial port.
/// You code will not be cross platform anymore if you use this.
#[cfg(any(doc, all(windows, feature = "windows")))]
#[cfg_attr(feature = "doc-cfg", doc(cfg(feature = "windows")))]
pub fn as_raw_dbc(&self) -> &crate::os::windows::DCB {
&self.inner.dcb
}
/// Get a mutable reference to the raw `DCB` struct.
///
/// You can use this function to access Windows specific features of the serial port.
/// You code will not be cross platform anymore if you use this.
#[cfg(any(doc, all(windows, feature = "windows")))]
#[cfg_attr(feature = "doc-cfg", doc(cfg(feature = "windows")))]
pub fn as_raw_dbc_mut(&mut self) -> &mut crate::os::windows::DCB {
&mut self.inner.dcb
}
}
impl std::fmt::Debug for Settings {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Settings")
.field("baud_rate", &self.get_baud_rate())
.field("char_size", &self.get_char_size())
.field("stop_bits", &self.get_stop_bits())
.field("parity", &self.get_parity())
.field("flow_control", &self.get_flow_control())
.finish()
}
}
#[cfg(feature = "serde")]
impl serde::Serialize for CharSize {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
serializer.serialize_u8(*self as u8)
}
}
#[cfg(feature = "serde")]
impl<'de> serde::Deserialize<'de> for CharSize {
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
struct Visitor;
impl<'de> serde::de::Visitor<'de> for Visitor {
type Value = CharSize;
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
formatter.write_str("the number 5, 6, 7 or 8")
}
fn visit_u64<E: serde::de::Error>(self, data: u64) -> Result<Self::Value, E> {
match data {
5 => Ok(CharSize::Bits5),
6 => Ok(CharSize::Bits6),
7 => Ok(CharSize::Bits7),
8 => Ok(CharSize::Bits8),
x => Err(E::invalid_value(serde::de::Unexpected::Unsigned(x), &"the number 5, 6, 7 or 8")),
}
}
}
deserializer.deserialize_u8(Visitor)
}
}
#[cfg(feature = "serde")]
impl serde::Serialize for StopBits {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
serializer.serialize_u8(*self as u8)
}
}
#[cfg(feature = "serde")]
impl<'de> serde::Deserialize<'de> for StopBits {
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
struct Visitor;
impl<'de> serde::de::Visitor<'de> for Visitor {
type Value = StopBits;
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
formatter.write_str("the number 1 or 2")
}
fn visit_u64<E: serde::de::Error>(self, data: u64) -> Result<Self::Value, E> {
match data {
1 => Ok(StopBits::One),
2 => Ok(StopBits::Two),
x => Err(E::invalid_value(serde::de::Unexpected::Unsigned(x), &"the number 1 or 2")),
}
}
}
deserializer.deserialize_u8(Visitor)
}
}
#[cfg(feature = "serde")]
impl serde::Serialize for Parity {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
match self {
Self::None => serializer.serialize_str("none"),
Self::Even => serializer.serialize_str("even"),
Self::Odd => serializer.serialize_str("odd"),
}
}
}
#[cfg(feature = "serde")]
impl<'de> serde::Deserialize<'de> for Parity {
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
struct Visitor;
impl<'de> serde::de::Visitor<'de> for Visitor {
type Value = Parity;
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
formatter.write_str("the string \"none\", \"even\" or \"odd\"")
}
fn visit_str<E: serde::de::Error>(self, data: &str) -> Result<Self::Value, E> {
match data {
"none" => Ok(Parity::None),
"even" => Ok(Parity::Even),
"odd" => Ok(Parity::Odd),
x => Err(E::invalid_value(serde::de::Unexpected::Str(x), &"the string \"none\", \"even\" or \"odd\"")),
}
}
}
deserializer.deserialize_str(Visitor)
}
}
#[cfg(feature = "serde")]
impl serde::Serialize for FlowControl {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
match self {
Self::None => serializer.serialize_str("none"),
Self::XonXoff => serializer.serialize_str("xon/xoff"),
Self::RtsCts => serializer.serialize_str("rts/cts"),
}
}
}
#[cfg(feature = "serde")]
impl<'de> serde::Deserialize<'de> for FlowControl {
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
struct Visitor;
impl<'de> serde::de::Visitor<'de> for Visitor {
type Value = FlowControl;
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
formatter.write_str("the string \"none\", \"xon/xoff\" or \"rts/cts\"")
}
fn visit_str<E: serde::de::Error>(self, data: &str) -> Result<Self::Value, E> {
match data {
"none" => Ok(FlowControl::None),
"xon/xoff" => Ok(FlowControl::XonXoff),
"rts/cts" => Ok(FlowControl::RtsCts),
x => Err(E::invalid_value(serde::de::Unexpected::Str(x), &"the string \"none\", \"xon/xoff\" or \"rts/cts\"")),
}
}
}
deserializer.deserialize_str(Visitor)
}
}