-1

I am unable to handle these errors which cause my program to freeze.

How to handle all these? This is my debugger output:

A first chance exception of type 'System.IO.IOException' occurred in System.dll
A first chance exception of type 'System.InvalidOperationException' occurred in System.dll
A first chance exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll
A first chance exception of type 'System.TimeoutException' occurred in System.dll
A first chance exception of type 'System.IO.IOException' occurred in System.dll

I used

Try
    Dim str As String = SerialPort.ReadLine()
Catch ex As Exception
    MsgBox(ex)
End Try

But still the program freezes!

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Sumit Rai
  • 65
  • 5
  • 13
  • 2
    Those are first-chance exceptions, and should be ignored. They are not causing your program to freeze. – John Saunders Feb 05 '14 at 10:03
  • What if there is nothing to read... – Arthur Rey Feb 05 '14 at 10:14
  • Receive First-Chance Exception Notifications (http://msdn.microsoft.com/en-us/library/dd997368%28v=vs.110%29.aspx) but as John says they are not causing your program to freeze, seems like your SerialPort is not correctly initialized. – vzamanillo Feb 05 '14 at 10:18

1 Answers1

1

The ReadLine methods blocks the program until it is done reading.

You should use the DataReceived event, like this :

Public WithEvents serial As New SerialPort("COM1")
Public Sub serial_OnDataReceived(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs) Handles serial.DataReceived
    MsgBox(e.ToString)
End Sub

Of course Open() your SerialPort ;)

Edit :

If you really want to use ReadLine(), try to set a timeout :

Try
    SerialPort.ReadTimeout = 1000
    Dim str As String = SerialPort.ReadLine()
Catch ex As Exception
    MsgBox(ex)
End Try

It SHOULD stop reading, but I already experienced problems this way.

Arthur Rey
  • 2,990
  • 3
  • 19
  • 42