Hardware Control Signals
Relevant source files
The following files were used as context for generating this wiki page:
Hardware control signals in the tauri-plugin-serialplugin project provide the programmatic interface for interacting with low-level RS-232/Serial modem control lines. These signals allow developers to monitor and manipulate specific hardware pins on a serial port beyond standard data transmission, facilitating hardware flow control and device state synchronization.
The plugin implements a bidirectional communication layer where the frontend (JavaScript/TypeScript) can request signal states or set output levels, which the Rust backend then executes using platform-specific APIs via the serialport crate.
Architecture and Data Flow
The control signal system follows the standard Tauri plugin architecture, bridging the guest JavaScript environment with the Rust host.
API Flow
When a frontend component requests a signal state (e.g., RI or CTS), the call travels through the SerialPort class in the guest JS, invokes a Tauri command, and executes the corresponding method in the Rust desktop or mobile API.
Sources: guest-js/index.ts, src/commands.rs, src/desktop_api.rs
Signal Classification
The hardware signals are divided into two primary categories: Output Signals (Writeable) and Input Signals (Readable).
Output Signals (Writeable)
These signals are driven by the application to control the state of the connected hardware device.
| Signal | Function | Rust Method | JS Method |
|---|---|---|---|
| RTS | Request To Send | write_request_to_send | setRequestToSend(level) |
| DTR | Data Terminal Ready | write_data_terminal_ready | setDataTerminalReady(level) |
Sources: guest-js/index.ts:540-558, src/desktop_api.rs:556-568
Input Signals (Readable)
These signals are generated by the external hardware and monitored by the application.
| Signal | Function | Rust Method | JS Method |
|---|---|---|---|
| CTS | Clear To Send | read_clear_to_send | readClearToSend() |
| DSR | Data Set Ready | read_data_set_ready | readDataSetReady() |
| RI | Ring Indicator | read_ring_indicator | readRingIndicator() |
| CD | Carrier Detect | read_carrier_detect | readCarrierDetect() |
Sources: guest-js/index.ts:560-614, src/desktop_api.rs:570-598
Implementation Details
Rust Command Layer
The commands in src/commands.rs act as thin wrappers that extract the serial port state from Tauri's managed state and delegate the actual hardware interaction to the platform API.
#[tauri::command]
pub fn read_ring_indicator<R: Runtime>(
_app: AppHandle<R>,
serial: State<'_, SerialPort<R>>,
path: String,
) -> Result<bool, Error> {
serial.read_ring_indicator(path)
}
Sources: src/commands.rs:385-391
Desktop API Integration
In src/desktop_api.rs, the plugin utilizes the serialport crate's traits. Most signal methods use a helper function get_serialport which ensures the port is in a Connected state before attempting to read or write pin levels.
pub fn read_carrier_detect(&self, path: String) -> Result<bool, Error> {
self.get_serialport(path, |port| {
port.read_carrier_detect().map_err(Error::from)
})
}
Sources: src/desktop_api.rs:594-598, src/desktop_api.rs:630-635
Break Signals
In addition to standard modem lines, the plugin supports "Break" signals, which pull the transmission line to a logical '0' state for a period longer than a character frame.
set_break(path): Commences a break condition.clear_break(path): Terminates a break condition.
Sources: src/desktop_api.rs:615-623, guest-js/index.ts:524-538
Usage in Components
Frontend components can react to these signals by polling or triggering updates. The example application demonstrates toggling output signals and displaying indicator statuses for input signals.
// Example: Toggling RTS in a Svelte component
async function toggleRTS() {
if (!serialport) return;
try {
rtsState = !rtsState;
await serialport.setRequestToSend(rtsState);
} catch (err) {
console.error(`Failed to toggle RTS:`, err);
}
}
Sources: examples/serialport-test/src/components/SerialPortComponent.svelte:185-193
Summary
Hardware control signals in tauri-plugin-serialplugin allow for granular management of RS-232 control lines. By providing dedicated methods for RTS, DTR, CTS, DSR, RI, and CD, the plugin enables complex hardware handshaking and device state monitoring essential for industrial and legacy hardware communication.