title: Closing and opening serial port the right way
body: <p>I am using the serial port to connect to a scale and get scale readings in a weighbridge windows app. Now I have coded in such a way that when I click the button to open and start receing the readings it starts to output the readings well and I use this code</p>
  
  <pre><code>Private Sub cmdOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOpen.Click
      comm.Parity = cboParity.Text
      comm.StopBits = cboStop.Text
      comm.DataBits = cboData.Text
      comm.BaudRate = cboBaud.Text
      comm.DisplayWindow = rtbDisplay
      comm.OpenPort()
  
      cmdOpen.Enabled = True
      cmdClose.Enabled = True
      cmdSend.Enabled = True
  
  End Sub
  Public Function OpenPort() As Boolean
      Try
          'first check if the port is already open
          'if its open then close it
          If comPort.IsOpen = True Then
              comPort.Close()
          End If
  
          'set the properties of our SerialPort Object
          comPort.BaudRate = Integer.Parse(_baudRate)
          'BaudRate
          comPort.DataBits = Integer.Parse(_dataBits)
          'DataBits
          comPort.StopBits = DirectCast([Enum].Parse(GetType(StopBits), _stopBits), StopBits)
          'StopBits
          comPort.Parity = DirectCast([Enum].Parse(GetType(Parity), _parity), Parity)
          'Parity
          comPort.PortName = _portName
          'PortName
          'now open the port
          comPort.Open()
          'display message
          _type = MessageType.Normal
          _msg = "Port opened at " + DateTime.Now + "" + Environment.NewLine + ""
          '_msg = Environment.NewLine
          DisplayData(_type, _msg)
          'return true
          Return True
      Catch ex As Exception
          DisplayData(MessageType.[Error], ex.Message)
          Return False
      End Try
  End Function
  Public Sub ClosePort()
      If comPort.IsOpen Then
          _msg = "Port closed at " + DateTime.Now + "" + Environment.NewLine + ""
          _type = MessageType.Normal
          DisplayData(_type, _msg)
          comPort.Close()
      End If
  End Sub
    Private Sub comPort_DataReceived(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs)
      'determine the mode the user selected (binary/string)
      Select Case CurrentTransmissionType
          Case TransmissionType.Text
              'user chose string
              'read data waiting in the buffer
              Dim msg As String = comPort.ReadExisting()
              'display the data to the user
              _type = MessageType.Incoming
              _msg = msg
              DisplayData(MessageType.Incoming, msg + "" + Environment.NewLine + "")
              Exit Select
          Case TransmissionType.Hex
              'user chose binary
              'retrieve number of bytes in the buffer
              Dim bytes As Integer = comPort.BytesToRead
              'create a byte array to hold the awaiting data
              Dim comBuffer As Byte() = New Byte(bytes - 1) {}
              'read the data and store it
              comPort.Read(comBuffer, 0, bytes)
              'display the data to the user
              _type = MessageType.Incoming
              _msg = ByteToHex(comBuffer) + "" + Environment.NewLine + ""
              DisplayData(MessageType.Incoming, ByteToHex(comBuffer) + "" + Environment.NewLine + "")
              Exit Select
          Case Else
              'read data waiting in the buffer
              Dim str As String = comPort.ReadExisting()
              'display the data to the user
              _type = MessageType.Incoming
              _msg = str + "" + Environment.NewLine + ""
              DisplayData(MessageType.Incoming, str + "" + Environment.NewLine + "")
              Exit Select
      End Select
  End Sub
  Private Sub MonitorSP_DataReceived(ByVal sender As Object, ByVal e As IO.Ports.SerialDataReceivedEventArgs)
      Dim SP As IO.Ports.SerialPort = DirectCast(sender, IO.Ports.SerialPort)
      Dim Data As String = SP.ReadLine
      'Remember this is in a background thread so you can not update the UI from here without using Me.Invoke
  End Sub
  
  Private Sub CloseSP()
      For Each selSP As IO.Ports.SerialPort In MonitorSP
          selSP.Close()
      Next
      MonitorSP.Clear()
  End Sub
   Private Sub cmdClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdClose.Click
      'Comm is an instance of the class that contains the methods I showed above like Datareceived and so forth
      comm.ClosePort()
      comm.ClosePort()
      'SetControlState()
      SetDefaults()
  End Sub
  </code></pre>
  
  <p>Now the challenge shows up when i try to close and re-open the port again when i close the port and wait for a few minutes and then attempt to reopen the port again it does not work (the readings do not show up on the scale). But when I close the port exit the form and then come back into the form and then open the port then the readings show up. My question is what can I do to open and close the port and the open it again without having to close the form or restart the app. </p>