Serial port hang/freeze when trying to disconnect

this is my program, can you look what debug should i use so it will not freeze

Imports System
Imports System.IO.Ports
Imports System.Threading
Imports System.ComponentModel

Public Class Form1
Dim comPORT As String
Dim receivedData As String = “”

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the ‘Cashsectiondb_01DataSet1.studinfo’ table. You can move, or remove it, as needed.
Timer1.Enabled = False
comPORT = “”
For Each sp As String In My.Computer.Ports.SerialPortNames
End Sub

Private Sub comPort_ComboBox_SelectedIndexChanged(sender As Object, e As EventArgs) Handles comPort_ComboBox.SelectedIndexChanged
If (comPort_ComboBox.SelectedItem <> “”) Then
comPORT = comPort_ComboBox.SelectedItem

End If
End Sub

Private Sub connect_BTN_Click(sender As Object, e As EventArgs) Handles connect_BTN.Click
If (connect_BTN.Text = “Connect”) Then
If (comPORT <> “”) Then
SerialPort1.PortName = comPORT
SerialPort1.BaudRate = 9600
SerialPort1.DataBits = 8
SerialPort1.Parity = Parity.None
SerialPort1.StopBits = StopBits.One
SerialPort1.Handshake = Handshake.None
SerialPort1.Encoding = System.Text.Encoding.Default
SerialPort1.ReadTimeout = 10000

connect_BTN.Text = “Dis-connect”
Timer1.Enabled = True
Timer_LBL.Text = “Timer: ON”
MsgBox(“Select a COM port first”)
End If
connect_BTN.Text = “Connect”
Timer1.Enabled = False
Timer_LBL.Text = “Timer: OFF”
End If
End Sub

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
receivedData = ReceiveSerialData()
RichTextBox1.Text &= receivedData
If RichTextBox1.Text = “” Then
End If

Catch ex As Exception

End Try
End Sub

Function ReceiveSerialData() As String
Dim Incoming As String
Incoming = SerialPort1.ReadExisting()
If Incoming Is Nothing Then

Return “nothing” & vbCrLf
Return Incoming
End If
Catch ex As TimeoutException
Return “Error: Serial Port read timed out.”
End Try

End Function

Private Sub clear_BTN_Click(sender As Object, e As EventArgs) Handles clear_BTN.Click
RichTextBox1.Text = “”
End Sub

Private Sub StudinfoBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs) Handles StudinfoBindingNavigatorSaveItem.Click

End Sub

End Class

this is my program, can you look what debug should i use so it will not freeze

First, I have serious doubts that that code will compile for the Arduino.

Second, you have said nothing about what the program does, or how you detect that it has frozen.

Third, periodically reading serial data is wrong. You read serial data when it arrives. not when you feel like it. The Serial object has many ways to buffer the data, and call your callback when there is a useful amount of data to process. Use one or more of them, NOT a timer.

its a queing machine, when i connect the port from arduino to VB , after i select the port the push button works well but the form freeze which i cant click the disconnect button for the port

the push button works well

Which push button?

but the form freeze

What causes that? What is the Arduino doing?

Using a timer to determine when to read serial data is still a (very) bad idea.

look at the picture, thats the output of the program, i can connect the port, the code is when u push the keypress it will go to the nextline, like the line que which we normally see at fall in line machine, the system works but the form freeze

the system works but the form freeze

I guess, then, that you'll need to figure out why.

I'm going to guess, though, that the timer fires, and the program tries to read non-existent serial data, and simply waits for some to become available. Meanwhile, the Arduino is not sending any, because it is waiting for input from the program.

Looks like you have a catch statement for timing out, but you don’t have one for serial not available.

As PaulS said, if the timer fires and it tries to retrieve data from your disconnected serial port, it will lock up.

Try something like this:

catch (InvalidOperationException^ exception)
   std::cout << "Serial disconnected" << "\n";

Which I’m assuming would be equivalent to:

Catch ex As InvalidOperationException