Handling DataReceived From Array/List of IO.SerialPort

Posted on 2011-10-25
Last Modified: 2012-05-12
I am refactoring a problem in that runs on one of our servers. Its role is to take input from barcode scanners connected to various virtual com ports and put the input to a database. Beofre there was 40 or so SerialPort components in the application; but now I am creating them dynamically into a array like this:

Dim PortList As New List(Of IO.Ports.SerialPort)

While x < rc
                PortList.Add(New IO.Ports.SerialPort)
                PortList(x).PortName = dset.Tables(0).Rows(x)("portname")
                PortList(x).ReadTimeout = 1500
                    For Each cCont As Label In gbStations.Controls
                        If cCont.Name = "Label" & CStr(x + 1) Then
                            cCont.BackColor = Color.Green
                        End If
                    Next cCont
                Catch ex As Exception
                    For Each cCont As Label In gbStations.Controls
                        If cCont.Name = "Label" & CStr(x + 1) Then
                            cCont.BackColor = Color.Red
                        End If
                    Next cCont

                End Try
                x = x + 1

Open in new window

What I want to do know is convert the fuicntion that recieves the data to use this array of ports for the DataReceived evetn. Before it was something like this

Public Sub getData (ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived, SerialPort2.DataReceived, SerialPort3.DataReceived etc.

Open in new window

Can anyone explain to me how to handle the DataReceived events dynamically? Thanks
Question by:wellso
    LVL 83

    Accepted Solution

    Remove the

    Handles SerialPort1.DataReceived, SerialPort2.DataReceived, SerialPort3.DataReceived etc.

    bit from the method

    and add

    AddHandler PortList(x).DataReceived, Addressof getData

    in the loop.
    LVL 4

    Expert Comment

    When you are creating your ports use a lambada delegate

    var port = new  IO.Ports.SerialPort
    port.DataReceived += (s,e) => { //perform your event code here }
    port.PortName = dset.Tables(0).Rows(x)("portname")

    LVL 2

    Author Closing Comment

    Thaks for the help
    LVL 83

    Expert Comment

    Glad to help :-)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
    Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
    The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
    The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now