@@ -89,6 +89,15 @@ impl Default for Device {
89
89
}
90
90
}
91
91
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
+
92
101
fn serial_write (
93
102
port : & mut BufReader < Box < dyn SerialPort > > ,
94
103
cmd : & [ u8 ] ,
@@ -165,19 +174,17 @@ pub fn serial_thread(
165
174
. create ( ) ;
166
175
167
176
' 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
+ ) {
174
183
break ' connected_loop;
175
184
}
176
185
177
186
perform_writes ( & mut port, & send_rx, & raw_data_tx, t_zero) ;
178
187
perform_reads ( & mut port, & raw_data_tx, t_zero) ;
179
-
180
- //std::thread::sleep(Duration::from_millis(10));
181
188
}
182
189
std:: mem:: drop ( port) ;
183
190
}
@@ -222,28 +229,30 @@ fn get_device(
222
229
223
230
fn disconnected (
224
231
device : & Device ,
225
- devices : & [ String ] ,
226
232
device_lock : & Arc < RwLock < Device > > ,
233
+ devices_lock : & Arc < RwLock < Vec < String > > > ,
227
234
last_connected_device : & mut Device ,
228
235
) -> bool {
229
236
// disconnection by button press
230
- if let Ok ( read_guard) = device_lock. read ( ) {
237
+ if let Ok ( read_guard) = device_lock. try_read ( ) {
231
238
if device. name != read_guard. name {
232
239
* last_connected_device = Device :: default ( ) ;
233
240
log:: info!( "Disconnected from serial port: {}" , device. name) ;
234
241
return true ;
235
242
}
236
243
}
237
244
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
+ }
247
256
false
248
257
}
249
258
@@ -279,7 +288,15 @@ fn perform_reads(
279
288
let mut buf = "" . to_string ( ) ;
280
289
match serial_read ( port, & mut buf) {
281
290
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
+ } ;
283
300
buf. split_terminator ( delimiter) . for_each ( |s| {
284
301
let packet = Packet {
285
302
relative_time : Instant :: now ( ) . duration_since ( t_zero) . as_millis ( ) as f64 ,
0 commit comments