I get a thread exception in this class the second time the Callback
"SerialPort_Dat aReceived" is called. The first
time its fine but the subsequent time I get a thread message
This call is created in frmMain
Dim WithEvents SerialPort As New cRs232
'My Serial class code
Option Explicit On
Option Strict On
Imports System.IO.Ports
Public Class cRs232
Private WithEvents SerialPort As New SerialPort
Public Event Rs232DataReceiv ed(ByVal Rs232ReceivedDa ta As String)
Public Event Rs232DataSent(B yVal Rs232DataSent As String)
Private Sub SerialPort_Data Received(ByVal sender As Object, ByVal e As
System.IO.Ports .SerialDataRece ivedEventArgs) Handles SerialPort.Data Received
Dim Rs232DataReceiv ed As String = SerialPort.Read Line
'THREAD ERROR MESSAGE HERE THE SECOND TIME THIS GETS CALLED!
'RaiseEvent Rs232DataReceiv ed(Rs232DataRec eived)
End Sub
Public Sub OpenPort()
Try
SerialPort.Baud Rate = 9600
SerialPort.Port Name = "Com3"
' Open the serial port
SerialPort.Open ()
' Set the event handler for data reception
AddHandler SerialPort.Data Received, AddressOf SerialPort_Data Received
Catch ex As System.Exceptio n
MessageBox.Show (ex.Message)
End Try
End Sub
Public Sub ClosePort()
SerialPort.Clos e()
End Sub
Public Sub SendData(ByVal cmd As String)
cmd += vbCrLf
'Dim Bytes() As Byte = System.Text.Enc oding.GetEncodi ng(1252).GetByt es(cmd)
Dim Bytes() As Byte = System.Text.Enc oding.Default.G etBytes(cmd)
Try
' Write a line to the serial port
SerialPort.Writ e(Bytes, 0, (Bytes.Length - 1))
'txtData.Append Text(Now.ToShor tTimeString & "->Sent: " & cmd)
'RaiseEvent Rs232DataSent(c md)
Catch ex As System.Exceptio n
MessageBox.Show (ex.Message)
End Try
End Sub
End Class