Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

reading data from serial port vb.net 2012

Status
Not open for further replies.

shaminda

Programmer
Jun 9, 2000
170
0
0
US
I am trying to read the following data from 4 serial ports in VB.Net 2012

00056
00056
00056
00056
00056

Its 5 lines of data. The starting digit is a space and there are four lines of data. I have tried so many different code but none has worked. I have searched only and had no success. All the samples I have seen talks to only one serial port. My newest code is as follows which I got from a Microsoft website. If I call this function 4 times will it work?

Code:
Public Function ReceiveSerialDataPort8() As String
        ' Receive strings from a serial port. 
        Dim returnStr As String = ""

        Dim com1 As IO.Ports.SerialPort = Nothing
        Try
            com1 = My.Computer.Ports.OpenSerialPort("COM8", 9600, Parity.None, 8, StopBits.One)
            com1.ReadTimeout = 10000
            Do
                Dim Incoming As String = com1.ReadLine()
                If Incoming Is Nothing Then
                    Exit Do
                Else
                    returnStr &= Incoming & vbCrLf
                End If
            Loop
        Catch ex As TimeoutException
            returnStr = "Error: Serial Port read timed out."
        Finally
            If com1 IsNot Nothing Then com1.Close()
        End Try

        Return returnStr
    End Function

Thanks,

Shaminda
 

First, you will need an array/collection/list of SerialPort objects, one for each port.

Private SerialPorts As New List(Of IO.Ports.SerialPort)

Next, loop through all serial ports, get each port's name, open it and add it to the list:

Code:
Dim ThisSP As IO.Ports.SerialPort

For Each sp As String In IO.Ports.SerialPort.GetPortNames()
    
    ThisSP = New IO.Ports.SerialPort(sp)

    ThisSP.Open()
        
    If ThisSP.IsOpen Then
        AddHandler ThisSP.DataReceived, AddressOf SerialPort_DataReceived
        SerialPorts.Add(ThisSP)
    End If

Next

Next, add an event handler for the SerialPort object's DataReceived event. This is linked to each SerialPort object in the list by the AddHandler code above.

Code:
Private Sub SerialPort_DataReceived(ByVal sender As Object, ByVal e As IO.Ports.SerialDataReceivedEventArgs)
    Dim ThisSP As IO.Ports.SerialPort = CType(sender, IO.Ports.SerialPort)
    Dim Data As String = ThisSP .ReadLine

    'do something with the data here

End Sub

Finally, some code to close all the SerialPort objects when you're finished:

Code:
Private Sub CloseSerialPorts()

    For Each sp As IO.Ports.SerialPort In SerialPorts
        sp.Close()
    Next

    SerialPorts.Clear()

End Sub

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top