HamBook – Diff between revs 7 and 9

Subversion Repositories:
Rev:
Show entire fileIgnore whitespace
Rev 7 Rev 9
Line 18... Line 18...
18 using HamBook.Properties; 18 using HamBook.Properties;
19 using HamBook.Radios.Generic; 19 using HamBook.Radios.Generic;
20 using PowerState = HamBook.Radios.Generic.PowerState; 20 using PowerState = HamBook.Radios.Generic.PowerState;
21 using System.Media; 21 using System.Media;
22 using HamBook.Utilities.Controls; 22 using HamBook.Utilities.Controls;
-   23 using RJCP.IO.Ports;
-   24 using System.Text;
Line 23... Line 25...
23   25  
24 namespace HamBook 26 namespace HamBook
25 { 27 {
26 public partial class Form1 : Form 28 public partial class Form1 : Form
27 { 29 {
28 private ScheduledContinuation _changedConfigurationContinuation; 30 private ScheduledContinuation _changedConfigurationContinuation;
29 private Configuration.Configuration Configuration { get; set; } 31 private Configuration.Configuration Configuration { get; set; }
30 private SerialPort _serialPort; 32 private SerialPortStream _serialPort;
31 private LogMemorySink _memorySink; 33 private LogMemorySink _memorySink;
32 private ViewLogsForm _viewLogsForm; 34 private ViewLogsForm _viewLogsForm;
33 private AboutForm _aboutForm; 35 private AboutForm _aboutForm;
34 private SettingsForm _settingsForm; 36 private SettingsForm _settingsForm;
Line 83... Line 85...
83   85  
Line 84... Line 86...
84 _catAssemblies = InitializeAssemblies(_serialPort); 86 _catAssemblies = InitializeAssemblies(_serialPort);
85   87  
-   88 try
-   89 {
-   90 await _catAssemblies.CatWriteAsync<InformationState>("AI", new object[] { InformationState.OFF }, _cancellationToken);
-   91 }
-   92 finally
-   93 {
-   94  
-   95 }
-   96  
86 try 97 try
87 { 98 {
88 switch (await _catAssemblies.CatRead<PowerState>("PS", new object[] { }, _cancellationToken)) 99 switch (await _catAssemblies.CatReadAsync<PowerState>("PS", new object[] { }, _cancellationToken))
89 { 100 {
90 case PowerState.ON: 101 case PowerState.ON:
91 Log.Information(Resources.Attempting_to_initialize_radio); 102 Log.Information(Resources.Attempting_to_initialize_radio);
92 if(!InitializeRadio()) 103 if(!await InitializeRadio())
93 { 104 {
Line 94... Line 105...
94 return; 105 return;
Line 102... Line 113...
102 { 113 {
103 Log.Error(exception, Resources.Failed_to_read_power_state); 114 Log.Error(exception, Resources.Failed_to_read_power_state);
104 } 115 }
105 } 116 }
Line 106... Line 117...
106   117  
107 private void quitToolStripMenuItem_Click(object sender, EventArgs e) 118 private async void quitToolStripMenuItem_Click(object sender, EventArgs e)
-   119 {
-   120 if(_bandScan != null)
-   121 {
-   122 await _bandScan.Stop();
-   123 _bandScan = null;
-   124 }
108 { 125  
109 Close(); 126 Close();
Line 110... Line 127...
110 } 127 }
111   128  
Line 188... Line 205...
188   205  
Line 189... Line 206...
189 _catAssemblies = InitializeAssemblies(_serialPort); 206 _catAssemblies = InitializeAssemblies(_serialPort);
190   207  
-   208 try
-   209 {
-   210 await _catAssemblies.CatWriteAsync<InformationState>("AI", new object[] { InformationState.OFF }, _cancellationToken);
-   211 }
-   212 finally
-   213 {
-   214  
-   215 }
-   216  
191 try 217 try
192 { 218 {
193 switch (await _catAssemblies.CatRead<PowerState>("PS", new object[] { }, _cancellationToken)) 219 switch (await _catAssemblies.CatReadAsync<PowerState>("PS", new object[] { }, _cancellationToken))
194 { 220 {
195 case PowerState.ON: 221 case PowerState.ON:
196 Log.Information(Resources.Attempting_to_initialize_radio); 222 Log.Information(Resources.Attempting_to_initialize_radio);
197 if (!InitializeRadio()) 223 if (!await InitializeRadio())
198 { 224 {
199 return; 225 return;
200 } 226 }
Line 255... Line 281...
255 default: 281 default:
256 return new Configuration.Configuration(); 282 return new Configuration.Configuration();
257 } 283 }
258 } 284 }
Line 259... Line 285...
259   285  
260 private bool InitializeRadio() 286 private async Task<bool> InitializeRadio()
261 { 287 {
262 try 288 try
-   289 {
-   290 await _catAssemblies.CatWriteAsync<InformationState>("AI", new object[] { InformationState.OFF }, _cancellationToken);
263 { 291  
264 if (!_catAssemblies.CatRead<bool>("ID", new object[] { })) 292 if (!_catAssemblies.CatRead<bool>("ID", new object[] { }))
265 { 293 {
266 return false; 294 return false;
267 } -  
268   -  
269 _catAssemblies.CatSet<InformationState>("AI", new object[] { InformationState.OFF }); 295 }
270 } 296 }
271 catch(Exception exception) 297 catch(Exception exception)
272 { 298 {
273 Log.Error(exception, Resources.Unable_to_initialize_radio); 299 Log.Error(exception, Resources.Unable_to_initialize_radio);
274 return false; 300 return false;
Line 275... Line 301...
275 } 301 }
276   302  
Line 277... Line 303...
277 return true; 303 return true;
278 } 304 }
279   305  
280 private CatAssemblies InitializeAssemblies(SerialPort serialPort) 306 private CatAssemblies InitializeAssemblies(SerialPortStream serialPort)
281 { 307 {
282 if(_catAssemblies != null) 308 if(_catAssemblies != null)
283 { 309 {
Line 284... Line 310...
284 _catAssemblies.Dispose(); 310 _catAssemblies.Dispose();
285 _catAssemblies = null; 311 _catAssemblies = null;
Line 286... Line 312...
286 } 312 }
287   313  
288 return new CatAssemblies(serialPort, Configuration.Radio); 314 return new CatAssemblies(serialPort, Configuration.Radio);
289 } 315 }
290   316  
291 private SerialPort InitializeSerialPort(Configuration.Configuration configuration) 317 private SerialPortStream InitializeSerialPort(Configuration.Configuration configuration)
Line 299... Line 325...
299 _serialPort.Dispose(); 325 _serialPort.Dispose();
300 _serialPort = null; 326 _serialPort = null;
301 } 327 }
Line 302... Line 328...
302   328  
303 // Set up serial connection. 329 // Set up serial connection.
304 var serialPort = new SerialPort(configuration.Port, configuration.Speed, configuration.Parity, configuration.DataBits, configuration.StopBits); 330 var serialPort = new SerialPortStream(configuration.Port, configuration.Speed, configuration.DataBits, configuration.Parity, configuration.StopBits);
305 serialPort.ReadTimeout = configuration.SerialPortTimeout.Read; 331 serialPort.ReadTimeout = configuration.SerialPortTimeout.Read;
306 serialPort.WriteTimeout = configuration.SerialPortTimeout.Write; 332 serialPort.WriteTimeout = configuration.SerialPortTimeout.Write;
-   333 serialPort.Handshake = configuration.Handshake;
Line 307... Line 334...
307 serialPort.Handshake = configuration.Handshake; 334 serialPort.Encoding = Encoding.ASCII;
Line 308... Line 335...
308   335  
309 Log.Information($"{Resources.Initialized_serial_port} {configuration.Port} {configuration.Speed} {configuration.Parity} {configuration.DataBits} {configuration.StopBits}"); 336 Log.Information($"{Resources.Initialized_serial_port} {configuration.Port} {configuration.Speed} {configuration.Parity} {configuration.DataBits} {configuration.StopBits}");
Line 325... Line 352...
325 MessageBox.Show(Resources.No_updates_available_at_this_time, Resources.HamBook, MessageBoxButtons.OK, 352 MessageBox.Show(Resources.No_updates_available_at_this_time, Resources.HamBook, MessageBoxButtons.OK,
326 MessageBoxIcon.Asterisk, 353 MessageBoxIcon.Asterisk,
327 MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly, false); 354 MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly, false);
328 } 355 }
Line 329... Line 356...
329   356  
330 private void toolStripComboBox1_SelectedIndexChanged(object sender, EventArgs e) 357 private async void toolStripComboBox1_SelectedIndexChanged(object sender, EventArgs e)
331 { 358 {
332 var toolStripComboBox = (ToolStripComboBox)sender; 359 var toolStripComboBox = (ToolStripComboBox)sender;
333 if(RadioMode.TryParse(toolStripComboBox.Text, out var radioMode)) 360 if(RadioMode.TryParse(toolStripComboBox.Text, out var radioMode))
334 { 361 {
335 try 362 try
336 { 363 {
337 _catAssemblies.CatSet<RadioMode>("MD", new object[] { radioMode }); 364 await _catAssemblies.CatSetAsync<RadioMode>("MD", new object[] { radioMode }, _cancellationToken);
338 } 365 }
339 catch (Exception exception) 366 catch (Exception exception)
340 { 367 {
341 Log.Error(exception, Resources.Failed_to_set_radio_mode, radioMode); 368 Log.Error(exception, Resources.Failed_to_set_radio_mode, radioMode);
Line 345... Line 372...
345   372  
346 private async void onToolStripMenuItem_Click(object sender, EventArgs e) 373 private async void onToolStripMenuItem_Click(object sender, EventArgs e)
347 { 374 {
348 try 375 try
349 { 376 {
350 await _catAssemblies.CatSet<PowerState>("PS", new object[] { PowerState.ON }, _cancellationToken); 377 await _catAssemblies.CatSetAsync<PowerState>("PS", new object[] { PowerState.ON }, _cancellationToken);
351 } 378 }
352 catch(Exception exception) 379 catch(Exception exception)
353 { 380 {
354 Log.Error(exception, Resources.Failed_to_set_power_state); 381 Log.Error(exception, Resources.Failed_to_set_power_state);
Line 357... Line 384...
357   384  
358 private async void offToolStripMenuItem_Click(object sender, EventArgs e) 385 private async void offToolStripMenuItem_Click(object sender, EventArgs e)
359 { 386 {
360 try 387 try
361 { 388 {
362 await _catAssemblies.CatSet<PowerState>("PS", new object[] { PowerState.OFF }, _cancellationToken); 389 await _catAssemblies.CatSetAsync<PowerState>("PS", new object[] { PowerState.OFF }, _cancellationToken);
363 } 390 }
364 catch(Exception exception) 391 catch(Exception exception)
365 { 392 {
366 Log.Error(exception, Resources.Failed_to_set_power_state); 393 Log.Error(exception, Resources.Failed_to_set_power_state);
367 } 394 }
Line 368... Line 395...
368 } 395 }
369   396  
370 private void toolStripComboBox2_MouseWheel(object sender, MouseEventArgs e) 397 private async void toolStripComboBox2_MouseWheel(object sender, MouseEventArgs e)
371 { 398 {
372 using (var soundPlayer = new SoundPlayer(Assembly.GetExecutingAssembly().GetManifestResourceStream("HamBook.Effects.pot.wav"))) 399 using (var soundPlayer = new SoundPlayer(Assembly.GetExecutingAssembly().GetManifestResourceStream("HamBook.Effects.pot.wav")))
373 { 400 {
Line 386... Line 413...
386   413  
Line 387... Line 414...
387 soundPlayer.Play(); 414 soundPlayer.Play();
388   415  
389 try 416 try
390 { 417 {
Line 391... Line 418...
391 _catAssemblies.CatSet<int>("FA", new object[] { frequency }); 418 await _catAssemblies.CatSetAsync<int>("FA", new object[] { frequency }, _cancellationToken);
392 toolStripComboBox.Text = $"{frequency}"; 419 toolStripComboBox.Text = $"{frequency}";
393   420  
Line 399... Line 426...
399 } 426 }
400 } 427 }
401 } 428 }
402 } 429 }
Line 403... Line 430...
403   430  
404 private void scrollableToolStripComboBox1_MouseWheel(object sender, MouseEventArgs e) 431 private async void scrollableToolStripComboBox1_MouseWheel(object sender, MouseEventArgs e)
405 { 432 {
406 using (var soundPlayer = new SoundPlayer(Assembly.GetExecutingAssembly().GetManifestResourceStream("HamBook.Effects.pot.wav"))) 433 using (var soundPlayer = new SoundPlayer(Assembly.GetExecutingAssembly().GetManifestResourceStream("HamBook.Effects.pot.wav")))
407 { 434 {
408 var toolStripComboBox = (ScrollableToolStripComboBox)sender; 435 var toolStripComboBox = (ScrollableToolStripComboBox)sender;
Line 420... Line 447...
420   447  
Line 421... Line 448...
421 soundPlayer.Play(); 448 soundPlayer.Play();
422   449  
423 try 450 try
424 { 451 {
Line 425... Line 452...
425 _catAssemblies.CatSet<int>("FB", new object[] { frequency }); 452 await _catAssemblies.CatSetAsync<int>("FB", new object[] { frequency }, _cancellationToken);
426 toolStripComboBox.Text = $"{frequency}"; 453 toolStripComboBox.Text = $"{frequency}";
427   454  
Line 433... Line 460...
433 } 460 }
434 } 461 }
435 } 462 }
436 } 463 }
Line 437... Line 464...
437   464  
438 private void scrollableToolStripComboBox1_TextChanged(object sender, EventArgs e) 465 private async void scrollableToolStripComboBox1_TextChanged(object sender, EventArgs e)
439 { 466 {
440 var toolStripComboBox = (ToolStripComboBox)sender; 467 var toolStripComboBox = (ToolStripComboBox)sender;
441 if (int.TryParse(toolStripComboBox.Text, out var frequency)) 468 if (int.TryParse(toolStripComboBox.Text, out var frequency))
442 { 469 {
443 try 470 try
444 { 471 {
445 _catAssemblies.CatSet<int>("FA", new object[] { frequency }); 472 await _catAssemblies.CatSetAsync<int>("FA", new object[] { frequency }, _cancellationToken);
446 } 473 }
447 catch (Exception exception) 474 catch (Exception exception)
448 { 475 {
449 Log.Error(exception, Resources.Failed_to_set_VFO_A_frequency); 476 Log.Error(exception, Resources.Failed_to_set_VFO_A_frequency);
450 } 477 }
451 } 478 }
Line 452... Line 479...
452 } 479 }
453   480  
454 private void scrollableToolStripComboBox2_TextChanged(object sender, EventArgs e) 481 private async void scrollableToolStripComboBox2_TextChanged(object sender, EventArgs e)
455 { 482 {
456 var toolStripComboBox = (ToolStripComboBox)sender; 483 var toolStripComboBox = (ToolStripComboBox)sender;
457 if (int.TryParse(toolStripComboBox.Text, out var frequency)) 484 if (int.TryParse(toolStripComboBox.Text, out var frequency))
458 { 485 {
459 try 486 try
460 { 487 {
461 _catAssemblies.CatSet<int>("FB", new object[] { frequency }); 488 await _catAssemblies.CatSetAsync<int>("FB", new object[] { frequency }, _cancellationToken);
462 } 489 }
463 catch (Exception exception) 490 catch (Exception exception)
464 { 491 {
465 Log.Error(exception, Resources.Failed_to_set_VFO_B_frequency); 492 Log.Error(exception, Resources.Failed_to_set_VFO_B_frequency);
466 } 493 }
Line 467... Line 494...
467 } 494 }
468 } 495 }
469   496  
470 private void toolStripMenuItem1_Click(object sender, EventArgs e) 497 private async void toolStripMenuItem1_Click(object sender, EventArgs e)
471 { 498 {
472 if (_bandScan == null) 499 if (_bandScan == null)
Line 473... Line 500...
473 { 500 {
474 return; 501 return;
475 } 502 }
Line 476... Line 503...
476   503  
477 _bandScan.Stop(); 504 await _bandScan.Stop();
478 _bandScan = null; 505 _bandScan = null;
479 } 506 }
480   507  
481 private void scanToolStripMenuItem_Click(object sender, EventArgs e) 508 private async void scanToolStripMenuItem_Click(object sender, EventArgs e)
Line 501... Line 528...
501 return; 528 return;
502 } 529 }
Line 503... Line 530...
503   530  
504 if (_bandScan != null) 531 if (_bandScan != null)
505 { 532 {
-   533 await _bandScan.Stop();
506 _bandScan.Stop(); 534 _bandScan = null;
Line 507... Line 535...
507 } 535 }
-   536  
508   537 _bandScan = new BandScan(_catAssemblies, band.Min, band.Max, _serialPort);
509 _bandScan = new BandScan(_catAssemblies, band.Min, band.Max, _serialPort); 538  
510 #pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed 539 #pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
511 _bandScan.Start(step, pause); 540 _bandScan.Start(step, pause, Configuration.ScanDetectPause);
Line 512... Line 541...
512 #pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed 541 #pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
513 } 542 }
514   543  
515 private void modeToolStripMenuItem_DropDownOpened(object sender, EventArgs e) 544 private async void modeToolStripMenuItem_DropDownOpened(object sender, EventArgs e)
516 { 545 {
517 try 546 try
518 { 547 {
519 var mode = _catAssemblies.CatRead<RadioMode>("MD", new object[] { }); 548 var mode = await _catAssemblies.CatReadAsync<RadioMode>("MD", new object[] { }, _cancellationToken);
520 toolStripComboBox1.Text = mode; 549 toolStripComboBox1.Text = mode;
521 } 550 }
522 catch (Exception exception) 551 catch (Exception exception)
Line 523... Line 552...
523 { 552 {
524 Log.Error(exception, Resources.Failed_to_read_radio_mode); 553 Log.Error(exception, Resources.Failed_to_read_radio_mode);
525 } 554 }
526   555  
527 try 556 try
528 { 557 {
529 var fa = _catAssemblies.CatRead<int>("FA", new object[] { }); 558 var fa = await _catAssemblies.CatReadAsync<int>("FA", new object[] { }, _cancellationToken);
530 scrollableToolStripComboBox1.Text = $"{fa}"; 559 scrollableToolStripComboBox1.Text = $"{fa}";
531 } 560 }
Line 532... Line 561...
532 catch (Exception exception) 561 catch (Exception exception)
533 { 562 {
534 Log.Error(exception, Resources.Failed_to_read_VFO_A); 563 Log.Error(exception, Resources.Failed_to_read_VFO_A);
535 } 564 }
536   565  
537 try 566 try
538 { 567 {
539 var fb = _catAssemblies.CatRead<int>("FB", new object[] { }); 568 var fb = await _catAssemblies.CatReadAsync<int>("FB", new object[] { }, _cancellationToken);