B
Ben Kim
Hello all,
I am learning about serial port communications using VB.NET 2005. However I
cannot get the proper event to fire when data is received from the other
end. When the following program connects to HyperTerminal, I get a "Got
Data" message. I can send data to hyperterminal without issue so it is not
a connection issue. Whenever I send data from hyperterminal to my program,
I get nothing. I am thinking it is a scoping issue but not positive. Can
someone please point me in the right direction? I have surfed the internet
and have not had any success in finding materials relating to this issue.
Dummy Test Program below:
-------
Imports System.IO.Ports
Public Class Form1
Dim objSerialPort As SerialPort
Private WithEvents evnHandler As
System.IO.Ports.SerialDataReceivedEventArgs
Public Sub evnHandler_DataReceived(ByVal sender As Object, ByVal e As
System.IO.Ports.SerialDataReceivedEventArgs)
MessageBox.Show("Got Data")
End Sub
Private Sub btnOpenPort_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnOpenPort.Click
If txtPortNo.Text = String.Empty Then
Exit Sub
End If
Try
objSerialPort = My.Computer.Ports.OpenSerialPort("COM" &
txtPortNo.Text, 19200, Parity.None, 8, StopBits.One)
AddHandler objSerialPort.DataReceived, AddressOf
evnHandler_DataReceived
objSerialPort.WriteLine("HELLO SERIAL PORT FROM VB.NET 2005!" &
Constants.vbCrLf)
Catch ex As Exception
MessageBox.Show(ex.Message, "Test Com Ports")
End Try
End Sub
Private Sub btnClosePort_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnClosePort.Click
If txtPortNo.Text = String.Empty Then
Exit Sub
End If
If IsNothing(objSerialPort) Then
Exit Sub
End If
Try
objSerialPort.Close()
Catch ex As Exception
MessageBox.Show(ex.Message, "Test Com Ports")
End Try
RemoveHandler objSerialPort.DataReceived, AddressOf
evnHandler_DataReceived
objSerialPort.Dispose()
objSerialPort = Nothing
End Sub
Private Sub btnSendText_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnSendText.Click
If txtPortNo.Text = String.Empty Then
Exit Sub
End If
If IsNothing(objSerialPort) Then
Exit Sub
End If
objSerialPort.WriteLine(txtSendText.Text & Constants.vbCrLf)
End Sub
End Class
I am learning about serial port communications using VB.NET 2005. However I
cannot get the proper event to fire when data is received from the other
end. When the following program connects to HyperTerminal, I get a "Got
Data" message. I can send data to hyperterminal without issue so it is not
a connection issue. Whenever I send data from hyperterminal to my program,
I get nothing. I am thinking it is a scoping issue but not positive. Can
someone please point me in the right direction? I have surfed the internet
and have not had any success in finding materials relating to this issue.
Dummy Test Program below:
-------
Imports System.IO.Ports
Public Class Form1
Dim objSerialPort As SerialPort
Private WithEvents evnHandler As
System.IO.Ports.SerialDataReceivedEventArgs
Public Sub evnHandler_DataReceived(ByVal sender As Object, ByVal e As
System.IO.Ports.SerialDataReceivedEventArgs)
MessageBox.Show("Got Data")
End Sub
Private Sub btnOpenPort_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnOpenPort.Click
If txtPortNo.Text = String.Empty Then
Exit Sub
End If
Try
objSerialPort = My.Computer.Ports.OpenSerialPort("COM" &
txtPortNo.Text, 19200, Parity.None, 8, StopBits.One)
AddHandler objSerialPort.DataReceived, AddressOf
evnHandler_DataReceived
objSerialPort.WriteLine("HELLO SERIAL PORT FROM VB.NET 2005!" &
Constants.vbCrLf)
Catch ex As Exception
MessageBox.Show(ex.Message, "Test Com Ports")
End Try
End Sub
Private Sub btnClosePort_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnClosePort.Click
If txtPortNo.Text = String.Empty Then
Exit Sub
End If
If IsNothing(objSerialPort) Then
Exit Sub
End If
Try
objSerialPort.Close()
Catch ex As Exception
MessageBox.Show(ex.Message, "Test Com Ports")
End Try
RemoveHandler objSerialPort.DataReceived, AddressOf
evnHandler_DataReceived
objSerialPort.Dispose()
objSerialPort = Nothing
End Sub
Private Sub btnSendText_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnSendText.Click
If txtPortNo.Text = String.Empty Then
Exit Sub
End If
If IsNothing(objSerialPort) Then
Exit Sub
End If
objSerialPort.WriteLine(txtSendText.Text & Constants.vbCrLf)
End Sub
End Class