@@ -89,6 +89,15 @@ impl Default for Device {
8989 }
9090}
9191
92+ pub fn serial_devices_thread ( devices_lock : Arc < RwLock < Vec < String > > > ) {
93+ loop {
94+ if let Ok ( mut write_guard) = devices_lock. write ( ) {
95+ * write_guard = available_devices ( ) ;
96+ }
97+ std:: thread:: sleep ( Duration :: from_millis ( 500 ) ) ;
98+ }
99+ }
100+
92101fn serial_write (
93102 port : & mut BufReader < Box < dyn SerialPort > > ,
94103 cmd : & [ u8 ] ,
@@ -165,19 +174,17 @@ pub fn serial_thread(
165174 . create ( ) ;
166175
167176 ' connected_loop: loop {
168- let devices = available_devices ( ) ;
169- if let Ok ( mut write_guard ) = devices_lock . write ( ) {
170- * write_guard = devices . clone ( ) ;
171- }
172-
173- if disconnected ( & device , & devices , & device_lock , & mut last_connected_device ) {
177+ if disconnected (
178+ & device ,
179+ & device_lock ,
180+ & devices_lock ,
181+ & mut last_connected_device ,
182+ ) {
174183 break ' connected_loop;
175184 }
176185
177186 perform_writes ( & mut port, & send_rx, & raw_data_tx, t_zero) ;
178187 perform_reads ( & mut port, & raw_data_tx, t_zero) ;
179-
180- //std::thread::sleep(Duration::from_millis(10));
181188 }
182189 std:: mem:: drop ( port) ;
183190 }
@@ -222,28 +229,30 @@ fn get_device(
222229
223230fn disconnected (
224231 device : & Device ,
225- devices : & [ String ] ,
226232 device_lock : & Arc < RwLock < Device > > ,
233+ devices_lock : & Arc < RwLock < Vec < String > > > ,
227234 last_connected_device : & mut Device ,
228235) -> bool {
229236 // disconnection by button press
230- if let Ok ( read_guard) = device_lock. read ( ) {
237+ if let Ok ( read_guard) = device_lock. try_read ( ) {
231238 if device. name != read_guard. name {
232239 * last_connected_device = Device :: default ( ) ;
233240 log:: info!( "Disconnected from serial port: {}" , device. name) ;
234241 return true ;
235242 }
236243 }
237244
238- // other types of disconnection (e.g. unplugging, power down)
239- if !devices. contains ( & device. name ) {
240- if let Ok ( mut write_guard) = device_lock. write ( ) {
241- write_guard. name . clear ( ) ;
242- }
243- * last_connected_device = device. clone ( ) ;
244- log:: error!( "Device has disconnected from serial port: {}" , device. name) ;
245- return true ;
246- } ;
245+ if let Ok ( devices) = devices_lock. try_read ( ) {
246+ // other types of disconnection (e.g. unplugging, power down)
247+ if !devices. contains ( & device. name ) {
248+ if let Ok ( mut write_guard) = device_lock. try_write ( ) {
249+ write_guard. name . clear ( ) ;
250+ }
251+ * last_connected_device = device. clone ( ) ;
252+ log:: error!( "Device has disconnected from serial port: {}" , device. name) ;
253+ return true ;
254+ } ;
255+ }
247256 false
248257}
249258
@@ -279,7 +288,15 @@ fn perform_reads(
279288 let mut buf = "" . to_string ( ) ;
280289 match serial_read ( port, & mut buf) {
281290 Ok ( _) => {
282- let delimiter = if buf. contains ( "\r \n " ) { "\r \n " } else { "\0 \0 " } ;
291+ let delimiter = if buf. contains ( "\r \n " ) {
292+ "\r \n "
293+ } else if buf. contains ( "\r " ) {
294+ "\r "
295+ } else if buf. contains ( "\n " ) {
296+ "\n "
297+ } else {
298+ "\0 \0 "
299+ } ;
283300 buf. split_terminator ( delimiter) . for_each ( |s| {
284301 let packet = Packet {
285302 relative_time : Instant :: now ( ) . duration_since ( t_zero) . as_millis ( ) as f64 ,
0 commit comments