Crate bevy_serialport

Source
Expand description

§bevy_serialport

Crates.io Downloads Documentation MIT/Apache 2.0

bevy_serialport is a plugin for add async serial port support for bevy.

§Usage

use bevy::prelude::*;
use bevy_app::ScheduleRunnerPlugin;
use bevy_log::{LogPlugin, info, error};
use std::time::Duration;
use bytes::Bytes;

use bevy_serialport::{
    DataBits, FlowControl, Parity, SerialData, SerialPortPlugin, SerialPortRuntime,
    SerialPortSetting, SerialResource, StopBits,
};

fn main() {
    App::new()
        .add_plugins((
            MinimalPlugins.set(ScheduleRunnerPlugin::run_loop(Duration::from_secs_f64(
                1.0 / 60.0,
            ))),
            LogPlugin::default(),
            SerialPortPlugin,
        ))
        .add_systems(Startup, setup)
        .add_systems(Update, (receive, send_test_data))
        .run();
}

fn setup(mut serial_res: ResMut<SerialResource>, rt: Res<SerialPortRuntime>) {
    // Using builder pattern for cleaner configuration
    let serial_setting = SerialPortSetting::new("COM1", 115_200)
        .with_data_bits(DataBits::Eight)
        .with_flow_control(FlowControl::None)
        .with_parity(Parity::None)
        .with_stop_bits(StopBits::One);
    
    match serial_res.open_with_setting(rt.clone(), serial_setting) {
        Ok(_) => info!("Successfully opened serial port"),
        Err(e) => error!("Failed to open serial port: {}", e),
    }
}

fn receive(mut serial_ev: EventReader<SerialData>) {
    for message in serial_ev.read() {
        // Enhanced API with convenient string conversion
        info!("Received from {}: {}", message.port, message.as_string_lossy());
    }
}

fn send_test_data(mut serial_res: ResMut<SerialResource>) {
    // Better error handling and convenient string method
    if let Err(e) = serial_res.send_string("COM1", "Hello, Serial!") {
        error!("Failed to send message: {}", e);
    }
}

§Supported Versions

bevybevy_serialport
0.160.9
0.150.8
0.140.7
0.130.6
0.120.5
0.110.4
0.100.3
0.90.2
0.80.1

§License

Dual-licensed under either:

At your option. This means that when using this crate in your game, you may choose which license to use.

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dually licensed as above, without any additional terms or conditions.

Re-exports§

pub use utils::*;

Modules§

codec
utils

Structs§

SerialData
Event containing serial port data received
SerialPortPlugin
Serial port plugin for Bevy
SerialPortRuntime
SerialPortSetting
Configuration settings for initializing a serial port
SerialPortWrap
Wrapper for serial port operations with async handling
SerialResource
Serial port resource for managing multiple serial connections

Enums§

DataBits
Number of bits per character
FlowControl
Flow control modes
Parity
Parity checking modes
SerialError
StopBits
Number of stop bits

Type Aliases§

ArcRuntime
RecvQueue