Initial commit

This commit is contained in:
hodasemi 2019-11-22 11:24:12 +01:00
commit fa0ba18d12
3 changed files with 70 additions and 0 deletions

4
.gitignore vendored Normal file
View file

@ -0,0 +1,4 @@
/target
**/*.rs.bk
Cargo.lock

11
Cargo.toml Normal file
View file

@ -0,0 +1,11 @@
[package]
name = "macroboard"
version = "0.1.0"
authors = ["hodasemi <michaelh.95@t-online.de>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
serial = "*"
utilities = { git = "http://dimov.cloud:80/hodasemi/Context.git" }

55
src/main.rs Normal file
View file

@ -0,0 +1,55 @@
use serial;
use serial::prelude::*;
use utilities::prelude::*;
use std::ffi::OsStr;
use std::time::Duration;
fn main() -> VerboseResult<()> {
let mut port = open("/dev/ttyACM0")?;
write(&mut port, "hello world")?;
let msg = read(&mut port)?;
println!("{}", msg);
Ok(())
}
fn open<T: AsRef<OsStr> + ?Sized>(port: &T) -> VerboseResult<impl SerialPort> {
let mut port = serial::open(port).map_err(|_| "could not open serial port")?;
port.reconfigure(&|port_settings| {
port_settings.set_baud_rate(serial::Baud9600)?;
port_settings.set_char_size(serial::Bits8);
port_settings.set_parity(serial::ParityNone);
port_settings.set_stop_bits(serial::Stop1);
port_settings.set_flow_control(serial::FlowNone);
Ok(())
})
.map_err(|_| "failed configuring serial port")?;
port.set_timeout(Duration::from_millis(1000))
.map_err(|_| "failed setting time out for serial port")?;
Ok(port)
}
fn read<T: SerialPort>(port: &mut T) -> VerboseResult<String> {
let mut buf: Vec<u8> = (0..255).collect();
port.read(&mut buf[..])
.map_err(|_| "failed reading serial port")?;
Ok(String::from_utf8(buf).map_err(|_| "failed converting utf8 buffer")?)
}
fn write<T: SerialPort>(port: &mut T, msg: &str) -> VerboseResult<()> {
port.write(msg.as_bytes())
.map_err(|_| "failed writing to serial port")?;
Ok(())
}