Rust Backend & Desktop API
Relevant source files
The following files were used as context for generating this wiki page:
The Rust backend of the tauri-plugin-serialplugin serves as the core orchestration layer for serial communication in Tauri applications. It provides a unified interface for both desktop (Windows, macOS, Linux) and mobile (Android) platforms, abstracting the low-level details of serial port management, data streaming, and hardware signal control.
The system is designed around a plugin architecture that registers a variety of commands with the Tauri runtime, allowing the frontend to invoke serial operations asynchronously. It manages complex state, including active port connections, background listener threads for continuous data reading, and error propagation from the underlying hardware drivers to the JavaScript environment.
Sources: src/lib.rs:113-167, src/desktop_api.rs:43-48
Core Architecture and Plugin Initialization
The plugin initializes by registering a comprehensive set of commands and setting up global state management. Depending on the target operating system, it instantiates either a desktop-specific or mobile-specific API handler. On desktop, the backend uses a HashMap protected by an Arc<Mutex<...>> to track active SerialportInfo objects, ensuring thread-safe access to hardware resources across multiple command invocations.
The initialization process handles the platform-specific storage of serial port handles. On desktop, this includes an AppHandle and the shared ports map.
Sources: src/lib.rs:163-196, src/desktop_api.rs:43-48
Desktop API Implementation
The desktop implementation relies on the serialport crate to interface with system hardware. It manages the lifecycle of a serial connection through three primary states: Opening, Connected, and Closed.
Port Lifecycle Management
Active ports are managed within a ConnectedPort struct, which contains the actual port trait object, a thread handle for the listener, and a sender channel to signal thread termination.
| Component | Description |
|---|---|
available_ports | Scans the system for hardware, returning details like VID, PID, and Product name. |
open | Configures and opens a port with specific Baud Rate, Data Bits, Parity, and Stop Bits. |
start_listening | Spawns a background thread that continuously polls the port for data. |
finish_serialport_info | Cleans up resources, joins threads, and drops port handles during closure. |
Sources: src/desktop_api.rs:13-30, src/desktop_api.rs:219-247, src/desktop_api.rs:260-350
Data Streaming and Threading
Continuous data reading is achieved through non-blocking background threads. When start_listening is called, the backend clones the serial port handle and enters a loop. This loop aggregates data into a buffer and emits a Tauri event to the frontend whenever the specified timeout is reached or the buffer size is met.
Signal Handling
The system supports both reading and writing hardware control signals, which is critical for flow control and device handshaking.
Mobile (Android) Bridge
On Android, the Rust backend acts as a bridge to the Kotlin-based plugin implementation. It uses run_mobile_plugin to forward requests to the Android hardware abstraction layer.
| Mobile Function | Purpose |
|---|---|
ensure_mobile_success | Validates JSON responses from the Android Kotlin side. |
available_ports | Requests the list of USB devices currently attached to the Android host. |
open | Passes port configuration to the Android SerialPortManager. |
Sources: src/mobile_api.rs:43-70, src/mobile_api.rs:98-124
Data Structures and Configuration
The backend utilizes strongly typed enums to ensure valid configuration across different serialport implementations.
Configuration Types
These types are used in both Desktop and Mobile APIs to define port characteristics.
pub enum DataBits { Five, Six, Seven, Eight }
pub enum FlowControl { None, Software, Hardware }
pub enum Parity { None, Odd, Even }
pub enum StopBits { One, Two }
Sources: src/state.rs
Conclusion
The Rust Backend & Desktop API provides a robust, thread-safe environment for serial communication. By leveraging Rust's concurrency primitives and Tauri's command system, it allows for high-performance data streaming while maintaining a simple, unified API for frontend developers across all supported desktop and mobile platforms.