diff --git a/src/VRio.App/MainForm.cs b/src/VRio.App/MainForm.cs index 2b75143..857cfd8 100644 --- a/src/VRio.App/MainForm.cs +++ b/src/VRio.App/MainForm.cs @@ -8,12 +8,21 @@ namespace VRio.App; /// /// vRIO main window: the interactive cockpit panel on the left (the same /// functional map RIOJoy's profile editor shows) and a control strip on the -/// right — COM port, device settings, and a live wire log. Open the COM port, -/// point RIOJoy at the other end of the null-modem pair, and every click here -/// arrives at RIOJoy exactly as if the physical cockpit sent it. +/// right — COM port, device settings, and a live wire log. At startup the +/// usual port () is opened automatically when it's +/// available; otherwise open a COM port by hand. Point RIOJoy at the other +/// end of the null-modem pair, and every click here arrives at RIOJoy exactly +/// as if the physical cockpit sent it. /// internal sealed class MainForm : Form { + /// + /// vRIO's usual port: the device end of the COM1⇄COM11 com0com pair. + /// Auto-opened at startup when present and free; the picker still allows + /// any other port. + /// + private const string PreferredPort = "COM11"; + private readonly VRioDevice _device = new(); private readonly VRioSerialService _service; private readonly PanelCanvas _canvas = new(); @@ -248,6 +257,7 @@ internal sealed class MainForm : Form UpdateStatus(); PrependLog("vRIO ready. Open a COM port, then point RIOJoy at the other end of the pair."); LoadBindings(); + AutoOpenPreferredPort(); } private Panel BuildControlStrip() @@ -307,6 +317,32 @@ internal sealed class MainForm : Form _portBox.SelectedIndex = idx >= 0 ? idx : 0; } + /// + /// Startup convenience: if exists, select it + /// and try to open it. Failures (port missing, or busy because another + /// vRIO/app holds it) just log — no modal box at launch — and leave the + /// manual picker in charge. + /// + private void AutoOpenPreferredPort() + { + int idx = _portBox.Items.IndexOf(PreferredPort); + if (idx < 0) + { + PrependLog($"{PreferredPort} not present — pick a port and open it manually."); + return; + } + + _portBox.SelectedIndex = idx; + try + { + _service.Open(PreferredPort); + } + catch (Exception ex) when (ex is IOException or UnauthorizedAccessException or InvalidOperationException or ArgumentException) + { + PrependLog($"{PreferredPort} is present but could not be opened ({ex.Message.TrimEnd('.')}) — open it manually once it frees up."); + } + } + private void ToggleOpen() { if (_service.IsOpen)