Core Capabilities
Relevant source files
The following files were used as context for generating this wiki page:
The tauri-plugin-serialplugin provides a cross-platform interface for serial port communication within Tauri applications. It bridges the gap between high-level frontend code (JavaScript/TypeScript) and low-level system serial ports on Desktop (Windows, macOS, Linux) and Mobile (Android) platforms. The plugin handles port discovery, configuration management, and bidirectional data transmission.
Sources: src/lib.rs:1-15, debug.md:30-50
Port Discovery and Management
The plugin allows applications to identify available hardware serial ports and manage their lifecycle. This includes scanning for active ports, listing ports managed by the plugin, and handling emergency closures.
Discovery Mechanisms
- available_ports: Returns a list of available serial ports detected by the operating system.
- available_ports_direct: A direct method for port retrieval, often used for hardware-specific identification.
- managed_ports: Returns a list of ports currently opened and managed by the plugin's internal state.
Sources: src/lib.rs:145-155, examples/serialport-test/src/App.svelte:15-45
Lifecycle Flow
The following diagram illustrates the typical lifecycle of a serial port connection managed by the plugin:
The application lifecycle begins with port discovery and configuration before moving into an active state where data is exchanged.
Sources: examples/serialport-test/src/components/SerialPortComponent.svelte:45-80, tests/README.md:25-40
Port Configuration and Control
The plugin provides granular control over serial communication parameters. These settings can be defined during port opening or modified dynamically during an active session.
Configuration Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
baudRate | Integer | 9600 | Communication speed in bits per second. |
dataBits | Enum | Eight | Number of bits per character (5, 6, 7, 8). |
flowControl | Enum | None | Handshaking protocol (None, Software, Hardware). |
parity | Enum | None | Error-detecting code (None, Odd, Even). |
stopBits | Enum | One | Number of bits at the end of a character (One, Two). |
timeout | Integer | 1000 | Read/write timeout in milliseconds. |
Sources: debug.md:130-145, examples/serialport-test/src/components/SerialPortComponent.svelte:15-35
Signal Control
Beyond data transmission, the plugin can manipulate and read hardware control signals (modem lines).
- Output Signals:
setRequestToSend(RTS),setDataTerminalReady(DTR). - Input Signals:
readClearToSend(CTS),readDataSetReady(DSR),readRingIndicator(RI),readCarrierDetect(CD).
Sources: src/lib.rs:165-175, examples/serialport-test/src/components/SerialPortComponent.svelte:160-185
Data Transmission and Streaming
The plugin supports both synchronous read/write operations and asynchronous event-driven data streaming.
Communication Flow
The plugin coordinates between the frontend's requests and the hardware's physical data lines, supporting both binary and string formats.
Sources: src/lib.rs:155-165, examples/serialport-test/src/components/SerialPortComponent.svelte:95-130
Key Data Methods
- write / write_binary: Sends data to the serial port. Supports UTF-8 strings or raw
Uint8Array. - read / read_binary: Synchronously retrieves data from the port buffer.
- start_listening: Initiates a background thread that monitors for incoming data and emits events to the frontend.
- clear_buffer: Flushes the input or output buffers.
Sources: src/lib.rs:150-170, tests/README.md:35-50
Security and Permissions
The plugin utilizes Tauri's Access Control List (ACL) system to restrict access to sensitive serial port operations. Permissions are defined in JSON schemas and can be assigned to specific application windows.
Permission Categories
- Read Operations:
allow-read,allow-read-binary,allow-start-listening. - Write Operations:
allow-write,allow-write-binary. - Management:
allow-open,allow-close,allow-available-ports. - Hardware Config:
allow-set-baud-rate,allow-set-break,allow-set-flow-control.
Sources: permissions/schemas/schema.json:150-250, build.rs:10-40
Default Permission Set
The "default" permission set includes a comprehensive list of commands required for basic serial communication, while denying access to critical Tauri components outside the serial context.
Sources: permissions/schemas/schema.json:550-600
Platform-Specific Implementation
The plugin maintains architectural consistency across platforms while using platform-specific backends for low-level access.
- Desktop: Uses the
serialportRust crate to interact with system tty/COM devices via a global state managed inHashMap. - Android: Utilizes a Kotlin-based manager (
SerialPortManager) via the Tauri mobile API. It handles USB device attachment events and permission requests.
Sources: src/lib.rs:80-110, debug.md:55-75, build.rs:45-55
The tauri-plugin-serialplugin provides a robust foundation for building industrial control, IoT, and hardware debugging applications. By abstracting platform-specific USB and Serial APIs into a unified Rust/JavaScript interface, it enables developers to focus on application logic rather than low-level driver management.