title: Program drops com port while receiving data from arduino VB.net
body: <p>When running the code the program will run to complete one cycle, then the arduino must be disconnected and reconnected (unplugged and replugged in) in order to run again. Sometimes the program will even stop halfway through. </p>
  
  <p>I am very new to VB programming and any help would be appreciated</p>
  
  <p>There is no problem with the arduino code, it was fully tested before creating the VB user form. 
  tried running on multiple computers/Operating systems same results</p>
  
  <pre><code>Private Sub SerialPort1_DataReceived(sender As Object, e As SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
      ''reads data from arduino
      Dim datastream As String
  
      datastream = (SerialPort1.ReadLine)
      '' RichTextBox1.Text = datastream
  
      Data_manipulate(arr:=datastream)
  End Sub
  
  Public Sub Data_manipulate(ByRef arr As String)
      ''processes data to calculate linearity
      Dim linearity As Single
      Dim percent As Single
      Dim nominal As Single
      ReDim Preserve points(i)
      ReDim Preserve counts(i)
      Dim values As String() = arr.Split(New Char() {","c})
      percent = values(0) / values(3)
      nominal = values(1) / values(2)
      linearity = (percent - nominal) * 100
      TextBox3.Text = values(2) * (0.00118)
      points(i) = linearity
      counts(i) = nominal * 100
      TextBox8.Text = (points).max
      TextBox10.Text = points.Min
      Chart1.Series("Linearity").Points.AddXY(counts(i), points(i))
      ''Chart1.Series("Linearity").Points.AddXY(counts(i), points(i))
      If points.Max &gt; NumericUpDown1.Value Then
          TextBox8.BackColor = Color.Red
  
      End If
      If points.Min &lt; -NumericUpDown1.Value Then
          TextBox10.BackColor = Color.Red
  
      End If
      If nominal &lt; 0.5 Then
          TextBox13.Text = values(1) * (0.00118)
          Dim Halfpercent As Double = values(1) * 0.00118
          If ((0.5 * (values(2) * 0.00118)) - 0.015) &lt; Halfpercent AndAlso Halfpercent &lt; ((0.5 * (values(2) * 0.00118)) + 0.015) Then
              TextBox13.BackColor = Color.LimeGreen
          Else TextBox13.BackColor = Color.Red
          End If
  
      End If
      If percent = 1 Then
          Auto_Slope_And_Level_Button_Click()
  
      End If
      i += 1
  
  End Sub
  
  Private Sub Connect_Button_click(sender As Object, e As EventArgs) Handles Connect_Button.Click
      If ComboBox1.SelectedItem Is Nothing Then
          MessageBox.Show("Please Select a Port")
          Return
      End If
      SerialPort1.BaudRate = 9600
      SerialPort1.PortName = ComboBox1.SelectedItem
      If Connect_Button.Text = "Connect" Then
          Try
              SerialPort1.Open()
              Connect_Button.Text = "Connected"
              Disconnect_Button.Text = "Disconnect"
              Connect_Button.BackColor = Color.LimeGreen
  
          Catch ex As Exception
  
          End Try
      Else
  
      End If
  End Sub
  
  Private Sub Start_Button_Click(sender As Object, e As EventArgs) Handles Start_Button.Click
      If SerialPort1.IsOpen Then
          Chart1.Series("Linearity").Points.Clear()
          ReDim points(0)
          ReDim counts(0)
  
          SerialPort1.Write(1)
          TextBox8.Text = 0
          TextBox10.Text = 0
          TextBox2.Text = 0
  
      Else
          MessageBox.Show("Not Connected")
          Return
      End If
  End Sub
  
  Private Sub Disconnect_Button_Click(sender As Object, e As EventArgs) Handles Disconnect_Button.Click
  
      Try
          SerialPort1.Close()
          Disconnect_Button.Text = "Disconnected"
          Connect_Button.Text = "Connect"
          Connect_Button.BackColor = Color.FromArgb(255, 0, 0)
  
      Catch ex As Exception
  
      End Try
  
  End Sub
  
  Private Sub ComboBox1_DropDown(sender As Object, e As EventArgs) Handles ComboBox1.DropDown
      ComboBox1.Items.Clear()
  
      Try
      For Each port As String In SerialPort.GetPortNames
              ComboBox1.Items.Add(port)
          Next
          ComboBox1.SelectedItem = ""
      Catch ex As Exception
  
      End Try
  End Sub
  </code></pre>
  
  <p>A "com port does not exist" error will occur if you don't reset between runs. 
  If the program stops partway through the computer needs either a log-off or restart to reconnect with the arduino.</p>