Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement configurable buffer size on Windows (#173) #174

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
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
7 changes: 7 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,9 @@ pub trait SerialPort: Send + io::Read + io::Write {
/// Sets the timeout for future I/O operations.
fn set_timeout(&mut self, timeout: Duration) -> Result<()>;

/// Set the buffer size
fn set_buffer_size(&mut self, receive_size: usize, transmit_size: usize) -> Result<()>;

// Functions for setting non-data control signal pins

/// Sets the state of the RTS (Request To Send) control signal.
Expand Down Expand Up @@ -675,6 +678,10 @@ impl<T: SerialPort> SerialPort for &mut T {
(**self).set_timeout(timeout)
}

fn set_buffer_size(&mut self, receive_size: usize, transmit_size: usize) -> Result<()> {
(**self).set_buffer_size(receive_size, transmit_size)
}

fn write_request_to_send(&mut self, level: bool) -> Result<()> {
(**self).write_request_to_send(level)
}
Expand Down
7 changes: 7 additions & 0 deletions src/posix/tty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,13 @@
Ok(())
}

fn set_buffer_size(&mut self, _rx: usize, _tx: usize) -> Result<()> {
Err(Error::new(
ErrorKind::Unsupported,

Check failure on line 680 in src/posix/tty.rs

View workflow job for this annotation

GitHub Actions / lint

no variant or associated item named `Unsupported` found for enum `ErrorKind` in the current scope
"Configurable buffer sizes are not supported on this platform",
))
}

fn write_request_to_send(&mut self, level: bool) -> Result<()> {
self.set_pin(SerialLines::REQUEST_TO_SEND, level)
}
Expand Down
7 changes: 7 additions & 0 deletions src/windows/com.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,13 @@ impl SerialPort for COMPort {
Ok(())
}

fn set_buffer_size(&mut self, receive_size: usize, transmit_size: usize) -> Result<()> {
match unsafe { SetupComm(self.handle, receive_size as DWORD, transmit_size as DWORD) } {
0 => Err(super::error::last_os_error()),
_ => Ok(()),
}
}

fn write_request_to_send(&mut self, level: bool) -> Result<()> {
if level {
self.escape_comm_function(SETRTS)
Expand Down
35 changes: 35 additions & 0 deletions tests/test_buffer_size.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#![cfg(windows)]
extern crate serialport;

#[test]
fn test_set_large_buffer() {
const STRIDE: usize = 2;
const WORDS: usize = 65536;
const BYTES: usize = WORDS * STRIDE;

let mut tx_port = serialport::new("COM12", 9600).open().unwrap();
let mut rx_port = serialport::new("COM13", 9600).open().unwrap();

tx_port.set_buffer_size(0, BYTES).unwrap();
rx_port.set_buffer_size(BYTES, 0).unwrap();

let mut rx_buf = [0_u8; BYTES];
let mut tx_buf = [0_u8; BYTES];

for i in 0..WORDS {
for j in 0..STRIDE {
tx_buf[i * STRIDE + j] = (i >> (j * 8) & 0xFF) as u8;
}
}

tx_port.write_all(&tx_buf[..]).unwrap();
rx_port.read_exact(&mut rx_buf).unwrap();

let transmitted = tx_buf.to_vec();
let received = rx_buf.to_vec();

println!("Transmitted: {:?}", &transmitted[..8]);
println!("Received: {:?}", &received[..8]);

assert_eq!(transmitted, received);
}
Loading