Example Application
Relevant source files
The following files were used as context for generating this wiki page:
The Example Application (serialport-test) is a multi-port serial communication demo built using Tauri, Svelte, and TypeScript. Its primary purpose is to showcase the capabilities of the tauri-plugin-serialplugin, providing a functional interface for port discovery, connection management, and data transmission.
Sources: examples/serialport-test/README.md, examples/serialport-test/src/App.svelte:1-10
The application allows users to scan for available serial ports on the host system (desktop or mobile), establish multiple concurrent connections, and interact with individual ports through dedicated control interfaces. It serves as both a testing tool for the plugin and a reference implementation for developers integrating serial port functionality into Tauri applications.
Sources: examples/serialport-test/src/App.svelte:100-150, examples/serialport-test/src/components/SerialPortComponent.svelte:1-20
Architecture and Component Structure
The application follows a modular architecture where the main entry point manages port discovery and the list of active connections, while individual port logic is encapsulated within a reusable Svelte component.
Component Hierarchy
The diagram above illustrates the relationship between the main application container and the individual port components.
Sources: examples/serialport-test/src/App.svelte:80-95, examples/serialport-test/src/components/SerialPortComponent.svelte:1-10
Application Components
| Component | Responsibility | Key Functions |
|---|---|---|
App.svelte | Root component managing global state and port discovery. | scanPorts(), addPort(), handleDisconnect() |
SerialPortComponent.svelte | Individual controller for a single serial port connection. | connect(), sendMessage(), updatePortSettings() |
main.ts | Mounts the Svelte application to the DOM. | mount() |
Sources: examples/serialport-test/src/App.svelte:15-60, examples/serialport-test/src/components/SerialPortComponent.svelte:40-80, examples/serialport-test/src/main.ts
Port Discovery Logic
The application provides three distinct methods for identifying serial ports on the system, accessible through the SerialPort static methods provided by the plugin API.
Discovery Methods
- Available Ports: Scans the system for standard serial devices.
- Direct Ports: Performs a direct scan, often used for specific hardware discovery.
- Managed Ports: Lists ports currently managed by the plugin state.
The sequence diagram shows the asynchronous flow of retrieving system ports into the frontend state.
Sources: examples/serialport-test/src/App.svelte:18-45
Connection Management
When a user selects a port from the discovery list, the application adds it to the activePorts array, which triggers the instantiation of a SerialPortComponent.
Connection Lifecycle
This flowchart details the state transitions of a serial port within the application.
Sources: examples/serialport-test/src/components/SerialPortComponent.svelte:45-75, examples/serialport-test/src/App.svelte:63-75
Port Configuration and Control
Each SerialPortComponent provides a comprehensive interface for configuring hardware parameters and managing control signals.
Configurable Parameters
| Parameter | Type | Default | Options |
|---|---|---|---|
| Baud Rate | Number | 9600 | User-defined numeric input |
| Data Bits | Enum | Eight | Five, Six, Seven, Eight |
| Flow Control | Enum | None | None, Software, Hardware |
| Parity | Enum | None | None, Odd, Even |
| Stop Bits | Enum | One | One, Two |
| Timeout | Number | 1000 | Milliseconds |
Sources: examples/serialport-test/src/components/SerialPortComponent.svelte:15-35
Control Signal Indicators
The application monitors and displays the state of standard RS-232 control lines:
- Outputs: RTS (Request to Send), DTR (Data Terminal Ready) - Toggled via buttons.
- Inputs: CTS (Clear to Send), DSR (Data Set Ready), RI (Ring Indicator), CD (Carrier Detect) - Read-only indicators updated during port status refreshes.
Sources: examples/serialport-test/src/components/SerialPortComponent.svelte:125-150, examples/serialport-test/src/components/SerialPortComponent.svelte:255-280
Data Transmission
The application supports both text-based and binary data transmission.
Incoming Data Handling
The application uses the listen method to handle asynchronous data batches emitted by the plugin. Incoming data is appended to a local buffer and displayed in a scrollable interface.
async function connect() {
// ... setup serialport ...
await serialport.startListening();
serialport.listen((data) => {
console.log(`[${portName}] incoming data:`, data);
receivedData += data;
updatePortStatus();
});
}
Sources: examples/serialport-test/src/components/SerialPortComponent.svelte:58-65
Outgoing Data
Users can send arbitrary strings or a predefined binary array ([1, 2, 3, 4, 5]) to the connected device.
Sources: examples/serialport-test/src/components/SerialPortComponent.svelte:85-105
Summary
The Example Application serves as a comprehensive demonstration of the tauri-plugin-serialplugin. By segregating global port management in App.svelte and specific port interaction in SerialPortComponent.svelte, it provides a robust framework for multi-device serial communication. It handles the full lifecycle of a serial connection—from discovery and parameter configuration to real-time data streaming and event-driven disconnection.
Sources: examples/serialport-test/src/App.svelte, examples/serialport-test/src/components/SerialPortComponent.svelte