COMPortTerminal.MainForm.SetPortParameters C# (CSharp) Method

SetPortParameters() private method

private SetPortParameters ( string userPort, int userBitRate, Handshake userHandshake ) : void
userPort string
userBitRate int
userHandshake Handshake
return void
        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 );
            }
        }