Home All Groups Group Topic Archive Search About

Trouble with SerialPort class in VB

Author
21 Jan 2006 10:05 AM
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.

Author
21 Jan 2006 5:44 PM
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.
Author
21 Jan 2006 8:35 PM
no-one
Many Thanks



On Sat, 21 Jan 2006 10:44:35 -0700, "DickGrier"
<dick_grierNOSPAM@.msn.com> wrote:

Show quote
>Hi,
>
>I have example code on my homepage. Perhaps it will help.

AddThis Social Bookmark Button