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 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 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 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). Returns the id for
Self::unwatch. sink runs on the port’s RX thread, keep it short; the last
events (parser remainder on unwatch / close) may run on the caller’s thread. It
never runs under a manager lock, so it may call back into the manager.
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
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
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
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.
pub fn at( &self, path: String, command: String, options: Option<AtCommandOptions>, ) -> Result<AtCommandResult, Error>
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.