/// <summary> /// Close the port if needed and save preferences. /// </summary> private void Form1_FormClosing(object sender, System.Windows.Forms.FormClosingEventArgs e) { UserPort1.CloseComPort(UserPort1.SelectedPort); }
/// <summary> /// Saves the passed port parameters. /// Called when the user clicks OK on PortSettingsDialog. /// </summary> private void SetPortParameters(string userPort, int userBitRate, Handshake userHandshake) { try { // Don't do anything if the system has no COM ports. if (ComPorts.comPortExists) { if (MyPortSettingsDialog.ParameterChanged()) { // One or more port parameters has changed. if ((string.Compare(MyPortSettingsDialog.oldPortName, userPort, true) != 0)) { // The port has changed. // Close the previously selected port. UserPort1.PreviousPort = UserPort1.SelectedPort; UserPort1.CloseComPort(UserPort1.SelectedPort); // Set SelectedPort to the current port. UserPort1.SelectedPort.PortName = userPort; UserPort1.PortChanged = true; } // Set other port parameters. UserPort1.SelectedPort.BaudRate = userBitRate; UserPort1.SelectedPort.Handshake = userHandshake; MyPortSettingsDialog.SavePortParameters(); UserPort1.ParameterChanged = true; } else { UserPort1.ParameterChanged = false; } } } catch (InvalidOperationException ex) { UserPort1.ParameterChanged = true; DisplayException(ModuleName, ex); } catch (UnauthorizedAccessException ex) { UserPort1.ParameterChanged = true; DisplayException(ModuleName, ex); // This exception can occur if the port was removed. // If the port was open, close it. UserPort1.CloseComPort(UserPort1.SelectedPort); } catch (System.IO.IOException ex) { UserPort1.ParameterChanged = true; DisplayException(ModuleName, ex); } }