Trouble with SerialPort class in VB

N

no-one

Hi Guys,

I am fairly new to Dot Net and I am trying to write a program that
listens to the serial port and displays the string in a text box or
listbox. I am using VS 2005 and framework 2.0

my code is thus ( don't laugh to much)

Public Class Form1
Delegate Sub SetTextCallback(ByVal [text] As String)

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
PortMon.PortName = "COM1"
PortMon.Open()
End Sub


Private Sub PortMon_DataReceived(ByVal sender As Object, ByVal e
As System.IO.Ports.SerialDataReceivedEventArgs) Handles
PortMon.DataReceived
Dim incoming As String
incoming = (PortMon.ReadLine.ToString)
Call SetText(incoming)
End Sub

Private Sub SetText(ByVal [text] As String)
If Me.ListBox1.InvokeRequired Then
Dim d As New SetTextCallback(AddressOf SetText)
Me.Invoke(d, New Object() {[text]})
Else
Me.ListBox1.Items.Add([text])
End If
End Sub
End Class


I know that a separate thread is used for the datareceived event and
hence the delegate sub ( found on another website). My problem occurs
when i close the program,causing exceptions. I believe the
datareceived thread is still trying to run. I did not want to get
involved with threading at this early stage but if I have to...

I managed to get this working using VS2003 with a separate serial port
class but I wanted to use the microsoft serialport class.
 
D

DickGrier

Hi,

I have example code on my homepage. Perhaps it will help.

--
Richard Grier, MVP
Hard & Software
Author of Visual Basic Programmer's Guide to Serial Communications, Fourth
Edition, ISBN 1-890422-28-2, Mabry Publishing (391 pages, includes CD-ROM).
July 2004.
See www.hardandsoftware.net for details and contact information.
 

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