read_binary

Function read_binary 

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

Reads binary data from a serial port

Reads binary data from the serial port and returns it as a vector of bytes. The port must be open before reading data.

§Arguments

  • _app - The Tauri app handle
  • serial - The serial port state
  • path - The path to the serial port (e.g., “COM1”, “/dev/ttyUSB0”)
  • timeout - Read timeout in milliseconds (None for no timeout)
  • size - Maximum number of bytes to read (None for unlimited)

§Returns

The binary data read from the port as a vector of bytes, or an Error if the operation failed.

§Example

use tauri_plugin_serialplugin::commands::read_binary;
use tauri::{AppHandle, State};
 
#[tauri::command]
async fn receive_binary_data(
    app: AppHandle<tauri::Wry>,
    serial: State<'_, tauri_plugin_serialplugin::desktop_api::SerialPort<tauri::Wry>>
) -> Result<(), String> {
    let data = read_binary(app, serial, "COM1".to_string(), Some(1000), Some(256))
        .map_err(|e| e.to_string())?;
    println!("Received {} bytes: {:?}", data.len(), data);
    Ok(())
}

§JavaScript Equivalent

import { SerialPort } from "tauri-plugin-serialplugin-api";;
 
const port = new SerialPort({ path: "COM1" });
await port.open();
const data = await port.readBinary({ timeout: 1000, size: 256 });
console.log(`Received ${data.length} bytes:`, data);