Module commands

Module commands 

Source
Expand description

Commands module providing Tauri commands for serial port operations

This module contains all the Tauri commands that can be invoked from the frontend to interact with serial ports. Each command is designed to be used with the tauri::invoke function or through the plugin’s JavaScript API.

§Examples

use tauri_plugin_serialplugin::commands;
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> {
    commands::open(app, serial, "COM1".to_string(), 9600, None, None, None, None, None)
        .map_err(|e| e.to_string())
}

Command functions for serial port operations

This module contains all the Tauri command functions that can be used to interact with serial ports. These functions can be imported and used directly in your Rust code.

§Example

use tauri_plugin_serialplugin::commands::{available_ports, open, write, read, close};
use tauri_plugin_serialplugin::state::{DataBits, FlowControl, Parity, StopBits};
use tauri::{AppHandle, State};
 
#[tauri::command]
async fn my_serial_function(
    app: AppHandle<tauri::Wry>,
    serial: State<'_, tauri_plugin_serialplugin::desktop_api::SerialPort<tauri::Wry>>
) -> Result<(), String> {
    // Get available ports
    let ports = available_ports(app.clone(), serial.clone())
        .map_err(|e| e.to_string())?;
     
    // Open a port
    open(app.clone(), serial.clone(), "COM1".to_string(), 9600, None, None, None, None, None)
        .map_err(|e| e.to_string())?;
     
    // Write data
    write(app.clone(), serial.clone(), "COM1".to_string(), "Hello".to_string())
        .map_err(|e| e.to_string())?;
     
    // Read data
    let data = read(app.clone(), serial.clone(), "COM1".to_string(), Some(1000), Some(1024))
        .map_err(|e| e.to_string())?;
     
    // Close port
    close(app, serial, "COM1".to_string())
        .map_err(|e| e.to_string())?;
     
    Ok(())
}

Functions§

available_ports
Lists all available serial ports on the system
available_ports_direct
Lists all available serial ports using platform-specific commands
bytes_to_read
Gets the number of bytes available to read from the serial port
bytes_to_write
Gets the number of bytes available to write to the serial port
cancel_read
Cancels ongoing read operations on a serial port
clear_break
Clears the break condition on the serial port
clear_buffer
Clears the specified buffer of the serial port
close
Closes a serial port
close_all
Closes all open serial ports
force_close
Forcefully closes a serial port
managed_ports
Lists all currently managed serial ports
open
Opens a serial port with the specified configuration
read
Reads string data from a serial port
read_binary
Reads binary data from a serial port
read_carrier_detect
Reads the CD (Carrier Detect) control signal state
read_clear_to_send
Reads the CTS (Clear To Send) control signal state
read_data_set_ready
Reads the DSR (Data Set Ready) control signal state
read_ring_indicator
Reads the RI (Ring Indicator) control signal state
set_baud_rate
Sets the baud rate for a serial port
set_break
Sets the break condition on the serial port
set_data_bits
Sets the number of data bits for a serial port
set_flow_control
Sets the flow control mode for a serial port
set_parity
Sets the parity checking mode for a serial port
set_stop_bits
Sets the number of stop bits for a serial port
set_timeout
Sets the read timeout for a serial port
start_listening
Starts listening for data on a serial port
stop_listening
Stops listening for data on a serial port
write
Writes string data to a serial port
write_binary
Writes binary data to a serial port
write_data_terminal_ready
Sets the DTR (Data Terminal Ready) control signal
write_request_to_send
Sets the RTS (Request To Send) control signal