pub struct SerialManager { /* private fields */ }Expand description
Owns every open port (desktop serial, tcp://, ble://, Android USB, CMUX channels).
Cheap to clone (all clones share the same ports). Methods block: call them from a
plain thread or tokio::task::spawn_blocking, not straight from an async task.
Lock order: serialports before virtual_ports; never hold a map lock across
device I/O, thread spawn, or JoinHandle join.
Implementations§
Source§impl SerialManager
impl SerialManager
Sourcepub fn available_ports_detailed(&self) -> Result<Vec<PortDetails>, Error>
pub fn available_ports_detailed(&self) -> Result<Vec<PortDetails>, Error>
Every serial port with typed details (numeric vid/pid, USB interface number,
location, Linux /dev/serial links), sorted by path. The 4.x
Self::available_ports map is unchanged. On Android the fields come from
the 4.x map (no interface / location).
Source§impl SerialManager
impl SerialManager
Sourcepub fn supports_modem_lines(&self, path: &str) -> bool
pub fn supports_modem_lines(&self, path: &str) -> bool
false when path has no real RTS/DTR/CTS/DSR/RI/DCD: tcp:// bridges and
ble:// UARTs, where the modem-line setters still succeed (the value is stored
locally) and the getters return fixed values, and CMUX channels
(path#dlci=N), where line calls fail (the lines belong to the physical
port). Check this before relying on hardware flow control or line signalling.
Local serial ports return true. Needs no open port.
Source§impl SerialManager
impl SerialManager
Sourcepub fn new() -> Self
pub fn new() -> Self
Examples found in repository?
10fn main() -> Result<(), serialplugin_core::Error> {
11 let mut args = std::env::args().skip(1);
12 let path = args.next().expect("usage: headless <path> [baud]");
13 let baud = args.next().map_or(115_200, |b| b.parse().expect("baud"));
14
15 let serial = SerialManager::new();
16 let path = serial.open(path, baud, None, None, None, None, None)?;
17 let sink: EventSink = Arc::new(|event| {
18 match event {
19 SerialEvent::Data { data, .. } => println!("{}", String::from_utf8_lossy(&data)),
20 SerialEvent::Disconnect { path, reason } => {
21 eprintln!("{path} disconnected: {reason}");
22 std::process::exit(1);
23 }
24 other => eprintln!("{other:?}"),
25 }
26 true
27 });
28 let parser =
29 serde_json::from_value(serde_json::json!({"type": "readline", "delimiter": "\r\n"}))
30 .expect("parser config");
31 serial.watch_framed(path, WatchOptions::default(), Some(parser), sink)?;
32 loop {
33 std::thread::park();
34 }
35}Sourcepub fn available_ports(
&self,
single_port_per_device: bool,
) -> Result<HashMap<String, HashMap<String, String>>, Error>
pub fn available_ports( &self, single_port_per_device: bool, ) -> Result<HashMap<String, HashMap<String, String>>, Error>
Get serial port list
Sourcepub fn watch_ports(
&self,
options: WatchPortsOptions,
sink: PortListSink,
) -> Result<u32, Error>
pub fn watch_ports( &self, options: WatchPortsOptions, sink: PortListSink, ) -> Result<u32, Error>
Subscribe to available-port attach/detach events. sink first gets a
Snapshot, then added / removed.
Returns the id for Self::unwatch_ports.
Sourcepub fn watch_ports_with_id(
&self,
id: u32,
options: WatchPortsOptions,
sink: PortListSink,
) -> Result<u32, Error>
pub fn watch_ports_with_id( &self, id: u32, options: WatchPortsOptions, sink: PortListSink, ) -> Result<u32, Error>
Self::watch_ports under a caller-chosen id (the plugin uses its IPC channel id).
Sourcepub fn unwatch_ports(&self, channel_id: u32) -> Result<(), Error>
pub fn unwatch_ports(&self, channel_id: u32) -> Result<(), Error>
Stop a port-list watch session.
Sourcepub fn cancel_read(&self, path: String) -> Result<(), Error>
pub fn cancel_read(&self, path: String) -> Result<(), Error>
Cancel an in-flight poll Self::read (does not stop an active Self::watch).
Sourcepub fn close(&self, path: String) -> Result<(), Error>
pub fn close(&self, path: String) -> Result<(), Error>
Close the specified serial port. What the hub still holds (the watch batch, a
parser’s remainder) reaches the watches first, in order; no Disconnect is
sent. Succeeds once on a port the core already released after a disconnect.
Sourcepub fn close_core(&self, path: String) -> Result<(), CoreError>
pub fn close_core(&self, path: String) -> Result<(), CoreError>
Self::close with a CoreError.
Sourcepub fn open(
&self,
path: String,
baud_rate: u32,
data_bits: Option<DataBits>,
flow_control: Option<FlowControl>,
parity: Option<Parity>,
stop_bits: Option<StopBits>,
timeout: Option<u64>,
) -> Result<String, Error>
pub fn open( &self, path: String, baud_rate: u32, data_bits: Option<DataBits>, flow_control: Option<FlowControl>, parity: Option<Parity>, stop_bits: Option<StopBits>, timeout: Option<u64>, ) -> Result<String, Error>
Examples found in repository?
10fn main() -> Result<(), serialplugin_core::Error> {
11 let mut args = std::env::args().skip(1);
12 let path = args.next().expect("usage: headless <path> [baud]");
13 let baud = args.next().map_or(115_200, |b| b.parse().expect("baud"));
14
15 let serial = SerialManager::new();
16 let path = serial.open(path, baud, None, None, None, None, None)?;
17 let sink: EventSink = Arc::new(|event| {
18 match event {
19 SerialEvent::Data { data, .. } => println!("{}", String::from_utf8_lossy(&data)),
20 SerialEvent::Disconnect { path, reason } => {
21 eprintln!("{path} disconnected: {reason}");
22 std::process::exit(1);
23 }
24 other => eprintln!("{other:?}"),
25 }
26 true
27 });
28 let parser =
29 serde_json::from_value(serde_json::json!({"type": "readline", "delimiter": "\r\n"}))
30 .expect("parser config");
31 serial.watch_framed(path, WatchOptions::default(), Some(parser), sink)?;
32 loop {
33 std::thread::park();
34 }
35}Sourcepub fn open_with_lines(
&self,
path: String,
baud_rate: u32,
data_bits: Option<DataBits>,
flow_control: Option<FlowControl>,
parity: Option<Parity>,
stop_bits: Option<StopBits>,
timeout: Option<u64>,
lines: ModemLinesOnOpen,
) -> Result<String, Error>
pub fn open_with_lines( &self, path: String, baud_rate: u32, data_bits: Option<DataBits>, flow_control: Option<FlowControl>, parity: Option<Parity>, stop_bits: Option<StopBits>, timeout: Option<u64>, lines: ModemLinesOnOpen, ) -> Result<String, Error>
Self::open with explicit DTR / RTS levels on open.
Sourcepub fn open_core(
&self,
path: String,
baud_rate: u32,
data_bits: Option<DataBits>,
flow_control: Option<FlowControl>,
parity: Option<Parity>,
stop_bits: Option<StopBits>,
timeout: Option<u64>,
lines: ModemLinesOnOpen,
) -> Result<String, CoreError>
pub fn open_core( &self, path: String, baud_rate: u32, data_bits: Option<DataBits>, flow_control: Option<FlowControl>, parity: Option<Parity>, stop_bits: Option<StopBits>, timeout: Option<u64>, lines: ModemLinesOnOpen, ) -> Result<String, CoreError>
Self::open_with_lines with a CoreError (kind NotFound, PermissionDenied,
Busy, … and the platform error as its source). timeout (ms, default 1000)
is the port’s read / write timeout; Self::set_timeout changes it later.
Sourcepub fn insert_port(
&self,
path: String,
port: Box<dyn SerialPortTrait>,
) -> Result<(), CoreError>
pub fn insert_port( &self, path: String, port: Box<dyn SerialPortTrait>, ) -> Result<(), CoreError>
Manage port under path as if Self::open had opened it (tests, custom
transports, a fake device). An open port under path is closed first. Its
current timeout() is the port timeout.
On Unix (not TCP / BLE) writes and control calls go to port.try_clone() when
it succeeds, reads to port: a clone must share the device (like a dup’ed fd),
or try_clone must return Err to keep one handle for everything.
Sourcepub fn watch(
&self,
path: String,
options: WatchOptions,
sink: EventSink,
) -> Result<u32, Error>
pub fn watch( &self, path: String, options: WatchOptions, sink: EventSink, ) -> Result<u32, Error>
Stream port events into sink (one watch per port and manager). Returns the id
for Self::unwatch. As in 4.x, sink runs on the RX side: the port’s hub
thread, or the caller of unwatch / close for the tail they deliver. It never
runs under a manager lock, but a slow sink holds up RX (keep it short, e.g.
forward into a channel), and calling at() / exchange on the same port from
it waits for the RX it blocks. Nothing reaches it after close() returns.
Self::watch_core has its own delivery thread instead.
Sourcepub fn watch_with_id(
&self,
channel_id: u32,
path: String,
options: WatchOptions,
sink: EventSink,
) -> Result<u32, Error>
pub fn watch_with_id( &self, channel_id: u32, path: String, options: WatchOptions, sink: EventSink, ) -> Result<u32, Error>
Self::watch under a caller-chosen id (the plugin uses its IPC channel id).
Sourcepub fn watch_framed(
&self,
path: String,
options: WatchOptions,
parser: Option<FramerConfig>,
sink: EventSink,
) -> Result<u32, Error>
pub fn watch_framed( &self, path: String, options: WatchOptions, parser: Option<FramerConfig>, sink: EventSink, ) -> Result<u32, Error>
Self::watch with a native parser: each complete frame becomes exactly one
SerialEvent::Data (the batch timer no longer
applies). An invalid config fails before the watch starts. With
WatchOptions::route_urc the URC split runs first and only non-URC lines reach
the parser. On unwatch, close or disconnect the parser’s buffered remainder is
sent as a final data event (before disconnect), like Node serialport emits
on stream end.
Examples found in repository?
10fn main() -> Result<(), serialplugin_core::Error> {
11 let mut args = std::env::args().skip(1);
12 let path = args.next().expect("usage: headless <path> [baud]");
13 let baud = args.next().map_or(115_200, |b| b.parse().expect("baud"));
14
15 let serial = SerialManager::new();
16 let path = serial.open(path, baud, None, None, None, None, None)?;
17 let sink: EventSink = Arc::new(|event| {
18 match event {
19 SerialEvent::Data { data, .. } => println!("{}", String::from_utf8_lossy(&data)),
20 SerialEvent::Disconnect { path, reason } => {
21 eprintln!("{path} disconnected: {reason}");
22 std::process::exit(1);
23 }
24 other => eprintln!("{other:?}"),
25 }
26 true
27 });
28 let parser =
29 serde_json::from_value(serde_json::json!({"type": "readline", "delimiter": "\r\n"}))
30 .expect("parser config");
31 serial.watch_framed(path, WatchOptions::default(), Some(parser), sink)?;
32 loop {
33 std::thread::park();
34 }
35}Sourcepub fn watch_framed_with_id(
&self,
channel_id: u32,
path: String,
options: WatchOptions,
parser: Option<FramerConfig>,
channel: EventSink,
) -> Result<u32, Error>
pub fn watch_framed_with_id( &self, channel_id: u32, path: String, options: WatchOptions, parser: Option<FramerConfig>, channel: EventSink, ) -> Result<u32, Error>
Self::watch_framed under a caller-chosen id.
pub fn unwatch(&self, channel_id: u32) -> Result<(), Error>
pub fn read( &self, path: String, timeout: Option<u64>, size: Option<usize>, ) -> Result<String, Error>
pub fn read_binary( &self, path: String, timeout: Option<u64>, size: Option<usize>, ) -> Result<Vec<u8>, Error>
Sourcepub fn write(&self, path: String, value: String) -> Result<usize, Error>
pub fn write(&self, path: String, value: String) -> Result<usize, Error>
Write data to the serial port (or a CMUX channel path#dlci=N).
Sourcepub fn write_binary(&self, path: String, value: Vec<u8>) -> Result<usize, Error>
pub fn write_binary(&self, path: String, value: Vec<u8>) -> Result<usize, Error>
Write binary data to the serial port (or a CMUX channel path#dlci=N).
Sourcepub fn write_core(
&self,
path: String,
data: Vec<u8>,
) -> Result<usize, CoreError>
pub fn write_core( &self, path: String, data: Vec<u8>, ) -> Result<usize, CoreError>
Self::write_binary with a CoreError (the I/O error as its source).
Sourcepub fn set_baud_rate(&self, path: String, baud_rate: u32) -> Result<(), Error>
pub fn set_baud_rate(&self, path: String, baud_rate: u32) -> Result<(), Error>
Set the baud rate
Sourcepub fn set_data_bits(
&self,
path: String,
data_bits: DataBits,
) -> Result<(), Error>
pub fn set_data_bits( &self, path: String, data_bits: DataBits, ) -> Result<(), Error>
Set the data bits
Sourcepub fn set_flow_control(
&self,
path: String,
flow_control: FlowControl,
) -> Result<(), Error>
pub fn set_flow_control( &self, path: String, flow_control: FlowControl, ) -> Result<(), Error>
Set the flow control
Sourcepub fn set_stop_bits(
&self,
path: String,
stop_bits: StopBits,
) -> Result<(), Error>
pub fn set_stop_bits( &self, path: String, stop_bits: StopBits, ) -> Result<(), Error>
Set the stop bits
Sourcepub fn set_timeout(&self, path: String, timeout: Duration) -> Result<(), Error>
pub fn set_timeout(&self, path: String, timeout: Duration) -> Result<(), Error>
Set the timeout
Sourcepub fn write_request_to_send(
&self,
path: String,
level: bool,
) -> Result<(), Error>
pub fn write_request_to_send( &self, path: String, level: bool, ) -> Result<(), Error>
Set the RTS (Request To Send) control signal
Sourcepub fn write_data_terminal_ready(
&self,
path: String,
level: bool,
) -> Result<(), Error>
pub fn write_data_terminal_ready( &self, path: String, level: bool, ) -> Result<(), Error>
Set the DTR (Data Terminal Ready) control signal
Sourcepub fn read_clear_to_send(&self, path: String) -> Result<bool, Error>
pub fn read_clear_to_send(&self, path: String) -> Result<bool, Error>
Read the CTS (Clear To Send) control signal state
Sourcepub fn read_data_set_ready(&self, path: String) -> Result<bool, Error>
pub fn read_data_set_ready(&self, path: String) -> Result<bool, Error>
Read the DSR (Data Set Ready) control signal state
Sourcepub fn read_ring_indicator(&self, path: String) -> Result<bool, Error>
pub fn read_ring_indicator(&self, path: String) -> Result<bool, Error>
Read the RI (Ring Indicator) control signal state
Sourcepub fn read_carrier_detect(&self, path: String) -> Result<bool, Error>
pub fn read_carrier_detect(&self, path: String) -> Result<bool, Error>
Read the CD (Carrier Detect) control signal state
Sourcepub fn bytes_to_read(&self, path: String) -> Result<u32, Error>
pub fn bytes_to_read(&self, path: String) -> Result<u32, Error>
Get the number of bytes available to read
Sourcepub fn bytes_to_write(&self, path: String) -> Result<u32, Error>
pub fn bytes_to_write(&self, path: String) -> Result<u32, Error>
Get the number of bytes waiting to be written
Sourcepub fn clear_buffer(
&self,
path: String,
buffer_to_clear: ClearBuffer,
) -> Result<(), Error>
pub fn clear_buffer( &self, path: String, buffer_to_clear: ClearBuffer, ) -> Result<(), Error>
Clear input/output buffers
Input / All also drop what the RX hub holds for no one yet.
Sourcepub fn exchange(
&self,
path: String,
value: String,
options: ExchangeOptions,
) -> Result<ExchangeResponse, Error>
pub fn exchange( &self, path: String, value: String, options: ExchangeOptions, ) -> Result<ExchangeResponse, Error>
Write payload and read until terminators, idle silence, or timeout (AT-style exchange).
Sourcepub fn exchange_binary(
&self,
path: String,
value: Vec<u8>,
options: ExchangeOptions,
) -> Result<ExchangeResponse, Error>
pub fn exchange_binary( &self, path: String, value: Vec<u8>, options: ExchangeOptions, ) -> Result<ExchangeResponse, Error>
Binary variant of Self::exchange.
Sourcepub fn exchange_core(
&self,
path: String,
value: Vec<u8>,
options: ExchangeOptions,
) -> Result<ExchangeResponse, CoreError>
pub fn exchange_core( &self, path: String, value: Vec<u8>, options: ExchangeOptions, ) -> Result<ExchangeResponse, CoreError>
Self::exchange_binary with a CoreError (Timeout, Cancelled, …).
pub fn at( &self, path: String, command: String, options: Option<AtCommandOptions>, ) -> Result<AtCommandResult, Error>
Sourcepub fn at_core(
&self,
path: String,
command: String,
options: Option<AtCommandOptions>,
) -> Result<AtCommandResult, CoreError>
pub fn at_core( &self, path: String, command: String, options: Option<AtCommandOptions>, ) -> Result<AtCommandResult, CoreError>
Sourcepub fn at_phases(
&self,
path: String,
phases: Vec<AtPhase>,
) -> Result<Vec<AtCommandResult>, Error>
pub fn at_phases( &self, path: String, phases: Vec<AtPhase>, ) -> Result<Vec<AtCommandResult>, Error>
Multi-phase AT flow (e.g. CMGS) — phases run atomically without interleaving other jobs.
Sourcepub fn send_sms_pdu(
&self,
path: String,
length: u32,
pdu: Vec<u8>,
options: Option<SendSmsPduOptions>,
) -> Result<Vec<AtCommandResult>, Error>
pub fn send_sms_pdu( &self, path: String, length: u32, pdu: Vec<u8>, options: Option<SendSmsPduOptions>, ) -> Result<Vec<AtCommandResult>, Error>
Built-in CMGS recipe: AT+CMGS=n → > → PDU + Ctrl+Z → final line.
Sourcepub fn configure_at_session(
&self,
path: String,
session: AtSessionConfig,
) -> Result<(), Error>
pub fn configure_at_session( &self, path: String, session: AtSessionConfig, ) -> Result<(), Error>
Configure AT session defaults for native at jobs on this port.
Sourcepub fn enable_mux(
&self,
path: String,
command: String,
timeout_ms: u64,
) -> Result<(), Error>
pub fn enable_mux( &self, path: String, command: String, timeout_ms: u64, ) -> Result<(), Error>
Enter GSM 07.10 CMUX mode (AT+CMUX=… then attach deframer to the RX hub).
Sourcepub fn open_mux_channel(
&self,
physical_path: String,
dlci: u8,
) -> Result<String, Error>
pub fn open_mux_channel( &self, physical_path: String, dlci: u8, ) -> Result<String, Error>
Register a virtual DLCI channel on an CMUX-enabled physical port.
Source§impl SerialManager
The 4.2 core API: CoreEvent streams, port-state subscriptions, queue limits.
impl SerialManager
The 4.2 core API: CoreEvent streams, port-state subscriptions, queue limits.
Sourcepub fn watch_core(
&self,
path: String,
options: WatchOptions,
parser: Option<FramerConfig>,
sink: CoreSink,
) -> Result<SubscriptionId, CoreError>
pub fn watch_core( &self, path: String, options: WatchOptions, parser: Option<FramerConfig>, sink: CoreSink, ) -> Result<SubscriptionId, CoreError>
Stream one port’s events into sink as CoreEvent::Event (the final
Disconnect included). Any number of these may run on a port, beside the old
Self::watch: each gets its own copy of the stream and its own parser /
route_urc state, and URCs caught during at() / exchange reach every one
with route_urc. See CoreSink for threading, the queue and overflow.
Sourcepub fn unwatch_core(&self, id: SubscriptionId) -> Result<(), CoreError>
pub fn unwatch_core(&self, id: SubscriptionId) -> Result<(), CoreError>
Stop a Self::watch_core stream; its batch and parser remainder arrive last.
NotFound also for a stream that already ended (overflow, sink returned
false, port gone).
Sourcepub fn subscribe_core(&self, sink: CoreSink) -> SubscriptionId
pub fn subscribe_core(&self, sink: CoreSink) -> SubscriptionId
Port states of every port of this manager (CoreEvent::PortState: opened,
disconnected with its kind and reason, closed); needs no watch. Same queue and
overflow rules as Self::watch_core.
pub fn unsubscribe_core(&self, id: SubscriptionId) -> Result<(), CoreError>
Sourcepub fn set_core_queue_capacity(&self, bytes: usize)
pub fn set_core_queue_capacity(&self, bytes: usize)
Queue limit (bytes of event data) of core receivers created from now on.
Default crate::events::DEFAULT_CORE_QUEUE_BYTES.