Newbie to VB.NET 2005 (and DOT NET in general)

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
 
B

Ben Kim

That would be great. I would rather try to do this through code using DOT
NET and without support from COM objects. The V2 of the Framework has
Serial comms builtin but hardly any documentation as to how to correctly use
them.

If you don't mind please send the class to bkim AT emergitech DOT com.

Thanks!

Ben
 
T

t

Hi,
I have almost the same trouble. Would you pls send me a share of your
class. Thanks a lot.
 
T

t

I forgot to say, developerblue.blogspot.com/ seems unreachable. I have tried
many times, but failed.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top