update status bar com serial port information
This commit is contained in:
parent
871fe65187
commit
cc56f48c0a
29
src/main.rs
29
src/main.rs
@ -47,6 +47,7 @@ fn build_ui(application: >k::Application) {
|
||||
let ui_databit: Label = builder.object("ui_databit").unwrap();
|
||||
let ui_parity: Label = builder.object("ui_parity").unwrap();
|
||||
let ui_stopbit: Label = builder.object("ui_stopbit").unwrap();
|
||||
let ui_com: Label = builder.object("ui_com").unwrap();
|
||||
let ui_statusbar: Statusbar = builder.object("statusbar").unwrap();
|
||||
let ui_log_view: TextView = builder.object("log_view").unwrap();
|
||||
|
||||
@ -83,6 +84,7 @@ fn build_ui(application: >k::Application) {
|
||||
Message::UiUpdateStatusBarDatabit(value) => ui_databit.set_text(value.to_string().as_str()),
|
||||
Message::UiUpdateStatusBarParity(value) => ui_parity.set_text(value.to_string().as_str()),
|
||||
Message::UiUpdateStatusBarStopbit(value) => ui_stopbit.set_text(value.to_string().as_str()),
|
||||
Message::UiUpdateStatusBarCom(value) => ui_com.set_text(value.to_string().as_str()),
|
||||
_ => {}
|
||||
}
|
||||
glib::Continue(true)
|
||||
@ -119,7 +121,8 @@ fn process_cmd(command: String, to_ui: glib::Sender<Message>, gui2main_tx: Sende
|
||||
}
|
||||
"open" => {
|
||||
if command_split.len() == 1 {
|
||||
to_ui.send(Message::UiUpdateLog("No enough argument\n".to_string())).unwrap();
|
||||
// to_ui.send(Message::UiUpdateLog("No enough argument\n".to_string())).unwrap();
|
||||
gui2main_tx.send(Message::MainCmdOpen("".to_string())).unwrap();
|
||||
} else {
|
||||
let args: Vec<String> = command_split[1..].to_vec().into_iter().map(|item| String::from(item)).collect();
|
||||
gui2main_tx.send(Message::MainCmdOpen(args[0].clone())).unwrap();
|
||||
@ -129,7 +132,7 @@ fn process_cmd(command: String, to_ui: glib::Sender<Message>, gui2main_tx: Sende
|
||||
gui2main_tx.send(Message::MainCmdClose).unwrap();
|
||||
}
|
||||
"list" => {
|
||||
to_ui.send(Message::UiUpdateLog(list_available_ports())).unwrap();
|
||||
to_ui.send(Message::UiUpdateLog(list_available_ports().0)).unwrap();
|
||||
}
|
||||
"send" => {
|
||||
gui2main_tx.send(Message::MainCmdSend(command[5..].parse().unwrap())).unwrap();
|
||||
@ -165,6 +168,7 @@ fn process_cmd(command: String, to_ui: glib::Sender<Message>, gui2main_tx: Sende
|
||||
|
||||
fn main_proc(to_main_tx: Sender<Message>, rx: Receiver<Message>, ui_upd: glib::Sender<Message>) {
|
||||
let (mut main2port_tx, _rx_port_receive) = channel::<Message>();
|
||||
let mut default_com_port = "USB0".to_string();
|
||||
let mut port_default_config = PortConfig {
|
||||
baud_rate: 115_200,
|
||||
data_bits: DataBits::Eight,
|
||||
@ -173,15 +177,27 @@ fn main_proc(to_main_tx: Sender<Message>, rx: Receiver<Message>, ui_upd: glib::S
|
||||
stopbit: StopBits::One,
|
||||
timeout: 10,
|
||||
};
|
||||
let mut port_status_open = false;
|
||||
default_com_port = list_available_ports().1;
|
||||
ui_upd.send(Message::UiUpdateStatusBarCom(default_com_port.clone()));
|
||||
loop {
|
||||
if let Ok(msg) = rx.try_recv() {
|
||||
match msg {
|
||||
Message::UiCleanReceiveMsg => {}
|
||||
Message::MainCmdOpen(arg) => {
|
||||
if port_status_open {
|
||||
ui_upd.send(Message::UiUpdateLog("Port Already Open\n".to_string())).unwrap();
|
||||
continue;
|
||||
}
|
||||
let (tmp_to_port, main2port_rx) = channel::<Message>();
|
||||
main2port_tx = tmp_to_port;
|
||||
match port_open(arg, port_default_config) {
|
||||
|
||||
let port_name = if arg.is_empty() {default_com_port.clone()} else {arg};
|
||||
default_com_port = port_name.clone();
|
||||
ui_upd.send(Message::UiUpdateStatusBarCom(default_com_port.clone()));
|
||||
match port_open(port_name, port_default_config) {
|
||||
Ok(port) => {
|
||||
port_status_open = true;
|
||||
std::thread::spawn(closure!(clone to_main_tx, || port_proc(port, to_main_tx, main2port_rx)));
|
||||
ui_upd.send(Message::UiUpdateLog("open Port succeed\n".to_string())).unwrap();
|
||||
}
|
||||
@ -195,8 +211,13 @@ fn main_proc(to_main_tx: Sender<Message>, rx: Receiver<Message>, ui_upd: glib::S
|
||||
}
|
||||
}
|
||||
Message::MainCmdClose => {
|
||||
if port_status_open{
|
||||
main2port_tx.send(Message::PortCmdClose).unwrap();
|
||||
}
|
||||
else {
|
||||
ui_upd.send(Message::UiUpdateLog("No port opened\n".to_string())).unwrap();
|
||||
}
|
||||
}
|
||||
Message::MainCmdSend(msg) => {
|
||||
main2port_tx.send(Message::PortCmdSend(msg)).unwrap();
|
||||
}
|
||||
@ -302,9 +323,11 @@ fn main_proc(to_main_tx: Sender<Message>, rx: Receiver<Message>, ui_upd: glib::S
|
||||
}
|
||||
Message::PortCloseSucceed => {
|
||||
ui_upd.send(Message::UiUpdateLog("Close port succeed\n".to_string())).unwrap();
|
||||
port_status_open = false;
|
||||
}
|
||||
Message::PortError(msg) => {
|
||||
ui_upd.send(Message::UiUpdateLog(format!("port closed by error: {}", msg))).unwrap();
|
||||
port_status_open = false;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
@ -12,6 +12,7 @@ pub enum Message {
|
||||
UiShowLog,
|
||||
UiHideLog,
|
||||
|
||||
UiUpdateStatusBarCom(String),
|
||||
UiUpdateStatusBarBaudrate(u32),
|
||||
UiUpdateStatusBarParity(String),
|
||||
UiUpdateStatusBarStopbit(u32),
|
||||
@ -94,17 +95,21 @@ pub fn port_proc(mut port: Box<dyn SerialPort>, to_main: Sender<Message>, from_m
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn list_available_ports() -> String {
|
||||
pub(crate) fn list_available_ports() -> (String, String){
|
||||
let mut ret = String::new();
|
||||
let mut ret_portname = String::new();
|
||||
match serialport::available_ports() {
|
||||
Ok(ports) => {
|
||||
for p in ports {
|
||||
ret += &*String::from(format!("{:?}\n", p));
|
||||
if ret_portname.is_empty() {
|
||||
ret_portname = p.port_name;
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
ret = format!("{}", err);
|
||||
}
|
||||
}
|
||||
ret
|
||||
(ret,ret_portname)
|
||||
}
|
||||
|
30
src/test.ui
30
src/test.ui
@ -5,10 +5,18 @@
|
||||
<object class="GtkWindow" id="log_window">
|
||||
<property name="can-focus">False</property>
|
||||
<property name="icon-name">network-server</property>
|
||||
<child>
|
||||
<object class="GtkScrolledWindow">
|
||||
<property name="visible">True</property>
|
||||
<property name="can-focus">True</property>
|
||||
<property name="shadow-type">in</property>
|
||||
<child>
|
||||
<object class="GtkTextView" id="log_view">
|
||||
<property name="visible">True</property>
|
||||
<property name="can-focus">True</property>
|
||||
<property name="editable">False</property>
|
||||
</object>
|
||||
</child>
|
||||
</object>
|
||||
</child>
|
||||
</object>
|
||||
@ -229,6 +237,7 @@
|
||||
<object class="GtkTextView" id="receive_view">
|
||||
<property name="visible">True</property>
|
||||
<property name="can-focus">True</property>
|
||||
<property name="editable">False</property>
|
||||
</object>
|
||||
</child>
|
||||
</object>
|
||||
@ -244,6 +253,19 @@
|
||||
<property name="can-focus">False</property>
|
||||
<property name="hexpand">True</property>
|
||||
<property name="spacing">5</property>
|
||||
<child>
|
||||
<object class="GtkLabel" id="ui_com">
|
||||
<property name="visible">True</property>
|
||||
<property name="can-focus">False</property>
|
||||
<property name="hexpand">True</property>
|
||||
<property name="label" translatable="yes">COM</property>
|
||||
</object>
|
||||
<packing>
|
||||
<property name="expand">False</property>
|
||||
<property name="fill">True</property>
|
||||
<property name="position">1</property>
|
||||
</packing>
|
||||
</child>
|
||||
<child>
|
||||
<object class="GtkLabel" id="ui_baudrate">
|
||||
<property name="visible">True</property>
|
||||
@ -254,7 +276,7 @@
|
||||
<packing>
|
||||
<property name="expand">False</property>
|
||||
<property name="fill">True</property>
|
||||
<property name="position">1</property>
|
||||
<property name="position">2</property>
|
||||
</packing>
|
||||
</child>
|
||||
<child>
|
||||
@ -267,7 +289,7 @@
|
||||
<packing>
|
||||
<property name="expand">False</property>
|
||||
<property name="fill">True</property>
|
||||
<property name="position">2</property>
|
||||
<property name="position">3</property>
|
||||
</packing>
|
||||
</child>
|
||||
<child>
|
||||
@ -280,7 +302,7 @@
|
||||
<packing>
|
||||
<property name="expand">False</property>
|
||||
<property name="fill">True</property>
|
||||
<property name="position">3</property>
|
||||
<property name="position">4</property>
|
||||
</packing>
|
||||
</child>
|
||||
<child>
|
||||
@ -293,7 +315,7 @@
|
||||
<packing>
|
||||
<property name="expand">False</property>
|
||||
<property name="fill">True</property>
|
||||
<property name="position">4</property>
|
||||
<property name="position">5</property>
|
||||
</packing>
|
||||
</child>
|
||||
</object>
|
||||
|
Loading…
x
Reference in New Issue
Block a user