write_binary

Function write_binary 

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

Writes binary data to a serial port

Sends the specified binary data (as a vector of bytes) 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 binary data to write as a vector of bytes

§Returns

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

§Example

use tauri_plugin_serialplugin::commands::write_binary;
use tauri::{AppHandle, State};
 
#[tauri::command]
async fn send_binary_data(
    app: AppHandle<tauri::Wry>,
    serial: State<'_, tauri_plugin_serialplugin::desktop_api::SerialPort<tauri::Wry>>
) -> Result<(), String> {
    let binary_data = vec![0x48, 0x65, 0x6C, 0x6C, 0x6F]; // "Hello" in ASCII
    let bytes_written = write_binary(app, serial, "COM1".to_string(), binary_data)
        .map_err(|e| e.to_string())?;
    println!("Wrote {} bytes of binary data", bytes_written);
    Ok(())
}

§JavaScript Equivalent

import { SerialPort } from "tauri-plugin-serialplugin-api";;
 
const port = new SerialPort({ path: "COM1" });
await port.open();
const binaryData = new Uint8Array([0x48, 0x65, 0x6C, 0x6C, 0x6F]); // "Hello" in ASCII
const bytesWritten = await port.writeBinary(binaryData);
console.log(`Wrote ${bytesWritten} bytes of binary data`);