-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
142 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
// SPDX-FileCopyrightText: Copyright (c) 2017-2023 slowtec GmbH <[email protected]> | ||
// SPDX-License-Identifier: MIT OR Apache-2.0 | ||
|
||
use super::{Context, Result}; | ||
use std::{io::Result, time::Duration}; | ||
|
||
use super::{block_on_with_timeout, Context}; | ||
|
||
use tokio_serial::{SerialPortBuilder, SerialStream}; | ||
|
||
|
@@ -14,17 +16,36 @@ pub fn connect(builder: &SerialPortBuilder) -> Result<Context> { | |
connect_slave(builder, Slave::broadcast()) | ||
} | ||
|
||
/// Connect to no particular Modbus slave device for sending | ||
/// broadcast messages with a timeout. | ||
pub fn connect_with_timeout( | ||
builder: &SerialPortBuilder, | ||
timeout: Option<Duration>, | ||
) -> Result<Context> { | ||
connect_slave_with_timeout(builder, Slave::broadcast(), timeout) | ||
} | ||
|
||
/// Connect to any kind of Modbus slave device. | ||
pub fn connect_slave(builder: &SerialPortBuilder, slave: Slave) -> Result<Context> { | ||
let rt = tokio::runtime::Builder::new_current_thread() | ||
connect_slave_with_timeout(builder, slave, None) | ||
} | ||
|
||
/// Connect to any kind of Modbus slave device with a timeout. | ||
pub fn connect_slave_with_timeout( | ||
builder: &SerialPortBuilder, | ||
slave: Slave, | ||
timeout: Option<Duration>, | ||
) -> Result<Context> { | ||
let runtime = tokio::runtime::Builder::new_current_thread() | ||
.enable_io() | ||
.build()?; | ||
// SerialStream::open requires a runtime at least on cfg(unix). | ||
let serial = rt.block_on(async { SerialStream::open(builder) })?; | ||
let async_ctx = rt.block_on(async_connect_slave(serial, slave))?; | ||
let serial = runtime.block_on(async { SerialStream::open(builder) })?; | ||
let async_ctx = block_on_with_timeout(&runtime, timeout, async_connect_slave(serial, slave))?; | ||
let sync_ctx = Context { | ||
core: rt, | ||
runtime, | ||
async_ctx, | ||
timeout, | ||
}; | ||
Ok(sync_ctx) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters