pub fn read<R: Runtime>(
_app: AppHandle<R>,
serial: State<'_, SerialPort<R>>,
path: String,
timeout: Option<u64>,
size: Option<usize>,
) -> Result<String, Error>
Expand description
Reads string data from a serial port
Reads data from the serial port and returns it as a string. The port must be open before reading data.
§Arguments
_app
- The Tauri app handleserial
- The serial port statepath
- 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 string data read from the port, or an Error
if the operation failed.
§Example
use tauri_plugin_serialplugin::commands::read;
use tauri::{AppHandle, State};
#[tauri::command]
async fn receive_data(
app: AppHandle<tauri::Wry>,
serial: State<'_, tauri_plugin_serialplugin::desktop_api::SerialPort<tauri::Wry>>
) -> Result<(), String> {
let data = read(app, serial, "COM1".to_string(), Some(1000), Some(1024))
.map_err(|e| e.to_string())?;
println!("Received: {}", data);
Ok(())
}
§JavaScript Equivalent
import { SerialPort } from "tauri-plugin-serialplugin-api";;
const port = new SerialPort({ path: "COM1" });
await port.open();
const data = await port.read({ timeout: 1000, size: 1024 });
console.log("Received:", data);