Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [5.0.3 and 5.0.4] 2023-01-12
- update dependencies
- Fix "Invalid data bits setting encountered" panic with some virtual COM ports

## [5.0.2] 2022-03-04
- merged [#28](https://github.com/berkowski/mio-serial/pull/28)
Expand Down
34 changes: 21 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,7 @@ mod os_prelude {
pub use winapi::um::fileapi::*;
pub use winapi::um::handleapi::INVALID_HANDLE_VALUE;
pub use winapi::um::winbase::{COMMTIMEOUTS, FILE_FLAG_OVERLAPPED};
pub use winapi::um::winnt::{
FILE_ATTRIBUTE_NORMAL, GENERIC_READ, GENERIC_WRITE, HANDLE,
};
pub use winapi::um::winnt::{FILE_ATTRIBUTE_NORMAL, GENERIC_READ, GENERIC_WRITE, HANDLE};
}
use os_prelude::*;

Expand Down Expand Up @@ -550,11 +548,11 @@ impl TryFrom<NativeBlockingSerialPort> for SerialStream {
let name = port
.name()
.ok_or_else(|| crate::Error::new(crate::ErrorKind::NoDevice, "Empty device name"))?;
let baud = port.baud_rate()?;
let parity = port.parity()?;
let data_bits = port.data_bits()?;
let stop_bits = port.stop_bits()?;
let flow_control = port.flow_control()?;
let baud = port.baud_rate().ok();
let parity = port.parity().ok();
let data_bits = port.data_bits().ok();
let stop_bits = port.stop_bits().ok();
let flow_control = port.flow_control().ok();

let mut path = Vec::<u16>::new();
path.extend(OsStr::new("\\\\.\\").encode_wide());
Expand Down Expand Up @@ -597,11 +595,21 @@ impl TryFrom<NativeBlockingSerialPort> for SerialStream {
mem::ManuallyDrop::new(unsafe { serialport::COMPort::from_raw_handle(handle) });

log::debug!("re-setting serial port parameters to original values from synchronous port");
com_port.set_baud_rate(baud)?;
com_port.set_parity(parity)?;
com_port.set_data_bits(data_bits)?;
com_port.set_stop_bits(stop_bits)?;
com_port.set_flow_control(flow_control)?;
if let Some(baud) = baud {
com_port.set_baud_rate(baud)?;
}
if let Some(parity) = parity {
com_port.set_parity(parity)?;
}
if let Some(data_bits) = data_bits {
com_port.set_data_bits(data_bits)?;
}
if let Some(stop_bits) = stop_bits {
com_port.set_stop_bits(stop_bits)?;
}
if let Some(flow_control) = flow_control {
com_port.set_flow_control(flow_control)?;
}
sys::override_comm_timeouts(handle)?;

Ok(Self {
Expand Down