Results 1 to 2 of 2

Thread: [2008] SerialPort DataRCV Handler

  1. #1

    Thread Starter
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    [2008] SerialPort DataRCV Handler

    I have a SerialPort DataReceived Handler that I am fond of. So I thought why not move it to it's own Module and re-use it.

    Well, that didn't work out well, even though I got it to work.

    So how would you go about having a generic SerialPort DataReceived Handler?
    (I have been copy / pasting the code)
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  2. #2
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: [2008] SerialPort DataRCV Handler

    Something like this maybe?
    vb.net Code:
    1. Public Class SerialPortDataReceiver
    2.  
    3.     Private comPort As System.IO.Ports.SerialPort = Nothing
    4.     Private _DisplayControl As Control = Nothing
    5.  
    6.     Public Sub New(ByVal serialPort As System.IO.Ports.SerialPort)
    7.         Me.comPort = serialPort
    8.         AddHandler Me.comPort.DataReceived, AddressOf SerialPortDataReceived
    9.         Me.OpenPort()
    10.     End Sub
    11.  
    12.     Public Sub New(ByVal serialPort As System.IO.Ports.SerialPort, ByVal displayControl As Control)
    13.         Me.comPort = serialPort
    14.         AddHandler Me.comPort.DataReceived, AddressOf SerialPortDataReceived
    15.         Me._DisplayControl = displayControl
    16.         Me.OpenPort()
    17.     End Sub
    18.  
    19.     Public Property DisplayControl() As Control
    20.         Get
    21.             Return Me._DisplayControl
    22.         End Get
    23.         Set(ByVal value As Control)
    24.             Me._DisplayControl = value
    25.         End Set
    26.     End Property
    27.  
    28.     Public ReadOnly Property SerialPort() As System.IO.Ports.SerialPort
    29.         Get
    30.             Return Me.comPort
    31.         End Get
    32.     End Property
    33.  
    34.     Private Sub SerialPortDataReceived(ByVal sender As Object, _
    35.                     ByVal e As System.IO.Ports.SerialDataReceivedEventArgs)
    36.         If Me._DisplayControl IsNot Nothing Then
    37.             Me._DisplayControl.Invoke(New myDelegate(AddressOf UpdateDisplayControlText), New Object() {})
    38.         End If
    39.     End Sub
    40.  
    41.     Private Delegate Sub myDelegate()
    42.  
    43.     Private Sub UpdateDisplayControlText()
    44.         Dim intBytesAvailable As Integer = comPort.BytesToRead
    45.         If intBytesAvailable > 0 Then
    46.             Me._DisplayControl.Text = comPort.ReadExisting()
    47.         End If
    48.     End Sub
    49.  
    50.     Public Sub SendCommand(ByVal command As String)
    51.         If Me.comPort.IsOpen() Then
    52.             Me.comPort.Write(command)
    53.         Else
    54.             MessageBox.Show(Me.comPort.PortName & " is closed!")
    55.         End If
    56.     End Sub
    57.  
    58.     Private Sub OpenPort()
    59.         If Not Me.comPort.IsOpen Then
    60.             Try
    61.                 Me.comPort.Open()
    62.             Catch ex As Exception
    63.                 Throw New IO.IOException("Can not open serial port " & Me.comPort.PortName, ex)
    64.             End Try
    65.         End If
    66.     End Sub
    67. End Class
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width