open

Function open 

Source
pub fn open<R: Runtime>(
    _app: AppHandle<R>,
    serial: State<'_, SerialPort<R>>,
    path: String,
    baud_rate: u32,
    data_bits: Option<DataBits>,
    flow_control: Option<FlowControl>,
    parity: Option<Parity>,
    stop_bits: Option<StopBits>,
    timeout: Option<u64>,
) -> Result<(), Error>
Expand description

Opens a serial port with the specified configuration

Opens a serial port and configures it with the given parameters. The port must be closed before it can be opened again.

§Arguments

  • _app - The Tauri app handle
  • serial - The serial port state
  • path - The path to the serial port (e.g., “COM1”, “/dev/ttyUSB0”)
  • baud_rate - The baud rate for communication (e.g., 9600, 115200)
  • data_bits - Number of data bits per character (5, 6, 7, or 8)
  • flow_control - Flow control mode (None, Software, or Hardware)
  • parity - Parity checking mode (None, Odd, or Even)
  • stop_bits - Number of stop bits (One or Two)
  • timeout - Read timeout in milliseconds

§Returns

Ok(()) if the port was opened successfully, or an Error if it failed.

§Example

use tauri_plugin_serialplugin::commands::open;
use tauri_plugin_serialplugin::state::{DataBits, FlowControl, Parity, StopBits};
use tauri::{AppHandle, State};
 
#[tauri::command]
async fn open_serial_port(
    app: AppHandle<tauri::Wry>,
    serial: State<'_, tauri_plugin_serialplugin::desktop_api::SerialPort<tauri::Wry>>
) -> Result<(), String> {
    open(
        app,
        serial,
        "COM1".to_string(),
        9600,
        Some(DataBits::Eight),
        Some(FlowControl::None),
        Some(Parity::None),
        Some(StopBits::One),
        Some(1000)
    ).map_err(|e| e.to_string())
}

§JavaScript Equivalent

import { SerialPort } from "tauri-plugin-serialplugin-api";;
 
const port = new SerialPort({
  path: "COM1",
  baudRate: 9600,
  dataBits: 8,
  flowControl: 0, // None
  parity: 0,      // None
  stopBits: 1
});
await port.open();