bevy_serial
is a plugin to add non-blocking serial communication to bevy. This plugin is based on mio-serial
that can realize non-blocking high performance I/O.
Reading and writing from/to serial port is realized via bevy's event system. Each serial port is handled via port name or a unique label you choose.
Here is a simple example:
```rust use bevy::prelude::*; use bevy_serial::{SerialPlugin, SerialReadEvent, SerialWriteEvent};
// to write data to serial port periodically struct SerialWriteTimer(Timer);
fn main() {
App::build()
.addplugins(MinimalPlugins)
// simply specify port name and baud rate for SerialPlugin
.addplugin(SerialPlugin::new("COM5", 115200))
// to write data to serial port periodically (every 1 second)
.insertresource(SerialWriteTimer(Timer::fromseconds(1.0, true)))
// reading and writing from/to serial port is achieved via bevy's event system
.addsystem(readserial.system())
.addsystem(writeserial.system())
.run();
}
// reading event for serial port
fn readserial(mut evserial: EventReaderSerialReadEvent
for SerialReadEvent(label, buffer) in evserial.iter() {
let s = String::fromutf8(buffer.clone()).unwrap();
println!("received packet from {}: {}", label, s);
}
}
// writing event for serial port
fn writeserial(
mut evserial: EventWriter
You can add multiple serial ports with additional settings.
```rust use bevy::prelude::*; use bevy_serial::{ DataBits, FlowControl, Parity, SerialPlugin, SerialReadEvent, SerialSetting, SerialWriteEvent, StopBits, }; use std::time::Duration;
// to write data to serial port periodically struct SerialWriteTimer(Timer);
fn main() { App::build() .addplugins(MinimalPlugins) // you can specify various configurations for multiple serial ports by this way .addplugin(SerialPlugin { settings: vec![SerialSetting { label: Some("myserial".tostring()), portname: "COM5".tostring(), baudrate: 115200, databits: DataBits::Eight, flowcontrol: FlowControl::None, parity: Parity::None, stopbits: StopBits::One, timeout: Duration::frommillis(0), }], }) // to write data to serial port periodically (every 1 second) .insertresource(SerialWriteTimer(Timer::fromseconds(1.0, true))) // reading and writing from/to serial port is achieved via bevy's event system .addsystem(readserial.system()) .addsystem(write_serial.system()) .run(); }
// reading event for serial port
fn readserial(mut evserial: EventReaderSerialReadEvent
for SerialReadEvent(label, buffer) in evserial.iter() {
let s = String::fromutf8(buffer.clone()).unwrap();
println!("read packet from {}: {}", label, s);
}
}
// writing event for serial port
fn writeserial(
mut evserial: EventWriter
MIT