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::{app::ScheduleRunnerPlugin, log::LogPlugin};
use std::time::Duration;

use bevy::prelude::*;
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>) {
    let serial_setting = SerialPortSetting {
        port_name: "COM1".to_string(),
        baud_rate: 115_200,
        data_bits: DataBits::Five,
        flow_control: FlowControl::None,
        parity: Parity::None,
        stop_bits: StopBits::One,
        timeout: Default::default(),
    };
    serial_res
        .open_with_setting(rt.clone(), serial_setting)
        .expect("open serial port error");
}

fn receive(mut serial_ev: EventReader<SerialData>) {
    for message in serial_ev.read() {
        info!("receive {:?}", message);
    }
}

fn send_test_data(mut serial_res: ResMut<SerialResource>) {
    serial_res.send_message("COM1", Bytes::from(&b"123457"[..]))
}

§Supported Versions

bevybevy_serialport
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

  • MIT
  • Apache 2.0

Modules§

Structs§

Enums§

Type Aliases§