Connection
Device is the handle. Opening one finds the box, runs the handshake, and starts the background threads in one call. Dropping the last handle releases the port.
See also: choosing a port, threading, and keepalive & teardown.
Open a device Auto-detect, or a known path
fn Device::open(path: impl AsRef<Path>) -> Result<Device>
Blocks
fn Device::find() -> Result<Device>
Blocks
fn find_medius() -> Vec<PortInfo>
No round-trip
open and find block on the handshake. Auto-detect matches the USB identity of the WCH CH343 bridge in every box (vid 0x1A86, pid 0x55D3).
FUNCTIONS
| Name | Description |
|---|---|
open | Opens a serial path (Linux /dev/ttyACM0, Windows COM3). |
find | Opens the first matching port, or returns Error::NotFound. |
find_medius | Lists every match as a PortInfo without opening one. |
EXAMPLE
use medius::Device;
// auto-detect the box:
let dev = Device::find()?;
// or open a known path:
let dev = Device::open("/dev/ttyACM0")?;Zero config Three read-only values
Two defaults bound the QUERY wait and the keepalive timer.
| Constant | Value |
|---|---|
DEFAULT_QUERY_TIMEOUT | 1 s |
DEFAULT_KEEPALIVE_CADENCE | 500 ms |
PROTO_VER | 9 |
PROTO_VER is the control protocol version this build speaks. A box reporting anything else is refused at the handshake.
EXAMPLE
use medius::{DEFAULT_QUERY_TIMEOUT, DEFAULT_KEEPALIVE_CADENCE, PROTO_VER};
println!("query timeout: {:?}", DEFAULT_QUERY_TIMEOUT); // 1s
println!("keepalive cadence: {:?}", DEFAULT_KEEPALIVE_CADENCE); // 500ms
println!("speaks protocol: {}", PROTO_VER); // 9Async device Same link, awaitable queries
fn AsyncDevice::open(path: impl AsRef<Path>) -> Result<AsyncDevice>
Blocks
fn AsyncDevice::find() -> Result<AsyncDevice>
Blocks
fn into_async(self) -> AsyncDevice
No round-trip
fn into_inner(self) -> Device
No round-trip
Behind the async feature, AsyncDevice makes the queries futures. Fire-and-forget calls stay synchronous.
cargo add medius --features asyncEXAMPLE
use futures::executor::block_on;
use medius::AsyncDevice;
// find and open as async:
let dev = AsyncDevice::find()?;
let version = block_on(dev.query_version())?; // awaits the reply
dev.move_rel(10, 0)?; // fire-and-forget, stays sync
// or open a known path:
let dev = AsyncDevice::open("/dev/ttyACM0")?;