Installation & Setup
Relevant source files
The following files were used as context for generating this wiki page:
The tauri-plugin-serialplugin provides a unified interface for serial port communication across desktop (Windows, Linux, macOS) and mobile (Android) platforms within a Tauri application. The installation process involves configuring both the Rust backend and the JavaScript/TypeScript frontend, along with platform-specific permissions and build requirements.
Sources: README.md, src/lib.rs:1-15, Cargo.toml:15-20
Backend Configuration
To integrate the plugin into a Tauri project, the Rust crate must be added as a dependency and initialized within the Tauri builder.
Dependency Specification
Add the plugin to your src-tauri/Cargo.toml file. The plugin depends on the serialport crate version 4.8.1 and the Tauri framework version 2.6.2 or higher.
[dependencies]
tauri-plugin-serialplugin = "2.22.0"
Sources: Cargo.toml:1-15, src/lib.rs:155-165
Plugin Initialization
The plugin must be registered in the main.rs or lib.rs file of your Tauri application. The init() function configures the necessary state management and command handlers.
fn main() {
tauri::Builder::default()
.plugin(tauri_plugin_serialplugin::init())
.run(tauri::generate_context!())
.expect("error while running tauri application");
}
Sources: src/lib.rs:155-170
Build Script Configuration
The build.rs file manages the compilation of the plugin, specifically handling Android-specific pathing and manifest updates for USB device detection.
The build script automatically injects <intent-filter> actions for USB_DEVICE_ATTACHED into the Android manifest to ensure the app responds when hardware is connected.
Sources: build.rs:45-70
Frontend Setup
The frontend interaction is managed via the @tauri-apps/api and the plugin's dedicated JavaScript API package.
Installation
Install the API package using your preferred package manager:
npm install tauri-plugin-serialplugin-api
# or
pnpm add tauri-plugin-serialplugin-api
Sources: package.json:1-15, examples/serialport-test/package.json:10-15
Usage Flow
The frontend communicates with the Rust backend through the SerialPort class, which handles the IPC (Inter-Process Communication) calls.
Sources: src/lib.rs:175-210, debug.md:105-115
Platform Specifics: Android
Android setup requires additional steps due to USB Host API restrictions and permission requirements.
Prerequisites
| Component | Requirement |
|---|---|
| Android Studio | Latest Version |
| Android SDK | API 24+ |
| Kotlin | 1.8+ |
| Gradle | 7.0+ |
| JDK | 17+ |
Sources: android/BUILD_INSTRUCTIONS.md:5-15, android/README.md:30-35
Build and Debugging
To build the Android module, navigate to the android directory and use the Gradle wrapper.
cd android
./gradlew assembleRelease
For debugging, use adb logcat with filters for the plugin identifiers:
adb logcat -v time | grep -E "SerialPort|Tauri|tauri-plugin-serialplugin"
Sources: android/BUILD_INSTRUCTIONS.md:20-25, debug.md:65-75
USB Permissions
Android requires explicit user permission to access USB devices. The plugin uses device_filter.xml to identify compatible hardware. The build.rs script ensures the following is present in the AndroidManifest.xml:
<intent-filter>
<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
</intent-filter>
<meta-data
android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
android:resource="@xml/device_filter" />
Sources: build.rs:60-70, android/README.md:10-15
Permissions & Capabilities
Tauri v2 requires explicit permission scoping for plugin commands. These are defined in the permissions folder.
| Permission | Description |
|---|---|
serialplugin:allow-available-ports | Allows listing system serial ports. |
serialplugin:allow-open | Allows opening a serial connection. |
serialplugin:allow-read | Allows receiving data from a port. |
serialplugin:allow-write | Allows sending data to a port. |
serialplugin:default | A set containing all standard read/write permissions. |
Sources: permissions/schemas/schema.json, examples/serialport-test/src-tauri/gen/schemas/desktop-schema.json
Conclusion
Setting up the tauri-plugin-serialplugin requires a coordinated configuration of the Rust backend, Android manifest (for mobile), and the JS/TS API. Once installed, the plugin provides a cross-platform SerialPort class that abstracts the underlying OS-specific serial drivers into a consistent interface.