Serial Port Configuration
Relevant source files
The following files were used as context for generating this wiki page:
Serial Port Configuration in the tauri-plugin-serialplugin project defines the communication parameters required to establish and maintain a stable connection between the host application and a serial hardware device. This configuration covers hardware-level signals (baud rate, parity, stop bits) and software-level controls (flow control, timeouts).
The system architecture allows for both static configuration during the initial port opening and dynamic updates while the port is active. Configuration is handled through a unified TypeScript interface in the frontend, which is then serialized and passed to platform-specific drivers in Rust (for Desktop) or Kotlin (for Android).
Sources: guest-js/index.ts, src/commands.rs, android/src/main/kotlin/app/tauri/serialplugin/SerialPlugin.kt
Configuration Data Models
The configuration is structured around a central set of parameters that define the physical and logical behavior of the connection.
Core Parameters
The primary configuration object is defined as SerialportOptions in the JavaScript API and SerialPortConfig in the Android native implementation.
| Parameter | Type | Default (Android) | Description |
|---|---|---|---|
path | string | Required | The system path to the device (e.g., COM1, /dev/ttyUSB0). |
baudRate | number | 9600 | The speed of data transmission in bits per second. |
dataBits | DataBits | Eight | Number of data bits per character (Five, Six, Seven, or Eight). |
flowControl | FlowControl | None | The flow control mode (None, Software, or Hardware). |
parity | Parity | None | Error detection mode (None, Odd, or Even). |
stopBits | StopBits | One | Number of stop bits (One or Two). |
timeout | number | 1000 | Read/Write timeout duration in milliseconds. |
Sources: guest-js/index.ts:31-40, android/src/main/kotlin/app/tauri/serialplugin/models/SerialPortConfig.kt:10-18
Configuration Flow Architecture
The following diagram illustrates how a configuration request moves from the user interface to the native hardware drivers.
This diagram shows the multi-layered approach to configuration, ensuring that the same TypeScript interface works across different operating systems.
Sources: src/commands.rs, android/src/main/kotlin/app/tauri/serialplugin/SerialPlugin.kt, src/desktop_api.rs
Hardware Communication Constants
The plugin utilizes specific enumerations to restrict configuration to valid hardware states. These enums are mapped between TypeScript, Rust, and Kotlin.
Data Bits and Stop Bits
- DataBits: Supported values are
5,6,7, and8. Android implementation supports both numeric and string-based initialization. - StopBits: Supported values are
1and2.
Flow Control and Parity
- FlowControl: Options include
None,Software(XON/XOFF), andHardware(RTS/CTS). - Parity: Options include
None,Odd, andEven.
Sources: guest-js/index.ts:1-29, android/src/main/kotlin/app/tauri/serialplugin/models/SerialPortConfig.kt:20-50, debug.md:75-85
Platform-Specific Implementation
Android Type Flexibility
On Android, the SerialPlugin provides enhanced flexibility for configuration parameters. The native PortConfigArgs class accepts Any? types for enums, allowing the frontend to pass either string labels (e.g., "EIGHT") or numeric values (e.g., 8).
// Example of flexible parameter handling in Android
val dataBits = when (args.dataBits) {
is String -> DataBits.valueOf(args.dataBits as String)
is Number -> DataBits.fromValue((args.dataBits as Number).toInt())
null -> DataBits.EIGHT
else -> throw IllegalArgumentException("Invalid data bits type")
}
Sources: android/src/main/kotlin/app/tauri/serialplugin/SerialPlugin.kt:165-171, debug.md:41-51
Desktop Port Management
On Desktop platforms (Windows, Linux, macOS), configuration is applied via the serialport-rs library. The desktop_api.rs module manages an internal HashMap of SerialportInfo objects, which track whether a port is Opening, Connected, or Closed. When a port is opened with a specific configuration, the system ensures any existing connection to that path is force-closed first to prevent resource conflicts.
Sources: src/desktop_api.rs:215-230, src/desktop_api.rs:240-250
Dynamic Configuration Updates
Configuration is not restricted to the initial connection. The plugin allows runtime modification of parameters through specific setter commands.
Sequence of Dynamic Configuration
The following sequence diagram shows the process of updating the baud rate on an active connection.
Available Setter Commands
Individual parameters can be adjusted using the following API methods:
setBaudRate(baudRate: number)setDataBits(dataBits: DataBits)setFlowControl(flowControl: FlowControl)setParity(parity: Parity)setStopBits(stopBits: StopBits)setTimeout(timeout: number)
Sources: guest-js/index.ts:250-300, src/commands.rs:180-250
Summary
Serial Port Configuration provides the necessary interface to bridge high-level application logic with low-level serial communication. By standardizing options like baud rate and flow control across Desktop and Android while maintaining platform-specific flexibility (such as Android's multi-type enum support), the plugin ensures consistent device interaction regardless of the underlying hardware or operating system.