Use serialport crate to query serial connection
This commit is contained in:
parent
a22670ad08
commit
b673509380
3 changed files with 42 additions and 28 deletions
|
@ -15,6 +15,6 @@ path = "src/gui.rs"
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
serial = "*"
|
serialport = "*"
|
||||||
gtk = { version = "*", features = ["v3_24"] }
|
gtk = { version = "*", features = ["v3_24"] }
|
||||||
utilities = { git = "http://dimov.cloud:80/hodasemi/Context.git" }
|
utilities = { git = "http://dimov.cloud:80/hodasemi/Context.git" }
|
50
src/port.rs
50
src/port.rs
|
@ -1,39 +1,32 @@
|
||||||
use serial;
|
use serialport;
|
||||||
use serial::prelude::*;
|
use serialport::prelude::*;
|
||||||
pub use serial::PortSettings;
|
|
||||||
|
|
||||||
use utilities::prelude::*;
|
use utilities::prelude::*;
|
||||||
|
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
use std::ffi::OsStr;
|
|
||||||
use std::time::Duration;
|
pub use serialport::{DataBits, FlowControl, Parity, SerialPortSettings, StopBits};
|
||||||
|
pub use std::time::Duration;
|
||||||
|
|
||||||
pub struct Port {
|
pub struct Port {
|
||||||
serial_port: RefCell<Box<dyn SerialPort>>,
|
serial_port: RefCell<Box<dyn SerialPort>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Port {
|
impl Port {
|
||||||
pub fn open<T: AsRef<OsStr> + ?Sized + std::fmt::Display>(
|
pub fn open(settings: SerialPortSettings) -> VerboseResult<Self> {
|
||||||
port: &T,
|
let port_path = Self::find_macroboard()?;
|
||||||
settings: PortSettings,
|
|
||||||
) -> VerboseResult<Self> {
|
let port = serialport::open_with_settings(&port_path, &settings).map_err(|err| {
|
||||||
let mut port = serial::open(port).map_err(|err| {
|
|
||||||
format!(
|
format!(
|
||||||
"could not open serial port ({:?}, {}, {})",
|
"could not open serial port ({:?}, {}, {})",
|
||||||
err.kind(),
|
err.kind(),
|
||||||
err,
|
err,
|
||||||
port
|
port_path
|
||||||
)
|
)
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
port.configure(&settings)
|
|
||||||
.map_err(|err| format!("failed configuring serial port ({})", err))?;
|
|
||||||
|
|
||||||
port.set_timeout(Duration::from_millis(2500))
|
|
||||||
.map_err(|err| format!("failed setting time out for serial port ({})", err))?;
|
|
||||||
|
|
||||||
Ok(Port {
|
Ok(Port {
|
||||||
serial_port: RefCell::new(Box::new(port)),
|
serial_port: RefCell::new(port),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -57,4 +50,25 @@ impl Port {
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn find_macroboard() -> VerboseResult<String> {
|
||||||
|
let available_ports = serialport::available_ports()
|
||||||
|
.map_err(|err| format!("error querying serial ports ( {})", err))?;
|
||||||
|
|
||||||
|
for available_port in available_ports.iter() {
|
||||||
|
if let serialport::SerialPortType::UsbPort(usb_info) = &available_port.port_type {
|
||||||
|
// check for the correct device
|
||||||
|
if let Some(product_name) = &usb_info.product {
|
||||||
|
if product_name == "FT232R_USB_UART"
|
||||||
|
&& usb_info.vid == 1027
|
||||||
|
&& usb_info.pid == 24577
|
||||||
|
{
|
||||||
|
return Ok(available_port.port_name.clone());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
create_error!("macro board not found on usb bus")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,20 +1,20 @@
|
||||||
mod port;
|
mod port;
|
||||||
|
|
||||||
use serial;
|
|
||||||
use utilities::prelude::*;
|
use utilities::prelude::*;
|
||||||
|
|
||||||
use port::{Port, PortSettings};
|
use port::*;
|
||||||
|
|
||||||
fn main() -> VerboseResult<()> {
|
fn main() -> VerboseResult<()> {
|
||||||
let settings = PortSettings {
|
let settings = SerialPortSettings {
|
||||||
baud_rate: serial::Baud9600,
|
baud_rate: 9600,
|
||||||
char_size: serial::Bits8,
|
data_bits: DataBits::Eight,
|
||||||
parity: serial::ParityNone,
|
parity: Parity::None,
|
||||||
stop_bits: serial::Stop1,
|
stop_bits: StopBits::One,
|
||||||
flow_control: serial::FlowNone,
|
flow_control: FlowControl::None,
|
||||||
|
timeout: Duration::from_millis(2500),
|
||||||
};
|
};
|
||||||
|
|
||||||
let port = Port::open("/dev/macroboard/tty00000000", settings)?;
|
let port = Port::open(settings)?;
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
if let Ok(msg) = port.read() {
|
if let Ok(msg) = port.read() {
|
||||||
|
|
Loading…
Reference in a new issue