update status bar com serial port information

This commit is contained in:
Guangzong Chen 2021-10-06 20:41:28 -04:00
parent 871fe65187
commit cc56f48c0a
No known key found for this signature in database
GPG Key ID: 46AFD2E1BC3FB8A1
3 changed files with 61 additions and 11 deletions

View File

@ -47,6 +47,7 @@ fn build_ui(application: &gtk::Application) {
let ui_databit: Label = builder.object("ui_databit").unwrap(); let ui_databit: Label = builder.object("ui_databit").unwrap();
let ui_parity: Label = builder.object("ui_parity").unwrap(); let ui_parity: Label = builder.object("ui_parity").unwrap();
let ui_stopbit: Label = builder.object("ui_stopbit").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_statusbar: Statusbar = builder.object("statusbar").unwrap();
let ui_log_view: TextView = builder.object("log_view").unwrap(); let ui_log_view: TextView = builder.object("log_view").unwrap();
@ -83,6 +84,7 @@ fn build_ui(application: &gtk::Application) {
Message::UiUpdateStatusBarDatabit(value) => ui_databit.set_text(value.to_string().as_str()), 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::UiUpdateStatusBarParity(value) => ui_parity.set_text(value.to_string().as_str()),
Message::UiUpdateStatusBarStopbit(value) => ui_stopbit.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) glib::Continue(true)
@ -119,7 +121,8 @@ fn process_cmd(command: String, to_ui: glib::Sender<Message>, gui2main_tx: Sende
} }
"open" => { "open" => {
if command_split.len() == 1 { 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 { } else {
let args: Vec<String> = command_split[1..].to_vec().into_iter().map(|item| String::from(item)).collect(); 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(); 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(); gui2main_tx.send(Message::MainCmdClose).unwrap();
} }
"list" => { "list" => {
to_ui.send(Message::UiUpdateLog(list_available_ports())).unwrap(); to_ui.send(Message::UiUpdateLog(list_available_ports().0)).unwrap();
} }
"send" => { "send" => {
gui2main_tx.send(Message::MainCmdSend(command[5..].parse().unwrap())).unwrap(); 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>) { 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 main2port_tx, _rx_port_receive) = channel::<Message>();
let mut default_com_port = "USB0".to_string();
let mut port_default_config = PortConfig { let mut port_default_config = PortConfig {
baud_rate: 115_200, baud_rate: 115_200,
data_bits: DataBits::Eight, 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, stopbit: StopBits::One,
timeout: 10, 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 { loop {
if let Ok(msg) = rx.try_recv() { if let Ok(msg) = rx.try_recv() {
match msg { match msg {
Message::UiCleanReceiveMsg => {} Message::UiCleanReceiveMsg => {}
Message::MainCmdOpen(arg) => { 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>(); let (tmp_to_port, main2port_rx) = channel::<Message>();
main2port_tx = tmp_to_port; 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) => { Ok(port) => {
port_status_open = true;
std::thread::spawn(closure!(clone to_main_tx, || port_proc(port, to_main_tx, main2port_rx))); 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(); ui_upd.send(Message::UiUpdateLog("open Port succeed\n".to_string())).unwrap();
} }
@ -195,7 +211,12 @@ fn main_proc(to_main_tx: Sender<Message>, rx: Receiver<Message>, ui_upd: glib::S
} }
} }
Message::MainCmdClose => { Message::MainCmdClose => {
main2port_tx.send(Message::PortCmdClose).unwrap(); 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) => { Message::MainCmdSend(msg) => {
main2port_tx.send(Message::PortCmdSend(msg)).unwrap(); 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 => { Message::PortCloseSucceed => {
ui_upd.send(Message::UiUpdateLog("Close port succeed\n".to_string())).unwrap(); ui_upd.send(Message::UiUpdateLog("Close port succeed\n".to_string())).unwrap();
port_status_open = false;
} }
Message::PortError(msg) => { Message::PortError(msg) => {
ui_upd.send(Message::UiUpdateLog(format!("port closed by error: {}", msg))).unwrap(); ui_upd.send(Message::UiUpdateLog(format!("port closed by error: {}", msg))).unwrap();
port_status_open = false;
} }
_ => {} _ => {}
} }

View File

@ -12,6 +12,7 @@ pub enum Message {
UiShowLog, UiShowLog,
UiHideLog, UiHideLog,
UiUpdateStatusBarCom(String),
UiUpdateStatusBarBaudrate(u32), UiUpdateStatusBarBaudrate(u32),
UiUpdateStatusBarParity(String), UiUpdateStatusBarParity(String),
UiUpdateStatusBarStopbit(u32), 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 = String::new();
let mut ret_portname = String::new();
match serialport::available_ports() { match serialport::available_ports() {
Ok(ports) => { Ok(ports) => {
for p in ports { for p in ports {
ret += &*String::from(format!("{:?}\n", p)); ret += &*String::from(format!("{:?}\n", p));
if ret_portname.is_empty() {
ret_portname = p.port_name;
}
} }
} }
Err(err) => { Err(err) => {
ret = format!("{}", err); ret = format!("{}", err);
} }
} }
ret (ret,ret_portname)
} }

View File

@ -6,9 +6,17 @@
<property name="can-focus">False</property> <property name="can-focus">False</property>
<property name="icon-name">network-server</property> <property name="icon-name">network-server</property>
<child> <child>
<object class="GtkTextView" id="log_view"> <object class="GtkScrolledWindow">
<property name="visible">True</property> <property name="visible">True</property>
<property name="can-focus">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> </object>
</child> </child>
</object> </object>
@ -229,6 +237,7 @@
<object class="GtkTextView" id="receive_view"> <object class="GtkTextView" id="receive_view">
<property name="visible">True</property> <property name="visible">True</property>
<property name="can-focus">True</property> <property name="can-focus">True</property>
<property name="editable">False</property>
</object> </object>
</child> </child>
</object> </object>
@ -244,6 +253,19 @@
<property name="can-focus">False</property> <property name="can-focus">False</property>
<property name="hexpand">True</property> <property name="hexpand">True</property>
<property name="spacing">5</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> <child>
<object class="GtkLabel" id="ui_baudrate"> <object class="GtkLabel" id="ui_baudrate">
<property name="visible">True</property> <property name="visible">True</property>
@ -254,7 +276,7 @@
<packing> <packing>
<property name="expand">False</property> <property name="expand">False</property>
<property name="fill">True</property> <property name="fill">True</property>
<property name="position">1</property> <property name="position">2</property>
</packing> </packing>
</child> </child>
<child> <child>
@ -267,7 +289,7 @@
<packing> <packing>
<property name="expand">False</property> <property name="expand">False</property>
<property name="fill">True</property> <property name="fill">True</property>
<property name="position">2</property> <property name="position">3</property>
</packing> </packing>
</child> </child>
<child> <child>
@ -280,7 +302,7 @@
<packing> <packing>
<property name="expand">False</property> <property name="expand">False</property>
<property name="fill">True</property> <property name="fill">True</property>
<property name="position">3</property> <property name="position">4</property>
</packing> </packing>
</child> </child>
<child> <child>
@ -293,7 +315,7 @@
<packing> <packing>
<property name="expand">False</property> <property name="expand">False</property>
<property name="fill">True</property> <property name="fill">True</property>
<property name="position">4</property> <property name="position">5</property>
</packing> </packing>
</child> </child>
</object> </object>