Sub OpenComPort() Try ' Get the selected COM port's name ' from the combo box. If Not myComPort.IsOpen Then myComPort.PortName = _ cmbPorts.SelectedItem.ToString ' Get the selected bit rate from the combo box. If cmbBitRate.SelectedIndex > 0 Then myComPort.BaudRate = _ CInt(cmbBitRate.SelectedItem) End If ' Set other port parameters. myComPort.Parity = Parity.None myComPort.DataBits = 8 myComPort.StopBits = StopBits.One myComPort.Handshake = Handshake.None myComPort.ReadTimeout = 3000 myComPort.WriteTimeout = 5000 ' Open the port. myComPort.Open() End If Catch ex As InvalidOperationException MessageBox.Show(ex.Message) Catch ex As UnauthorizedAccessException MessageBox.Show(ex.Message) Catch ex As System.IO.IOException MessageBox.Show(ex.Message) End Try End Sub Listing 2. Before transferring data, the application must set port parameters and open a connection to the port.