write

Function write 

Source
pub fn write<R: Runtime>(
    _app: AppHandle<R>,
    serial: State<'_, SerialPort<R>>,
    path: String,
    value: String,
) -> Result<usize, Error>
Expand description

Writes string data to a serial port

Sends the specified string data to the serial port. The port must be open before writing data.

§Arguments

  • _app - The Tauri app handle
  • serial - The serial port state
  • path - The path to the serial port (e.g., “COM1”, “/dev/ttyUSB0”)
  • value - The string data to write to the port

§Returns

The number of bytes written, or an Error if the operation failed.

§Example

use tauri_plugin_serialplugin::commands::write;
use tauri::{AppHandle, State};
 
#[tauri::command]
async fn send_data(
    app: AppHandle<tauri::Wry>,
    serial: State<'_, tauri_plugin_serialplugin::desktop_api::SerialPort<tauri::Wry>>
) -> Result<(), String> {
    let bytes_written = write(app, serial, "COM1".to_string(), "Hello World".to_string())
        .map_err(|e| e.to_string())?;
    println!("Wrote {} bytes", bytes_written);
    Ok(())
}

§JavaScript Equivalent

import { SerialPort } from "tauri-plugin-serialplugin-api";;
 
const port = new SerialPort({ path: "COM1" });
await port.open();
const bytesWritten = await port.write("Hello World");
console.log(`Wrote ${bytesWritten} bytes`);